| 148 |
lars |
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\DependencyInjection\Exception\RuntimeException;
|
|
|
16 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
17 |
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
|
|
|
18 |
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
|
|
|
19 |
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Yields a service keyed by _controller and argument name.
|
|
|
23 |
*
|
|
|
24 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
25 |
*/
|
|
|
26 |
final class ServiceValueResolver implements ArgumentValueResolverInterface, ValueResolverInterface
|
|
|
27 |
{
|
|
|
28 |
private ContainerInterface $container;
|
|
|
29 |
|
|
|
30 |
public function __construct(ContainerInterface $container)
|
|
|
31 |
{
|
|
|
32 |
$this->container = $container;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @deprecated since Symfony 6.2, use resolve() instead
|
|
|
37 |
*/
|
|
|
38 |
public function supports(Request $request, ArgumentMetadata $argument): bool
|
|
|
39 |
{
|
|
|
40 |
@trigger_deprecation('symfony/http-kernel', '6.2', 'The "%s()" method is deprecated, use "resolve()" instead.', __METHOD__);
|
|
|
41 |
|
|
|
42 |
$controller = $request->attributes->get('_controller');
|
|
|
43 |
|
|
|
44 |
if (\is_array($controller) && \is_callable($controller, true) && \is_string($controller[0])) {
|
|
|
45 |
$controller = $controller[0].'::'.$controller[1];
|
|
|
46 |
} elseif (!\is_string($controller) || '' === $controller) {
|
|
|
47 |
return false;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
if ('\\' === $controller[0]) {
|
|
|
51 |
$controller = ltrim($controller, '\\');
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
if (!$this->container->has($controller) && false !== $i = strrpos($controller, ':')) {
|
|
|
55 |
$controller = substr($controller, 0, $i).strtolower(substr($controller, $i));
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
return $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public function resolve(Request $request, ArgumentMetadata $argument): array
|
|
|
62 |
{
|
|
|
63 |
$controller = $request->attributes->get('_controller');
|
|
|
64 |
|
|
|
65 |
if (\is_array($controller) && \is_callable($controller, true) && \is_string($controller[0])) {
|
|
|
66 |
$controller = $controller[0].'::'.$controller[1];
|
|
|
67 |
} elseif (!\is_string($controller) || '' === $controller) {
|
|
|
68 |
return [];
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
if ('\\' === $controller[0]) {
|
|
|
72 |
$controller = ltrim($controller, '\\');
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
if (!$this->container->has($controller) && false !== $i = strrpos($controller, ':')) {
|
|
|
76 |
$controller = substr($controller, 0, $i).strtolower(substr($controller, $i));
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
if (!$this->container->has($controller) || !$this->container->get($controller)->has($argument->getName())) {
|
|
|
80 |
return [];
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
try {
|
|
|
84 |
return [$this->container->get($controller)->get($argument->getName())];
|
|
|
85 |
} catch (RuntimeException $e) {
|
|
|
86 |
$what = sprintf('argument $%s of "%s()"', $argument->getName(), $controller);
|
|
|
87 |
$message = preg_replace('/service "\.service_locator\.[^"]++"/', $what, $e->getMessage());
|
|
|
88 |
|
|
|
89 |
if ($e->getMessage() === $message) {
|
|
|
90 |
$message = sprintf('Cannot resolve %s: %s', $what, $message);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
$r = new \ReflectionProperty($e, 'message');
|
|
|
94 |
$r->setValue($e, $message);
|
|
|
95 |
|
|
|
96 |
throw $e;
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
}
|