[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\HttpKernel\DependencyInjection; 13 14 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 15 use Symfony\Component\DependencyInjection\ChildDefinition; 16 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 17 use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; 18 use Symfony\Component\DependencyInjection\ContainerAwareInterface; 19 use Symfony\Component\DependencyInjection\ContainerBuilder; 20 use Symfony\Component\DependencyInjection\ContainerInterface; 21 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; 22 use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper; 23 use Symfony\Component\DependencyInjection\Reference; 24 use Symfony\Component\DependencyInjection\TypedReference; 25 use Symfony\Component\HttpFoundation\Request; 26 27 /** 28 * Creates the service-locators required by ServiceValueResolver. 29 * 30 * @author Nicolas Grekas <p@tchwork.com> 31 */ 32 class RegisterControllerArgumentLocatorsPass implements CompilerPassInterface 33 { 34 private $resolverServiceId; 35 private $controllerTag; 36 37 public function __construct($resolverServiceId = 'argument_resolver.service', $controllerTag = 'controller.service_arguments') 38 { 39 $this->resolverServiceId = $resolverServiceId; 40 $this->controllerTag = $controllerTag; 41 } 42 43 public function process(ContainerBuilder $container) 44 { 45 if (false === $container->hasDefinition($this->resolverServiceId)) { 46 return; 47 } 48 49 $parameterBag = $container->getParameterBag(); 50 $controllers = []; 51 52 foreach ($container->findTaggedServiceIds($this->controllerTag, true) as $id => $tags) { 53 $def = $container->getDefinition($id); 54 $def->setPublic(true); 55 $class = $def->getClass(); 56 $autowire = $def->isAutowired(); 57 $bindings = $def->getBindings(); 58 59 // resolve service class, taking parent definitions into account 60 while ($def instanceof ChildDefinition) { 61 $def = $container->findDefinition($def->getParent()); 62 $class = $class ?: $def->getClass(); 63 $bindings += $def->getBindings(); 64 } 65 $class = $parameterBag->resolveValue($class); 66 67 if (!$r = $container->getReflectionClass($class)) { 68 throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); 69 } 70 $isContainerAware = $r->implementsInterface(ContainerAwareInterface::class) || is_subclass_of($class, AbstractController::class); 71 72 // get regular public methods 73 $methods = []; 74 $arguments = []; 75 foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) { 76 if ('setContainer' === $r->name && $isContainerAware) { 77 continue; 78 } 79 if (!$r->isConstructor() && !$r->isDestructor() && !$r->isAbstract()) { 80 $methods[strtolower($r->name)] = [$r, $r->getParameters()]; 81 } 82 } 83 84 // validate and collect explicit per-actions and per-arguments service references 85 foreach ($tags as $attributes) { 86 if (!isset($attributes['action']) && !isset($attributes['argument']) && !isset($attributes['id'])) { 87 $autowire = true; 88 continue; 89 } 90 foreach (['action', 'argument', 'id'] as $k) { 91 if (!isset($attributes[$k][0])) { 92 throw new InvalidArgumentException(sprintf('Missing "%s" attribute on tag "%s" %s for service "%s".', $k, $this->controllerTag, json_encode($attributes, \JSON_UNESCAPED_UNICODE), $id)); 93 } 94 } 95 if (!isset($methods[$action = strtolower($attributes['action'])])) { 96 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)); 97 } 98 list($r, $parameters) = $methods[$action]; 99 $found = false; 100 101 foreach ($parameters as $p) { 102 if ($attributes['argument'] === $p->name) { 103 if (!isset($arguments[$r->name][$p->name])) { 104 $arguments[$r->name][$p->name] = $attributes['id']; 105 } 106 $found = true; 107 break; 108 } 109 } 110 111 if (!$found) { 112 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)); 113 } 114 } 115 116 foreach ($methods as list($r, $parameters)) { 117 /** @var \ReflectionMethod $r */ 118 119 // create a per-method map of argument-names to service/type-references 120 $args = []; 121 foreach ($parameters as $p) { 122 /** @var \ReflectionParameter $p */ 123 $type = $target = ProxyHelper::getTypeHint($r, $p, true); 124 $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE; 125 126 if (isset($arguments[$r->name][$p->name])) { 127 $target = $arguments[$r->name][$p->name]; 128 if ('?' !== $target[0]) { 129 $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; 130 } elseif ('' === $target = (string) substr($target, 1)) { 131 throw new InvalidArgumentException(sprintf('A "%s" tag must have non-empty "id" attributes for service "%s".', $this->controllerTag, $id)); 132 } elseif ($p->allowsNull() && !$p->isOptional()) { 133 $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE; 134 } 135 } elseif (isset($bindings[$bindingName = '$'.$p->name]) || isset($bindings[$bindingName = $type])) { 136 $binding = $bindings[$bindingName]; 137 138 list($bindingValue, $bindingId) = $binding->getValues(); 139 140 if (!$bindingValue instanceof Reference) { 141 continue; 142 } 143 144 $binding->setValues([$bindingValue, $bindingId, true]); 145 $args[$p->name] = $bindingValue; 146 147 continue; 148 } elseif (!$type || !$autowire) { 149 continue; 150 } 151 152 if (Request::class === $type) { 153 continue; 154 } 155 156 if ($type && !$p->isOptional() && !$p->allowsNull() && !class_exists($type) && !interface_exists($type, false)) { 157 $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); 158 159 // see if the type-hint lives in the same namespace as the controller 160 if (0 === strncmp($type, $class, strrpos($class, '\\'))) { 161 $message .= ' Did you forget to add a use statement?'; 162 } 163 164 throw new InvalidArgumentException($message); 165 } 166 167 $args[$p->name] = $type ? new TypedReference($target, $type, $r->class, $invalidBehavior) : new Reference($target, $invalidBehavior); 168 } 169 // register the maps as a per-method service-locators 170 if ($args) { 171 $controllers[$id.':'.$r->name] = ServiceLocatorTagPass::register($container, $args); 172 } 173 } 174 } 175 176 $container->getDefinition($this->resolverServiceId) 177 ->replaceArgument(0, ServiceLocatorTagPass::register($container, $controllers)); 178 } 179 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |