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