Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 /*
|
Chris@14
|
4 * This file is part of the Symfony package.
|
Chris@14
|
5 *
|
Chris@14
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@14
|
7 *
|
Chris@14
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
9 * file that was distributed with this source code.
|
Chris@14
|
10 */
|
Chris@14
|
11
|
Chris@14
|
12 namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver;
|
Chris@14
|
13
|
Chris@14
|
14 use Psr\Container\ContainerInterface;
|
Chris@14
|
15 use Symfony\Component\HttpFoundation\Request;
|
Chris@14
|
16 use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
|
Chris@14
|
17 use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
|
Chris@14
|
18
|
Chris@14
|
19 /**
|
Chris@14
|
20 * Yields a service keyed by _controller and argument name.
|
Chris@14
|
21 *
|
Chris@14
|
22 * @author Nicolas Grekas <p@tchwork.com>
|
Chris@14
|
23 */
|
Chris@14
|
24 final class ServiceValueResolver implements ArgumentValueResolverInterface
|
Chris@14
|
25 {
|
Chris@14
|
26 private $container;
|
Chris@14
|
27
|
Chris@14
|
28 public function __construct(ContainerInterface $container)
|
Chris@14
|
29 {
|
Chris@14
|
30 $this->container = $container;
|
Chris@14
|
31 }
|
Chris@14
|
32
|
Chris@14
|
33 /**
|
Chris@14
|
34 * {@inheritdoc}
|
Chris@14
|
35 */
|
Chris@14
|
36 public function supports(Request $request, ArgumentMetadata $argument)
|
Chris@14
|
37 {
|
Chris@14
|
38 $controller = $request->attributes->get('_controller');
|
Chris@14
|
39
|
Chris@14
|
40 if (\is_array($controller) && \is_callable($controller, true) && \is_string($controller[0])) {
|
Chris@14
|
41 $controller = $controller[0].'::'.$controller[1];
|
Chris@16
|
42 } elseif (!\is_string($controller) || '' === $controller) {
|
Chris@16
|
43 return false;
|
Chris@14
|
44 }
|
Chris@14
|
45
|
Chris@16
|
46 if ('\\' === $controller[0]) {
|
Chris@16
|
47 $controller = ltrim($controller, '\\');
|
Chris@16
|
48 }
|
Chris@16
|
49
|
Chris@16
|
50 return $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
|
Chris@14
|
51 }
|
Chris@14
|
52
|
Chris@14
|
53 /**
|
Chris@14
|
54 * {@inheritdoc}
|
Chris@14
|
55 */
|
Chris@14
|
56 public function resolve(Request $request, ArgumentMetadata $argument)
|
Chris@14
|
57 {
|
Chris@14
|
58 if (\is_array($controller = $request->attributes->get('_controller'))) {
|
Chris@14
|
59 $controller = $controller[0].'::'.$controller[1];
|
Chris@14
|
60 }
|
Chris@14
|
61
|
Chris@16
|
62 if ('\\' === $controller[0]) {
|
Chris@16
|
63 $controller = ltrim($controller, '\\');
|
Chris@16
|
64 }
|
Chris@16
|
65
|
Chris@14
|
66 yield $this->container->get($controller)->get($argument->getName());
|
Chris@14
|
67 }
|
Chris@14
|
68 }
|