3294ec862ee30b5302a0cdce908b63032b9650c2
[yaffs-website] / vendor / symfony / http-kernel / Controller / ArgumentResolver / ServiceValueResolver.php
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\Controller\ArgumentResolver;
13
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;
18
19 /**
20  * Yields a service keyed by _controller and argument name.
21  *
22  * @author Nicolas Grekas <p@tchwork.com>
23  */
24 final class ServiceValueResolver implements ArgumentValueResolverInterface
25 {
26     private $container;
27
28     public function __construct(ContainerInterface $container)
29     {
30         $this->container = $container;
31     }
32
33     /**
34      * {@inheritdoc}
35      */
36     public function supports(Request $request, ArgumentMetadata $argument)
37     {
38         $controller = $request->attributes->get('_controller');
39
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) {
43             return false;
44         }
45
46         if ('\\' === $controller[0]) {
47             $controller = ltrim($controller, '\\');
48         }
49
50         if (!$this->container->has($controller) && false !== $i = strrpos($controller, ':')) {
51             $controller = substr($controller, 0, $i).strtolower(substr($controller, $i));
52         }
53
54         return $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     public function resolve(Request $request, ArgumentMetadata $argument)
61     {
62         if (\is_array($controller = $request->attributes->get('_controller'))) {
63             $controller = $controller[0].'::'.$controller[1];
64         }
65
66         if ('\\' === $controller[0]) {
67             $controller = ltrim($controller, '\\');
68         }
69
70         if (!$this->container->has($controller)) {
71             $i = strrpos($controller, ':');
72             $controller = substr($controller, 0, $i).strtolower(substr($controller, $i));
73         }
74
75         yield $this->container->get($controller)->get($argument->getName());
76     }
77 }