RewindableGenerator.php 916 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Argument;
  11. /**
  12. * @internal
  13. */
  14. class RewindableGenerator implements \IteratorAggregate, \Countable
  15. {
  16. private $generator;
  17. private $count;
  18. /**
  19. * @param int|callable $count
  20. */
  21. public function __construct(callable $generator, $count)
  22. {
  23. $this->generator = $generator;
  24. $this->count = $count;
  25. }
  26. public function getIterator(): \Traversable
  27. {
  28. $g = $this->generator;
  29. return $g();
  30. }
  31. public function count(): int
  32. {
  33. if (\is_callable($count = $this->count)) {
  34. $this->count = $count();
  35. }
  36. return $this->count;
  37. }
  38. }