Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpKernel\Controller;
|
Chris@0
|
13
|
Chris@0
|
14 use Psr\Log\LoggerInterface;
|
Chris@0
|
15 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * This implementation uses the '_controller' request attribute to determine
|
Chris@0
|
19 * the controller to execute and uses the request attributes to determine
|
Chris@0
|
20 * the controller method arguments.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
23 */
|
Chris@0
|
24 class ControllerResolver implements ArgumentResolverInterface, ControllerResolverInterface
|
Chris@0
|
25 {
|
Chris@0
|
26 private $logger;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * If the ...$arg functionality is available.
|
Chris@0
|
30 *
|
Chris@0
|
31 * Requires at least PHP 5.6.0 or HHVM 3.9.1
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var bool
|
Chris@0
|
34 */
|
Chris@0
|
35 private $supportsVariadic;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * If scalar types exists.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @var bool
|
Chris@0
|
41 */
|
Chris@0
|
42 private $supportsScalarTypes;
|
Chris@0
|
43
|
Chris@0
|
44 public function __construct(LoggerInterface $logger = null)
|
Chris@0
|
45 {
|
Chris@0
|
46 $this->logger = $logger;
|
Chris@0
|
47
|
Chris@0
|
48 $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
|
Chris@0
|
49 $this->supportsScalarTypes = method_exists('ReflectionParameter', 'getType');
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * {@inheritdoc}
|
Chris@0
|
54 *
|
Chris@0
|
55 * This method looks for a '_controller' request attribute that represents
|
Chris@0
|
56 * the controller name (a string like ClassName::MethodName).
|
Chris@0
|
57 */
|
Chris@0
|
58 public function getController(Request $request)
|
Chris@0
|
59 {
|
Chris@0
|
60 if (!$controller = $request->attributes->get('_controller')) {
|
Chris@0
|
61 if (null !== $this->logger) {
|
Chris@0
|
62 $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing.');
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 return false;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@17
|
68 if (\is_array($controller)) {
|
Chris@0
|
69 return $controller;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@17
|
72 if (\is_object($controller)) {
|
Chris@0
|
73 if (method_exists($controller, '__invoke')) {
|
Chris@0
|
74 return $controller;
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@17
|
77 throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', \get_class($controller), $request->getPathInfo()));
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 if (false === strpos($controller, ':')) {
|
Chris@0
|
81 if (method_exists($controller, '__invoke')) {
|
Chris@0
|
82 return $this->instantiateController($controller);
|
Chris@17
|
83 } elseif (\function_exists($controller)) {
|
Chris@0
|
84 return $controller;
|
Chris@0
|
85 }
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 $callable = $this->createController($controller);
|
Chris@0
|
89
|
Chris@17
|
90 if (!\is_callable($callable)) {
|
Chris@0
|
91 throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable. %s', $request->getPathInfo(), $this->getControllerError($callable)));
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 return $callable;
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 /**
|
Chris@0
|
98 * {@inheritdoc}
|
Chris@0
|
99 *
|
Chris@0
|
100 * @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface and inject it in the HttpKernel instead.
|
Chris@0
|
101 */
|
Chris@0
|
102 public function getArguments(Request $request, $controller)
|
Chris@0
|
103 {
|
Chris@17
|
104 @trigger_error(sprintf('The "%s()" method is deprecated as of 3.1 and will be removed in 4.0. Implement the %s and inject it in the HttpKernel instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
|
Chris@0
|
105
|
Chris@17
|
106 if (\is_array($controller)) {
|
Chris@0
|
107 $r = new \ReflectionMethod($controller[0], $controller[1]);
|
Chris@17
|
108 } elseif (\is_object($controller) && !$controller instanceof \Closure) {
|
Chris@0
|
109 $r = new \ReflectionObject($controller);
|
Chris@0
|
110 $r = $r->getMethod('__invoke');
|
Chris@0
|
111 } else {
|
Chris@0
|
112 $r = new \ReflectionFunction($controller);
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 return $this->doGetArguments($request, $controller, $r->getParameters());
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * @param Request $request
|
Chris@0
|
120 * @param callable $controller
|
Chris@0
|
121 * @param \ReflectionParameter[] $parameters
|
Chris@0
|
122 *
|
Chris@0
|
123 * @return array The arguments to use when calling the action
|
Chris@0
|
124 *
|
Chris@0
|
125 * @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface and inject it in the HttpKernel instead.
|
Chris@0
|
126 */
|
Chris@0
|
127 protected function doGetArguments(Request $request, $controller, array $parameters)
|
Chris@0
|
128 {
|
Chris@17
|
129 @trigger_error(sprintf('The "%s()" method is deprecated as of 3.1 and will be removed in 4.0. Implement the %s and inject it in the HttpKernel instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
|
Chris@0
|
130
|
Chris@0
|
131 $attributes = $request->attributes->all();
|
Chris@17
|
132 $arguments = [];
|
Chris@0
|
133 foreach ($parameters as $param) {
|
Chris@18
|
134 if (\array_key_exists($param->name, $attributes)) {
|
Chris@17
|
135 if ($this->supportsVariadic && $param->isVariadic() && \is_array($attributes[$param->name])) {
|
Chris@0
|
136 $arguments = array_merge($arguments, array_values($attributes[$param->name]));
|
Chris@0
|
137 } else {
|
Chris@0
|
138 $arguments[] = $attributes[$param->name];
|
Chris@0
|
139 }
|
Chris@0
|
140 } elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
|
Chris@0
|
141 $arguments[] = $request;
|
Chris@0
|
142 } elseif ($param->isDefaultValueAvailable()) {
|
Chris@0
|
143 $arguments[] = $param->getDefaultValue();
|
Chris@0
|
144 } elseif ($this->supportsScalarTypes && $param->hasType() && $param->allowsNull()) {
|
Chris@0
|
145 $arguments[] = null;
|
Chris@0
|
146 } else {
|
Chris@17
|
147 if (\is_array($controller)) {
|
Chris@17
|
148 $repr = sprintf('%s::%s()', \get_class($controller[0]), $controller[1]);
|
Chris@17
|
149 } elseif (\is_object($controller)) {
|
Chris@17
|
150 $repr = \get_class($controller);
|
Chris@0
|
151 } else {
|
Chris@0
|
152 $repr = $controller;
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 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));
|
Chris@0
|
156 }
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 return $arguments;
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 /**
|
Chris@0
|
163 * Returns a callable for the given controller.
|
Chris@0
|
164 *
|
Chris@0
|
165 * @param string $controller A Controller string
|
Chris@0
|
166 *
|
Chris@0
|
167 * @return callable A PHP callable
|
Chris@0
|
168 *
|
Chris@0
|
169 * @throws \InvalidArgumentException
|
Chris@0
|
170 */
|
Chris@0
|
171 protected function createController($controller)
|
Chris@0
|
172 {
|
Chris@0
|
173 if (false === strpos($controller, '::')) {
|
Chris@0
|
174 throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 list($class, $method) = explode('::', $controller, 2);
|
Chris@0
|
178
|
Chris@0
|
179 if (!class_exists($class)) {
|
Chris@0
|
180 throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@17
|
183 return [$this->instantiateController($class), $method];
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 /**
|
Chris@0
|
187 * Returns an instantiated controller.
|
Chris@0
|
188 *
|
Chris@0
|
189 * @param string $class A class name
|
Chris@0
|
190 *
|
Chris@0
|
191 * @return object
|
Chris@0
|
192 */
|
Chris@0
|
193 protected function instantiateController($class)
|
Chris@0
|
194 {
|
Chris@0
|
195 return new $class();
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 private function getControllerError($callable)
|
Chris@0
|
199 {
|
Chris@17
|
200 if (\is_string($callable)) {
|
Chris@0
|
201 if (false !== strpos($callable, '::')) {
|
Chris@0
|
202 $callable = explode('::', $callable);
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 if (class_exists($callable) && !method_exists($callable, '__invoke')) {
|
Chris@0
|
206 return sprintf('Class "%s" does not have a method "__invoke".', $callable);
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@17
|
209 if (!\function_exists($callable)) {
|
Chris@0
|
210 return sprintf('Function "%s" does not exist.', $callable);
|
Chris@0
|
211 }
|
Chris@0
|
212 }
|
Chris@0
|
213
|
Chris@17
|
214 if (!\is_array($callable)) {
|
Chris@17
|
215 return sprintf('Invalid type for controller given, expected string or array, got "%s".', \gettype($callable));
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@17
|
218 if (2 !== \count($callable)) {
|
Chris@17
|
219 return 'Invalid format for controller, expected [controller, method] or controller::method.';
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 list($controller, $method) = $callable;
|
Chris@0
|
223
|
Chris@17
|
224 if (\is_string($controller) && !class_exists($controller)) {
|
Chris@0
|
225 return sprintf('Class "%s" does not exist.', $controller);
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@17
|
228 $className = \is_object($controller) ? \get_class($controller) : $controller;
|
Chris@0
|
229
|
Chris@0
|
230 if (method_exists($controller, $method)) {
|
Chris@0
|
231 return sprintf('Method "%s" on class "%s" should be public and non-abstract.', $method, $className);
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 $collection = get_class_methods($controller);
|
Chris@0
|
235
|
Chris@17
|
236 $alternatives = [];
|
Chris@0
|
237
|
Chris@0
|
238 foreach ($collection as $item) {
|
Chris@0
|
239 $lev = levenshtein($method, $item);
|
Chris@0
|
240
|
Chris@17
|
241 if ($lev <= \strlen($method) / 3 || false !== strpos($item, $method)) {
|
Chris@0
|
242 $alternatives[] = $item;
|
Chris@0
|
243 }
|
Chris@0
|
244 }
|
Chris@0
|
245
|
Chris@0
|
246 asort($alternatives);
|
Chris@0
|
247
|
Chris@0
|
248 $message = sprintf('Expected method "%s" on class "%s"', $method, $className);
|
Chris@0
|
249
|
Chris@17
|
250 if (\count($alternatives) > 0) {
|
Chris@0
|
251 $message .= sprintf(', did you mean "%s"?', implode('", "', $alternatives));
|
Chris@0
|
252 } else {
|
Chris@0
|
253 $message .= sprintf('. Available methods: "%s".', implode('", "', $collection));
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 return $message;
|
Chris@0
|
257 }
|
Chris@0
|
258 }
|