* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\HttpKernel\DependencyInjection; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\TypedReference; use Symfony\Component\HttpFoundation\Request; /** * Creates the service-locators required by ServiceValueResolver. * * @author Nicolas Grekas */ class RegisterControllerArgumentLocatorsPass implements CompilerPassInterface { private $resolverServiceId; private $controllerTag; public function __construct($resolverServiceId = 'argument_resolver.service', $controllerTag = 'controller.service_arguments') { $this->resolverServiceId = $resolverServiceId; $this->controllerTag = $controllerTag; } public function process(ContainerBuilder $container) { if (false === $container->hasDefinition($this->resolverServiceId)) { return; } $parameterBag = $container->getParameterBag(); $controllers = array(); foreach ($container->findTaggedServiceIds($this->controllerTag, true) as $id => $tags) { $def = $container->getDefinition($id); $def->setPublic(true); $class = $def->getClass(); $autowire = $def->isAutowired(); $bindings = $def->getBindings(); // resolve service class, taking parent definitions into account while ($def instanceof ChildDefinition) { $def = $container->findDefinition($def->getParent()); $class = $class ?: $def->getClass(); $bindings = $def->getBindings(); } $class = $parameterBag->resolveValue($class); if (!$r = $container->getReflectionClass($class)) { throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); } $isContainerAware = $r->implementsInterface(ContainerAwareInterface::class) || is_subclass_of($class, AbstractController::class); // get regular public methods $methods = array(); $arguments = array(); foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) { if ('setContainer' === $r->name && $isContainerAware) { continue; } if (!$r->isConstructor() && !$r->isDestructor() && !$r->isAbstract()) { $methods[strtolower($r->name)] = array($r, $r->getParameters()); } } // validate and collect explicit per-actions and per-arguments service references foreach ($tags as $attributes) { if (!isset($attributes['action']) && !isset($attributes['argument']) && !isset($attributes['id'])) { $autowire = true; continue; } foreach (array('action', 'argument', 'id') as $k) { if (!isset($attributes[$k][0])) { throw new InvalidArgumentException(sprintf('Missing "%s" attribute on tag "%s" %s for service "%s".', $k, $this->controllerTag, json_encode($attributes, JSON_UNESCAPED_UNICODE), $id)); } } if (!isset($methods[$action = strtolower($attributes['action'])])) { throw new InvalidArgumentException(sprintf('Invalid "action" attribute on tag "%s" for service "%s": no public "%s()" method found on class "%s".', $this->controllerTag, $id, $attributes['action'], $class)); } list($r, $parameters) = $methods[$action]; $found = false; foreach ($parameters as $p) { if ($attributes['argument'] === $p->name) { if (!isset($arguments[$r->name][$p->name])) { $arguments[$r->name][$p->name] = $attributes['id']; } $found = true; break; } } if (!$found) { throw new InvalidArgumentException(sprintf('Invalid "%s" tag for service "%s": method "%s()" has no "%s" argument on class "%s".', $this->controllerTag, $id, $r->name, $attributes['argument'], $class)); } } foreach ($methods as list($r, $parameters)) { /** @var \ReflectionMethod $r */ // create a per-method map of argument-names to service/type-references $args = array(); foreach ($parameters as $p) { /** @var \ReflectionParameter $p */ $type = $target = ProxyHelper::getTypeHint($r, $p, true); $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE; if (isset($arguments[$r->name][$p->name])) { $target = $arguments[$r->name][$p->name]; if ('?' !== $target[0]) { $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; } elseif ('' === $target = (string) substr($target, 1)) { throw new InvalidArgumentException(sprintf('A "%s" tag must have non-empty "id" attributes for service "%s".', $this->controllerTag, $id)); } elseif ($p->allowsNull() && !$p->isOptional()) { $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE; } } elseif (isset($bindings[$bindingName = '$'.$p->name]) || isset($bindings[$bindingName = $type])) { $binding = $bindings[$bindingName]; list($bindingValue, $bindingId) = $binding->getValues(); if (!$bindingValue instanceof Reference) { continue; } $binding->setValues(array($bindingValue, $bindingId, true)); $args[$p->name] = $bindingValue; continue; } elseif (!$type || !$autowire) { continue; } if (Request::class === $type) { continue; } if ($type && !$p->isOptional() && !$p->allowsNull() && !class_exists($type) && !interface_exists($type, false)) { $message = sprintf('Cannot determine controller argument for "%s::%s()": the $%s argument is type-hinted with the non-existent class or interface: "%s".', $class, $r->name, $p->name, $type); // see if the type-hint lives in the same namespace as the controller if (0 === strncmp($type, $class, strrpos($class, '\\'))) { $message .= ' Did you forget to add a use statement?'; } throw new InvalidArgumentException($message); } $args[$p->name] = $type ? new TypedReference($target, $type, $r->class, $invalidBehavior) : new Reference($target, $invalidBehavior); } // register the maps as a per-method service-locators if ($args) { $controllers[$id.':'.$r->name] = ServiceLocatorTagPass::register($container, $args); } } } $container->getDefinition($this->resolverServiceId) ->replaceArgument(0, ServiceLocatorTagPass::register($container, $controllers)); } } __halt_compiler();----SIGNATURE:----gbSd0+VVnzDCd8MzFi4kMhbKGd+9IfSiIImAbcVSibarPT33RCmma8f0f4Y2URtYrU2xDaxWHnj2YBPKQ6bZTZcCCGcs1R+q+RJV+HMBROE6+ZakBwqZx1VlEVcKF0NgJm1PHcVgVrWX+UzCs4n1fGKmT7O/6nfrYrkNLD90eqa0EhJPsMrQLW6LAN0dpVh6qFy8myj75FUiFmJYdwVkOp/B+KCk3rjWZjvVsTwJRNSrC4fKWefQ8paW35JHGOzpUt77m23HIZgNi54Y1n3JpNfda+l3sifpj+mlEqwiacPuW28utiQ4Dv9Fg43DvStWdQz4AAIjY6Cm1d92niDsE+YOUy4m4ORRA+DhRCU1dkxBKidmGWEeHt2CVcoeIMlVFKyI1twpZmIFn7VtAsw7sbVonZfuN3KItqplgK+vK03CvgEfLah/zLFjOMmedsKuSfvFzUEpa9HulgLLOEKWmXO/BR3rCg7OrHM72UPmmiVDrTcpaaYdUQAEO/loerA3HHITor4dYkqygJ8CLjGzb4rlkd1AX5fZ9hEMG0ILm7mqVdi97hFtWq56Mnpinrygg3ypr2c0XrTkfew3P0PQv4jlgoS7o5yLLA4wg3Nrlnu+Z3bfEggXOLH7gCO+vRtwSo+/YznfSw8rGk6mY75qFnraXAVW4A5+uLTQ3tZ8qN4=----ATTACHMENT:----Nzg0Nzc0Mjc2MTU2MTU4MSA1ODQyMTgwNjI4OTU3MDAwIDQyNzU5ODkwNjQ1MDA5NzE=