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@17
|
50 if (!$this->container->has($controller) && false !== $i = strrpos($controller, ':')) {
|
Chris@17
|
51 $controller = substr($controller, 0, $i).strtolower(substr($controller, $i));
|
Chris@17
|
52 }
|
Chris@17
|
53
|
Chris@16
|
54 return $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
|
Chris@14
|
55 }
|
Chris@14
|
56
|
Chris@14
|
57 /**
|
Chris@14
|
58 * {@inheritdoc}
|
Chris@14
|
59 */
|
Chris@14
|
60 public function resolve(Request $request, ArgumentMetadata $argument)
|
Chris@14
|
61 {
|
Chris@14
|
62 if (\is_array($controller = $request->attributes->get('_controller'))) {
|
Chris@14
|
63 $controller = $controller[0].'::'.$controller[1];
|
Chris@14
|
64 }
|
Chris@14
|
65
|
Chris@16
|
66 if ('\\' === $controller[0]) {
|
Chris@16
|
67 $controller = ltrim($controller, '\\');
|
Chris@16
|
68 }
|
Chris@16
|
69
|
Chris@17
|
70 if (!$this->container->has($controller)) {
|
Chris@17
|
71 $i = strrpos($controller, ':');
|
Chris@17
|
72 $controller = substr($controller, 0, $i).strtolower(substr($controller, $i));
|
Chris@17
|
73 }
|
Chris@17
|
74
|
Chris@14
|
75 yield $this->container->get($controller)->get($argument->getName());
|
Chris@14
|
76 }
|
Chris@14
|
77 }
|