|
| 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\Bundle\FrameworkBundle\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; |
| 15 | +use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; |
| 16 | +use Symfony\Component\DependencyInjection\AutowirableReference; |
| 17 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 18 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 19 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 20 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 21 | +use Symfony\Component\DependencyInjection\LazyProxy\InheritanceProxyHelper; |
| 22 | +use Symfony\Component\DependencyInjection\Reference; |
| 23 | +use Symfony\Component\DependencyInjection\ServiceLocator; |
| 24 | + |
| 25 | +/** |
| 26 | + * Creates the controller arguments service locator required by "argument_resolver.service". |
| 27 | + * |
| 28 | + * @author Nicolas Grekas <p@tchwork.com> |
| 29 | + * |
| 30 | + * @experimental in version 3.3 |
| 31 | + */ |
| 32 | +class RoutingControllerPass implements CompilerPassInterface |
| 33 | +{ |
| 34 | + public function process(ContainerBuilder $container) |
| 35 | + { |
| 36 | + if (false === $container->hasDefinition('argument_resolver.service')) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + $serviceResolver = $container->getDefinition('argument_resolver.service'); |
| 41 | + $parameterBag = $container->getParameterBag(); |
| 42 | + $controllers = array(); |
| 43 | + |
| 44 | + foreach ($container->findTaggedServiceIds('routing.controller') as $id => $tags) { |
| 45 | + $def = $container->getDefinition($id); |
| 46 | + $class = $def->getClass(); |
| 47 | + |
| 48 | + if ($def->isAbstract()) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + while (!$class && $def instanceof ChildDefinition) { |
| 53 | + $def = $container->findDefinition($def->getParent()); |
| 54 | + $class = $def->getClass(); |
| 55 | + } |
| 56 | + $class = $parameterBag->resolveValue($class); |
| 57 | + |
| 58 | + if (!$r = $container->getReflectionClass($class)) { |
| 59 | + throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); |
| 60 | + } |
| 61 | + |
| 62 | + $methods = array(); |
| 63 | + foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) { |
| 64 | + $methods[strtolower($r->name)] = $r; |
| 65 | + } |
| 66 | + |
| 67 | + $actions = array(); |
| 68 | + foreach ($tags as $attributes) { |
| 69 | + if (!isset($attributes['action'])) { |
| 70 | + throw new InvalidArgumentException(sprintf('Service "%s" must define the "action" attribute on "routing.controller" tags.', $id)); |
| 71 | + } |
| 72 | + $action = strtolower($attributes['action']); |
| 73 | + |
| 74 | + if (false !== strpos($action, '*')) { |
| 75 | + $regex = '/^'.str_replace('\*', '.*', preg_quote($action, '/')).'$/'; |
| 76 | + $found = false; |
| 77 | + |
| 78 | + foreach ($methods as $name => $r) { |
| 79 | + if (preg_match($regex, $name)) { |
| 80 | + $actions[$methods[$action]->name] = $methods[$action]; |
| 81 | + $found = true; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (!$found) { |
| 86 | + $container->log($this, sprintf('No "action" found for service "%s": class "%s" has no "%s" methods.', $id, $class, $attributes['action'])); |
| 87 | + } |
| 88 | + } elseif (isset($methods[$action])) { |
| 89 | + $actions[$methods[$action]->name] = $methods[$action]; |
| 90 | + } else { |
| 91 | + throw new InvalidArgumentException(sprintf('Invalid "action" for service "%s": no "%s" method found on class "%s".', $id, $class, $attributes['action'])); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (!$actions) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + foreach ($actions as $name => $r) { |
| 100 | + $args = array(); |
| 101 | + foreach ($r->getParameters() as $p) { |
| 102 | + if ($type = InheritanceProxyHelper::getTypeHint($r, $p, true)) { |
| 103 | + $args[$p->name] = new ServiceClosureArgument(new AutowirableReference($type, $type, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, false)); |
| 104 | + } |
| 105 | + } |
| 106 | + if ($args) { |
| 107 | + $argsId = sprintf('arguments.%s:%s', $id, $name); |
| 108 | + $container->register($argsId, ServiceLocator::class)->addArgument($args)->setPublic(false); |
| 109 | + $controllers[$id.':'.$name] = new Reference($argsId); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + $serviceResolver->replaceArgument(0, new ServiceLocatorArgument($controllers)); |
| 115 | + } |
| 116 | +} |