|
12 | 12 | namespace Symfony\Component\HttpKernel\Controller;
|
13 | 13 |
|
14 | 14 | use Psr\Log\LoggerInterface;
|
| 15 | +use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; |
| 16 | +use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface; |
15 | 17 | use Symfony\Component\HttpFoundation\Request;
|
16 | 18 |
|
17 | 19 | /**
|
|
22 | 24 | * the controller method arguments.
|
23 | 25 | *
|
24 | 26 | * @author Fabien Potencier <fabien@symfony.com>
|
| 27 | + * @author Kévin Dunglas <dunglas@gmail.com> |
25 | 28 | *
|
26 | 29 | * @api
|
27 | 30 | */
|
28 | 31 | class ControllerResolver implements ControllerResolverInterface
|
29 | 32 | {
|
30 | 33 | private $logger;
|
| 34 | + private $httpMessageFactory; |
31
35 |
|
32 | 36 | /**
|
33 | 37 | * Constructor.
|
34 | 38 | *
|
35 | 39 | * @param LoggerInterface $logger A LoggerInterface instance
|
36 | 40 | */
|
37 |
| - public function __construct(LoggerInterface $logger = null) |
| 41 | + public function __construct(LoggerInterface $logger = null, HttpMessageFactoryInterface $httpMessageFactory = null) |
38 | 42 | {
|
39 | 43 | $this->logger = $logger;
|
| 44 | + |
| 45 | + if (null === $httpMessageFactory && class_exists('Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory')) { |
| 46 | + $this->httpMessageFactory = new DiactorosFactory(); |
| 47 | + } |
40 | 48 | }
|
41 | 49 |
|
42 | 50 | /**
|
@@ -112,21 +120,43 @@ protected function doGetArguments(Request $request, $controller, array $paramete
|
112 | 120 | foreach ($parameters as $param) {
|
113 | 121 | if (array_key_exists($param->name, $attributes)) {
|
114 | 122 | $arguments[] = $attributes[$param->name];
|
115 |
| - } elseif ($param->getClass() && $param->getClass()->isInstance($request)) { |
116 |
| - $arguments[] = $request; |
117 |
| - } elseif ($param->isDefaultValueAvailable()) { |
118 |
| - $arguments[] = $param->getDefaultValue(); |
119 |
| - } else { |
120 |
| - if (is_array($controller)) { |
121 |
| - $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]); |
122 |
| - } elseif (is_object($controller)) { |
123 |
| - $repr = get_class($controller); |
124 |
| - } else { |
125 |
| - $repr = $controller; |
| 123 | + |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + if ($class = $param->getClass()) { |
| 128 | + if ($class->isInstance($request)) { |
| 129 | + $arguments[] = $request; |
| 130 | + |
| 131 | + continue; |
126 | 132 | }
|
127 | 133 |
|
128 |
| - throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $repr, $param->name)); |
| 134 | + if ($class->implementsInterface('Psr\Http\Message\ServerRequestInterface')) { |
| 135 | + if (null === $this->httpMessageFactory) { |
| 136 | + throw new \RuntimeException('The PSR-7 Bridge must be installed to inject HttpMessage in controllers.'); |
| 137 | + } |
| 138 | + |
| 139 | + $arguments[] = $this->httpMessageFactory->createRequest($request); |
| 140 | + |
| 141 | + continue; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if ($param->isDefaultValueAvailable()) { |
| 146 | + $arguments[] = $param->getDefaultValue(); |
| 147 | + |
| 148 | + continue; |
129 | 149 | }
|
| 150 | + |
| 151 | + if (is_array($controller)) { |
| 152 | + $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]); |
| 153 | + } elseif (is_object($controller)) { |
| 154 | + $repr = get_class($controller); |
| 155 | + } else { |
| 156 | + $repr = $controller; |
| 157 | + } |
| 158 | + |
| 159 | + throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $repr, $param->name)); |
130 | 160 | }
|
131 | 161 |
|
132 | 162 | return $arguments;
|
|
0 commit comments