RewindableGenerator.php
TLDR
The RewindableGenerator
class in the Illuminate\Container
namespace is used to create a generator instance that implements the Countable
and IteratorAggregate
interfaces. It allows for iterating over and counting tagged services.
Classes
RewindableGenerator
The RewindableGenerator
class is used to create a generator instance that can be iterated over and counted. It implements the Countable
and IteratorAggregate
interfaces. The class has the following properties:
-
$generator
(protected): The generator callback. -
$count
(protected): The number of tagged services or a callable that returns the count.
The class has the following methods:
-
__construct(callable $generator, $count)
: Initializes a newRewindableGenerator
instance. It takes a generator callback and the count of tagged services. -
getIterator(): Traversable
: Returns an iterator from the generator. -
count(): int
: Returns the total number of tagged services.
<?php
namespace Illuminate\Container;
use Countable;
use IteratorAggregate;
use Traversable;
class RewindableGenerator implements Countable, IteratorAggregate
{
/**
* The generator callback.
*
* @var callable
*/
protected $generator;
/**
* The number of tagged services.
*
* @var callable|int
*/
protected $count;
/**
* Create a new generator instance.
*
* @param callable $generator
* @param callable|int $count
* @return void
*/
public function __construct(callable $generator, $count)
{
$this->count = $count;
$this->generator = $generator;
}
/**
* Get an iterator from the generator.
*
* @return \Traversable
*/
public function getIterator(): Traversable
{
return ($this->generator)();
}
/**
* Get the total number of tagged services.
*
* @return int
*/
public function count(): int
{
if (is_callable($count = $this->count)) {
$this->count = $count();
}
return $this->count;
}
}