RepeatedPass.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\Compiler;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  13. /**
  14. * A pass that might be run repeatedly.
  15. *
  16. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  17. */
  18. class RepeatedPass implements CompilerPassInterface
  19. {
  20. /**
  21. * @var bool
  22. */
  23. private $repeat = false;
  24. private $passes;
  25. /**
  26. * @param RepeatablePassInterface[] $passes An array of RepeatablePassInterface objects
  27. *
  28. * @throws InvalidArgumentException when the passes don't implement RepeatablePassInterface
  29. */
  30. public function __construct(array $passes)
  31. {
  32. foreach ($passes as $pass) {
  33. if (!$pass instanceof RepeatablePassInterface) {
  34. throw new InvalidArgumentException('$passes must be an array of RepeatablePassInterface.');
  35. }
  36. $pass->setRepeatedPass($this);
  37. }
  38. $this->passes = $passes;
  39. }
  40. /**
  41. * Process the repeatable passes that run more than once.
  42. */
  43. public function process(ContainerBuilder $container)
  44. {
  45. do {
  46. $this->repeat = false;
  47. foreach ($this->passes as $pass) {
  48. $pass->process($container);
  49. }
  50. } while ($this->repeat);
  51. }
  52. /**
  53. * Sets if the pass should repeat.
  54. */
  55. public function setRepeat()
  56. {
  57. $this->repeat = true;
  58. }
  59. /**
  60. * Returns the passes.
  61. *
  62. * @return RepeatablePassInterface[] An array of RepeatablePassInterface objects
  63. */
  64. public function getPasses()
  65. {
  66. return $this->passes;
  67. }
  68. }