AutowireExceptionPass.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. /**
  13. * Throws autowire exceptions from AutowirePass for definitions that still exist.
  14. *
  15. * @author Ryan Weaver <ryan@knpuniversity.com>
  16. */
  17. class AutowireExceptionPass implements CompilerPassInterface
  18. {
  19. private $autowirePass;
  20. private $inlineServicePass;
  21. public function __construct(AutowirePass $autowirePass, InlineServiceDefinitionsPass $inlineServicePass)
  22. {
  23. $this->autowirePass = $autowirePass;
  24. $this->inlineServicePass = $inlineServicePass;
  25. }
  26. public function process(ContainerBuilder $container)
  27. {
  28. // the pass should only be run once
  29. if (null === $this->autowirePass || null === $this->inlineServicePass) {
  30. return;
  31. }
  32. $inlinedIds = $this->inlineServicePass->getInlinedServiceIds();
  33. $exceptions = $this->autowirePass->getAutowiringExceptions();
  34. // free up references
  35. $this->autowirePass = null;
  36. $this->inlineServicePass = null;
  37. foreach ($exceptions as $exception) {
  38. if ($this->doesServiceExistInTheContainer($exception->getServiceId(), $container, $inlinedIds)) {
  39. throw $exception;
  40. }
  41. }
  42. }
  43. private function doesServiceExistInTheContainer($serviceId, ContainerBuilder $container, array $inlinedIds)
  44. {
  45. if ($container->hasDefinition($serviceId)) {
  46. return true;
  47. }
  48. // was the service inlined? Of so, does its parent service exist?
  49. if (isset($inlinedIds[$serviceId])) {
  50. foreach ($inlinedIds[$serviceId] as $parentId) {
  51. if ($this->doesServiceExistInTheContainer($parentId, $container, $inlinedIds)) {
  52. return true;
  53. }
  54. }
  55. }
  56. return false;
  57. }
  58. }