4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver;
14 use Psr\Container\ContainerInterface;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
17 use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
20 * Yields a service keyed by _controller and argument name.
22 * @author Nicolas Grekas <p@tchwork.com>
24 final class ServiceValueResolver implements ArgumentValueResolverInterface
28 public function __construct(ContainerInterface $container)
30 $this->container = $container;
36 public function supports(Request $request, ArgumentMetadata $argument)
38 $controller = $request->attributes->get('_controller');
40 if (\is_array($controller) && \is_callable($controller, true) && \is_string($controller[0])) {
41 $controller = $controller[0].'::'.$controller[1];
42 } elseif (!\is_string($controller) || '' === $controller) {
46 if ('\\' === $controller[0]) {
47 $controller = ltrim($controller, '\\');
50 if (!$this->container->has($controller) && false !== $i = strrpos($controller, ':')) {
51 $controller = substr($controller, 0, $i).strtolower(substr($controller, $i));
54 return $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
60 public function resolve(Request $request, ArgumentMetadata $argument)
62 if (\is_array($controller = $request->attributes->get('_controller'))) {
63 $controller = $controller[0].'::'.$controller[1];
66 if ('\\' === $controller[0]) {
67 $controller = ltrim($controller, '\\');
70 if (!$this->container->has($controller)) {
71 $i = strrpos($controller, ':');
72 $controller = substr($controller, 0, $i).strtolower(substr($controller, $i));
75 yield $this->container->get($controller)->get($argument->getName());