RewindableGenerator.php 934 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 callable $generator
  20. * @param int|callable $count
  21. */
  22. public function __construct(callable $generator, $count)
  23. {
  24. $this->generator = $generator;
  25. $this->count = $count;
  26. }
  27. public function getIterator()
  28. {
  29. $g = $this->generator;
  30. return $g();
  31. }
  32. public function count()
  33. {
  34. if (is_callable($count = $this->count)) {
  35. $this->count = $count();
  36. }
  37. return $this->count;
  38. }
  39. }