You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RewindableGenerator.php 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Illuminate\Container;
  3. use Countable;
  4. use IteratorAggregate;
  5. class RewindableGenerator implements Countable, IteratorAggregate
  6. {
  7. /**
  8. * The generator callback.
  9. *
  10. * @var callable
  11. */
  12. protected $generator;
  13. /**
  14. * The number of tagged services.
  15. *
  16. * @var callable|int
  17. */
  18. protected $count;
  19. /**
  20. * Create a new generator instance.
  21. *
  22. * @param callable $generator
  23. * @param callable|int $count
  24. * @return void
  25. */
  26. public function __construct(callable $generator, $count)
  27. {
  28. $this->count = $count;
  29. $this->generator = $generator;
  30. }
  31. /**
  32. * Get an iterator from the generator.
  33. *
  34. * @return mixed
  35. */
  36. public function getIterator()
  37. {
  38. return ($this->generator)();
  39. }
  40. /**
  41. * Get the total number of tagged services.
  42. *
  43. * @return int
  44. */
  45. public function count()
  46. {
  47. if (is_callable($count = $this->count)) {
  48. $this->count = $count();
  49. }
  50. return $this->count;
  51. }
  52. }