vendor/symfony/dependency-injection/Compiler/AutowirePass.php line 321

Open in your IDE?
  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\Config\Resource\ClassExistenceResource;
  12. use Symfony\Component\DependencyInjection\Config\AutowireServiceResource;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
  16. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  17. use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
  18. use Symfony\Component\DependencyInjection\TypedReference;
  19. /**
  20.  * Inspects existing service definitions and wires the autowired ones using the type hints of their classes.
  21.  *
  22.  * @author Kévin Dunglas <dunglas@gmail.com>
  23.  * @author Nicolas Grekas <p@tchwork.com>
  24.  */
  25. class AutowirePass extends AbstractRecursivePass
  26. {
  27.     private $definedTypes = [];
  28.     private $types;
  29.     private $ambiguousServiceTypes;
  30.     private $autowired = [];
  31.     private $lastFailure;
  32.     private $throwOnAutowiringException;
  33.     private $autowiringExceptions = [];
  34.     private $strictMode;
  35.     /**
  36.      * @param bool $throwOnAutowireException Errors can be retrieved via Definition::getErrors()
  37.      */
  38.     public function __construct($throwOnAutowireException true)
  39.     {
  40.         $this->throwOnAutowiringException $throwOnAutowireException;
  41.     }
  42.     /**
  43.      * @deprecated since version 3.4, to be removed in 4.0.
  44.      *
  45.      * @return AutowiringFailedException[]
  46.      */
  47.     public function getAutowiringExceptions()
  48.     {
  49.         @trigger_error('Calling AutowirePass::getAutowiringExceptions() is deprecated since Symfony 3.4 and will be removed in 4.0. Use Definition::getErrors() instead.'E_USER_DEPRECATED);
  50.         return $this->autowiringExceptions;
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function process(ContainerBuilder $container)
  56.     {
  57.         // clear out any possibly stored exceptions from before
  58.         $this->autowiringExceptions = [];
  59.         $this->strictMode $container->hasParameter('container.autowiring.strict_mode') && $container->getParameter('container.autowiring.strict_mode');
  60.         try {
  61.             parent::process($container);
  62.         } finally {
  63.             $this->definedTypes = [];
  64.             $this->types null;
  65.             $this->ambiguousServiceTypes null;
  66.             $this->autowired = [];
  67.         }
  68.     }
  69.     /**
  70.      * Creates a resource to help know if this service has changed.
  71.      *
  72.      * @return AutowireServiceResource
  73.      *
  74.      * @deprecated since version 3.3, to be removed in 4.0. Use ContainerBuilder::getReflectionClass() instead.
  75.      */
  76.     public static function createResourceForClass(\ReflectionClass $reflectionClass)
  77.     {
  78.         @trigger_error('The '.__METHOD__.'() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use ContainerBuilder::getReflectionClass() instead.'E_USER_DEPRECATED);
  79.         $metadata = [];
  80.         foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
  81.             if (!$reflectionMethod->isStatic()) {
  82.                 $metadata[$reflectionMethod->name] = self::getResourceMetadataForMethod($reflectionMethod);
  83.             }
  84.         }
  85.         return new AutowireServiceResource($reflectionClass->name$reflectionClass->getFileName(), $metadata);
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     protected function processValue($value$isRoot false)
  91.     {
  92.         try {
  93.             return $this->doProcessValue($value$isRoot);
  94.         } catch (AutowiringFailedException $e) {
  95.             if ($this->throwOnAutowiringException) {
  96.                 throw $e;
  97.             }
  98.             $this->autowiringExceptions[] = $e;
  99.             $this->container->getDefinition($this->currentId)->addError($e->getMessage());
  100.             return parent::processValue($value$isRoot);
  101.         }
  102.     }
  103.     private function doProcessValue($value$isRoot false)
  104.     {
  105.         if ($value instanceof TypedReference) {
  106.             if ($ref $this->getAutowiredReference($value$value->getRequiringClass() ? sprintf('for "%s" in "%s"'$value->getType(), $value->getRequiringClass()) : '')) {
  107.                 return $ref;
  108.             }
  109.             $this->container->log($this$this->createTypeNotFoundMessage($value'it'));
  110.         }
  111.         $value parent::processValue($value$isRoot);
  112.         if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
  113.             return $value;
  114.         }
  115.         if (!$reflectionClass $this->container->getReflectionClass($value->getClass(), false)) {
  116.             $this->container->log($thissprintf('Skipping service "%s": Class or interface "%s" cannot be loaded.'$this->currentId$value->getClass()));
  117.             return $value;
  118.         }
  119.         $methodCalls $value->getMethodCalls();
  120.         try {
  121.             $constructor $this->getConstructor($valuefalse);
  122.         } catch (RuntimeException $e) {
  123.             throw new AutowiringFailedException($this->currentId$e->getMessage(), 0$e);
  124.         }
  125.         if ($constructor) {
  126.             array_unshift($methodCalls, [$constructor$value->getArguments()]);
  127.         }
  128.         $methodCalls $this->autowireCalls($reflectionClass$methodCalls);
  129.         if ($constructor) {
  130.             list(, $arguments) = array_shift($methodCalls);
  131.             if ($arguments !== $value->getArguments()) {
  132.                 $value->setArguments($arguments);
  133.             }
  134.         }
  135.         if ($methodCalls !== $value->getMethodCalls()) {
  136.             $value->setMethodCalls($methodCalls);
  137.         }
  138.         return $value;
  139.     }
  140.     /**
  141.      * @return array
  142.      */
  143.     private function autowireCalls(\ReflectionClass $reflectionClass, array $methodCalls)
  144.     {
  145.         foreach ($methodCalls as $i => $call) {
  146.             list($method$arguments) = $call;
  147.             if ($method instanceof \ReflectionFunctionAbstract) {
  148.                 $reflectionMethod $method;
  149.             } else {
  150.                 $definition = new Definition($reflectionClass->name);
  151.                 try {
  152.                     $reflectionMethod $this->getReflectionMethod($definition$method);
  153.                 } catch (RuntimeException $e) {
  154.                     if ($definition->getFactory()) {
  155.                         continue;
  156.                     }
  157.                     throw $e;
  158.                 }
  159.             }
  160.             $arguments $this->autowireMethod($reflectionMethod$arguments);
  161.             if ($arguments !== $call[1]) {
  162.                 $methodCalls[$i][1] = $arguments;
  163.             }
  164.         }
  165.         return $methodCalls;
  166.     }
  167.     /**
  168.      * Autowires the constructor or a method.
  169.      *
  170.      * @return array The autowired arguments
  171.      *
  172.      * @throws AutowiringFailedException
  173.      */
  174.     private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, array $arguments)
  175.     {
  176.         $class $reflectionMethod instanceof \ReflectionMethod $reflectionMethod->class $this->currentId;
  177.         $method $reflectionMethod->name;
  178.         $parameters $reflectionMethod->getParameters();
  179.         if (method_exists('ReflectionMethod''isVariadic') && $reflectionMethod->isVariadic()) {
  180.             array_pop($parameters);
  181.         }
  182.         foreach ($parameters as $index => $parameter) {
  183.             if (\array_key_exists($index$arguments) && '' !== $arguments[$index]) {
  184.                 continue;
  185.             }
  186.             $type ProxyHelper::getTypeHint($reflectionMethod$parametertrue);
  187.             if (!$type) {
  188.                 if (isset($arguments[$index])) {
  189.                     continue;
  190.                 }
  191.                 // no default value? Then fail
  192.                 if (!$parameter->isDefaultValueAvailable()) {
  193.                     // For core classes, isDefaultValueAvailable() can
  194.                     // be false when isOptional() returns true. If the
  195.                     // argument *is* optional, allow it to be missing
  196.                     if ($parameter->isOptional()) {
  197.                         continue;
  198.                     }
  199.                     $type ProxyHelper::getTypeHint($reflectionMethod$parameterfalse);
  200.                     $type $type sprintf('is type-hinted "%s"'$type) : 'has no type-hint';
  201.                     throw new AutowiringFailedException($this->currentIdsprintf('Cannot autowire service "%s": argument "$%s" of method "%s()" %s, you should configure its value explicitly.'$this->currentId$parameter->name$class !== $this->currentId $class.'::'.$method $method$type));
  202.                 }
  203.                 // specifically pass the default value
  204.                 $arguments[$index] = $parameter->getDefaultValue();
  205.                 continue;
  206.             }
  207.             if (!$value $this->getAutowiredReference($ref = new TypedReference($type$type, !$parameter->isOptional() ? $class ''), 'for '.sprintf('argument "$%s" of method "%s()"'$parameter->name$class.'::'.$method))) {
  208.                 $failureMessage $this->createTypeNotFoundMessage($refsprintf('argument "$%s" of method "%s()"'$parameter->name$class !== $this->currentId $class.'::'.$method $method));
  209.                 if ($parameter->isDefaultValueAvailable()) {
  210.                     $value $parameter->getDefaultValue();
  211.                 } elseif (!$parameter->allowsNull()) {
  212.                     throw new AutowiringFailedException($this->currentId$failureMessage);
  213.                 }
  214.                 $this->container->log($this$failureMessage);
  215.             }
  216.             $arguments[$index] = $value;
  217.         }
  218.         if ($parameters && !isset($arguments[++$index])) {
  219.             while (<= --$index) {
  220.                 $parameter $parameters[$index];
  221.                 if (!$parameter->isDefaultValueAvailable() || $parameter->getDefaultValue() !== $arguments[$index]) {
  222.                     break;
  223.                 }
  224.                 unset($arguments[$index]);
  225.             }
  226.         }
  227.         // it's possible index 1 was set, then index 0, then 2, etc
  228.         // make sure that we re-order so they're injected as expected
  229.         ksort($arguments);
  230.         return $arguments;
  231.     }
  232.     /**
  233.      * @return TypedReference|null A reference to the service matching the given type, if any
  234.      */
  235.     private function getAutowiredReference(TypedReference $reference$deprecationMessage)
  236.     {
  237.         $this->lastFailure null;
  238.         $type $reference->getType();
  239.         if ($type !== $this->container->normalizeId($reference) || ($this->container->has($type) && !$this->container->findDefinition($type)->isAbstract())) {
  240.             return $reference;
  241.         }
  242.         if (null === $this->types) {
  243.             $this->populateAvailableTypes($this->strictMode);
  244.         }
  245.         if (isset($this->definedTypes[$type])) {
  246.             return new TypedReference($this->types[$type], $type);
  247.         }
  248.         if (!$this->strictMode && isset($this->types[$type])) {
  249.             $message 'Autowiring services based on the types they implement is deprecated since Symfony 3.3 and won\'t be supported in version 4.0.';
  250.             if ($aliasSuggestion $this->getAliasesSuggestionForType($type $reference->getType(), $deprecationMessage)) {
  251.                 $message .= ' '.$aliasSuggestion;
  252.             } else {
  253.                 $message .= sprintf(' You should %s the "%s" service to "%s" instead.', isset($this->types[$this->types[$type]]) ? 'alias' 'rename (or alias)'$this->types[$type], $type);
  254.             }
  255.             @trigger_error($messageE_USER_DEPRECATED);
  256.             return new TypedReference($this->types[$type], $type);
  257.         }
  258.         if (!$reference->canBeAutoregistered() || isset($this->types[$type]) || isset($this->ambiguousServiceTypes[$type])) {
  259.             return null;
  260.         }
  261.         if (isset($this->autowired[$type])) {
  262.             return $this->autowired[$type] ? new TypedReference($this->autowired[$type], $type) : null;
  263.         }
  264.         if (!$this->strictMode) {
  265.             return $this->createAutowiredDefinition($type);
  266.         }
  267.         return null;
  268.     }
  269.     /**
  270.      * Populates the list of available types.
  271.      */
  272.     private function populateAvailableTypes($onlyAutowiringTypes false)
  273.     {
  274.         $this->types = [];
  275.         if (!$onlyAutowiringTypes) {
  276.             $this->ambiguousServiceTypes = [];
  277.         }
  278.         foreach ($this->container->getDefinitions() as $id => $definition) {
  279.             $this->populateAvailableType($id$definition$onlyAutowiringTypes);
  280.         }
  281.     }
  282.     /**
  283.      * Populates the list of available types for a given definition.
  284.      *
  285.      * @param string $id
  286.      */
  287.     private function populateAvailableType($idDefinition $definition$onlyAutowiringTypes)
  288.     {
  289.         // Never use abstract services
  290.         if ($definition->isAbstract()) {
  291.             return;
  292.         }
  293.         foreach ($definition->getAutowiringTypes(false) as $type) {
  294.             $this->definedTypes[$type] = true;
  295.             $this->types[$type] = $id;
  296.             unset($this->ambiguousServiceTypes[$type]);
  297.         }
  298.         if ($onlyAutowiringTypes) {
  299.             return;
  300.         }
  301.         if (preg_match('/^\d+_[^~]++~[._a-zA-Z\d]{7}$/'$id) || $definition->isDeprecated() || !$reflectionClass $this->container->getReflectionClass($definition->getClass(), false)) {
  302.             return;
  303.         }
  304.         foreach ($reflectionClass->getInterfaces() as $reflectionInterface) {
  305.             $this->set($reflectionInterface->name$id);
  306.         }
  307.         do {
  308.             $this->set($reflectionClass->name$id);
  309.         } while ($reflectionClass $reflectionClass->getParentClass());
  310.     }
  311.     /**
  312.      * Associates a type and a service id if applicable.
  313.      *
  314.      * @param string $type
  315.      * @param string $id
  316.      */
  317.     private function set($type$id)
  318.     {
  319.         if (isset($this->definedTypes[$type])) {
  320.             return;
  321.         }
  322.         // is this already a type/class that is known to match multiple services?
  323.         if (isset($this->ambiguousServiceTypes[$type])) {
  324.             $this->ambiguousServiceTypes[$type][] = $id;
  325.             return;
  326.         }
  327.         // check to make sure the type doesn't match multiple services
  328.         if (!isset($this->types[$type]) || $this->types[$type] === $id) {
  329.             $this->types[$type] = $id;
  330.             return;
  331.         }
  332.         // keep an array of all services matching this type
  333.         if (!isset($this->ambiguousServiceTypes[$type])) {
  334.             $this->ambiguousServiceTypes[$type] = [$this->types[$type]];
  335.             unset($this->types[$type]);
  336.         }
  337.         $this->ambiguousServiceTypes[$type][] = $id;
  338.     }
  339.     /**
  340.      * Registers a definition for the type if possible or throws an exception.
  341.      *
  342.      * @param string $type
  343.      *
  344.      * @return TypedReference|null A reference to the registered definition
  345.      */
  346.     private function createAutowiredDefinition($type)
  347.     {
  348.         if (!($typeHint $this->container->getReflectionClass($typefalse)) || !$typeHint->isInstantiable()) {
  349.             return null;
  350.         }
  351.         $currentId $this->currentId;
  352.         $this->currentId $type;
  353.         $this->autowired[$type] = $argumentId sprintf('autowired.%s'$type);
  354.         $argumentDefinition = new Definition($type);
  355.         $argumentDefinition->setPublic(false);
  356.         $argumentDefinition->setAutowired(true);
  357.         try {
  358.             $originalThrowSetting $this->throwOnAutowiringException;
  359.             $this->throwOnAutowiringException true;
  360.             $this->processValue($argumentDefinitiontrue);
  361.             $this->container->setDefinition($argumentId$argumentDefinition);
  362.         } catch (AutowiringFailedException $e) {
  363.             $this->autowired[$type] = false;
  364.             $this->lastFailure $e->getMessage();
  365.             $this->container->log($this$this->lastFailure);
  366.             return null;
  367.         } finally {
  368.             $this->throwOnAutowiringException $originalThrowSetting;
  369.             $this->currentId $currentId;
  370.         }
  371.         @trigger_error(sprintf('Relying on service auto-registration for type "%s" is deprecated since Symfony 3.4 and won\'t be supported in 4.0. Create a service named "%s" instead.'$type$type), E_USER_DEPRECATED);
  372.         $this->container->log($thissprintf('Type "%s" has been auto-registered for service "%s".'$type$this->currentId));
  373.         return new TypedReference($argumentId$type);
  374.     }
  375.     private function createTypeNotFoundMessage(TypedReference $reference$label)
  376.     {
  377.         $trackResources $this->container->isTrackingResources();
  378.         $this->container->setResourceTracking(false);
  379.         try {
  380.             if ($r $this->container->getReflectionClass($type $reference->getType(), false)) {
  381.                 $alternatives $this->createTypeAlternatives($reference);
  382.             }
  383.         } finally {
  384.             $this->container->setResourceTracking($trackResources);
  385.         }
  386.         if (!$r) {
  387.             // either $type does not exist or a parent class does not exist
  388.             try {
  389.                 $resource = new ClassExistenceResource($typefalse);
  390.                 // isFresh() will explode ONLY if a parent class/trait does not exist
  391.                 $resource->isFresh(0);
  392.                 $parentMsg false;
  393.             } catch (\ReflectionException $e) {
  394.                 $parentMsg $e->getMessage();
  395.             }
  396.             $message sprintf('has type "%s" but this class %s.'$type$parentMsg sprintf('is missing a parent class (%s)'$parentMsg) : 'was not found');
  397.         } else {
  398.             $message $this->container->has($type) ? 'this service is abstract' 'no such service exists';
  399.             $message sprintf('references %s "%s" but %s.%s'$r->isInterface() ? 'interface' 'class'$type$message$alternatives);
  400.             if ($r->isInterface() && !$alternatives) {
  401.                 $message .= ' Did you create a class that implements this interface?';
  402.             }
  403.         }
  404.         $message sprintf('Cannot autowire service "%s": %s %s'$this->currentId$label$message);
  405.         if (null !== $this->lastFailure) {
  406.             $message $this->lastFailure."\n".$message;
  407.             $this->lastFailure null;
  408.         }
  409.         return $message;
  410.     }
  411.     private function createTypeAlternatives(TypedReference $reference)
  412.     {
  413.         // try suggesting available aliases first
  414.         if ($message $this->getAliasesSuggestionForType($type $reference->getType())) {
  415.             return ' '.$message;
  416.         }
  417.         if (null === $this->ambiguousServiceTypes) {
  418.             $this->populateAvailableTypes();
  419.         }
  420.         if (isset($this->ambiguousServiceTypes[$type])) {
  421.             $message sprintf('one of these existing services: "%s"'implode('", "'$this->ambiguousServiceTypes[$type]));
  422.         } elseif (isset($this->types[$type])) {
  423.             $message sprintf('the existing "%s" service'$this->types[$type]);
  424.         } elseif ($reference->getRequiringClass() && !$reference->canBeAutoregistered() && !$this->strictMode) {
  425.             return ' It cannot be auto-registered because it is from a different root namespace.';
  426.         } else {
  427.             return '';
  428.         }
  429.         return sprintf(' You should maybe alias this %s to %s.'class_exists($typefalse) ? 'class' 'interface'$message);
  430.     }
  431.     /**
  432.      * @deprecated since version 3.3, to be removed in 4.0.
  433.      */
  434.     private static function getResourceMetadataForMethod(\ReflectionMethod $method)
  435.     {
  436.         $methodArgumentsMetadata = [];
  437.         foreach ($method->getParameters() as $parameter) {
  438.             try {
  439.                 $class $parameter->getClass();
  440.             } catch (\ReflectionException $e) {
  441.                 // type-hint is against a non-existent class
  442.                 $class false;
  443.             }
  444.             $isVariadic method_exists($parameter'isVariadic') && $parameter->isVariadic();
  445.             $methodArgumentsMetadata[] = [
  446.                 'class' => $class,
  447.                 'isOptional' => $parameter->isOptional(),
  448.                 'defaultValue' => ($parameter->isOptional() && !$isVariadic) ? $parameter->getDefaultValue() : null,
  449.             ];
  450.         }
  451.         return $methodArgumentsMetadata;
  452.     }
  453.     private function getAliasesSuggestionForType($type$extraContext null)
  454.     {
  455.         $aliases = [];
  456.         foreach (class_parents($type) + class_implements($type) as $parent) {
  457.             if ($this->container->has($parent) && !$this->container->findDefinition($parent)->isAbstract()) {
  458.                 $aliases[] = $parent;
  459.             }
  460.         }
  461.         $extraContext $extraContext ' '.$extraContext '';
  462.         if ($len = \count($aliases)) {
  463.             $message sprintf('Try changing the type-hint%s to one of its parents: '$extraContext);
  464.             for ($i 0, --$len$i $len; ++$i) {
  465.                 $message .= sprintf('%s "%s", 'class_exists($aliases[$i], false) ? 'class' 'interface'$aliases[$i]);
  466.             }
  467.             $message .= sprintf('or %s "%s".'class_exists($aliases[$i], false) ? 'class' 'interface'$aliases[$i]);
  468.             return $message;
  469.         }
  470.         if ($aliases) {
  471.             return sprintf('Try changing the type-hint%s to "%s" instead.'$extraContext$aliases[0]);
  472.         }
  473.         return null;
  474.     }
  475. }