RepeatedPass.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Boolean
  22. */
  23. private $repeat = false;
  24. /**
  25. * @var RepeatablePassInterface[]
  26. */
  27. private $passes;
  28. /**
  29. * Constructor.
  30. *
  31. * @param RepeatablePassInterface[] $passes An array of RepeatablePassInterface objects
  32. *
  33. * @throws InvalidArgumentException when the passes don't implement RepeatablePassInterface
  34. */
  35. public function __construct(array $passes)
  36. {
  37. foreach ($passes as $pass) {
  38. if (!$pass instanceof RepeatablePassInterface) {
  39. throw new InvalidArgumentException('$passes must be an array of RepeatablePassInterface.');
  40. }
  41. $pass->setRepeatedPass($this);
  42. }
  43. $this->passes = $passes;
  44. }
  45. /**
  46. * Process the repeatable passes that run more than once.
  47. *
  48. * @param ContainerBuilder $container
  49. */
  50. public function process(ContainerBuilder $container)
  51. {
  52. $this->repeat = false;
  53. foreach ($this->passes as $pass) {
  54. $pass->process($container);
  55. }
  56. if ($this->repeat) {
  57. $this->process($container);
  58. }
  59. }
  60. /**
  61. * Sets if the pass should repeat
  62. */
  63. public function setRepeat()
  64. {
  65. $this->repeat = true;
  66. }
  67. /**
  68. * Returns the passes
  69. *
  70. * @return RepeatablePassInterface[] An array of RepeatablePassInterface objects
  71. */
  72. public function getPasses()
  73. {
  74. return $this->passes;
  75. }
  76. }