annotate vendor/symfony/http-kernel/Controller/ControllerResolver.php @ 8:50b0d041100e

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