Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel\Controller; Chris@0: Chris@0: use Psr\Log\LoggerInterface; Chris@0: use Symfony\Component\HttpFoundation\Request; Chris@0: Chris@0: /** Chris@0: * This implementation uses the '_controller' request attribute to determine Chris@0: * the controller to execute and uses the request attributes to determine Chris@0: * the controller method arguments. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class ControllerResolver implements ArgumentResolverInterface, ControllerResolverInterface Chris@0: { Chris@0: private $logger; Chris@0: Chris@0: /** Chris@0: * If the ...$arg functionality is available. Chris@0: * Chris@0: * Requires at least PHP 5.6.0 or HHVM 3.9.1 Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: private $supportsVariadic; Chris@0: Chris@0: /** Chris@0: * If scalar types exists. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: private $supportsScalarTypes; Chris@0: Chris@0: public function __construct(LoggerInterface $logger = null) Chris@0: { Chris@0: $this->logger = $logger; Chris@0: Chris@0: $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic'); Chris@0: $this->supportsScalarTypes = method_exists('ReflectionParameter', 'getType'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * This method looks for a '_controller' request attribute that represents Chris@0: * the controller name (a string like ClassName::MethodName). Chris@0: */ Chris@0: public function getController(Request $request) Chris@0: { Chris@0: if (!$controller = $request->attributes->get('_controller')) { Chris@0: if (null !== $this->logger) { Chris@0: $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing.'); Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@17: if (\is_array($controller)) { Chris@0: return $controller; Chris@0: } Chris@0: Chris@17: if (\is_object($controller)) { Chris@0: if (method_exists($controller, '__invoke')) { Chris@0: return $controller; Chris@0: } Chris@0: Chris@17: throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', \get_class($controller), $request->getPathInfo())); Chris@0: } Chris@0: Chris@0: if (false === strpos($controller, ':')) { Chris@0: if (method_exists($controller, '__invoke')) { Chris@0: return $this->instantiateController($controller); Chris@17: } elseif (\function_exists($controller)) { Chris@0: return $controller; Chris@0: } Chris@0: } Chris@0: Chris@0: $callable = $this->createController($controller); Chris@0: Chris@17: if (!\is_callable($callable)) { Chris@0: throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable. %s', $request->getPathInfo(), $this->getControllerError($callable))); Chris@0: } Chris@0: Chris@0: return $callable; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * @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: */ Chris@0: public function getArguments(Request $request, $controller) Chris@0: { Chris@17: @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: Chris@17: if (\is_array($controller)) { Chris@0: $r = new \ReflectionMethod($controller[0], $controller[1]); Chris@17: } elseif (\is_object($controller) && !$controller instanceof \Closure) { Chris@0: $r = new \ReflectionObject($controller); Chris@0: $r = $r->getMethod('__invoke'); Chris@0: } else { Chris@0: $r = new \ReflectionFunction($controller); Chris@0: } Chris@0: Chris@0: return $this->doGetArguments($request, $controller, $r->getParameters()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param Request $request Chris@0: * @param callable $controller Chris@0: * @param \ReflectionParameter[] $parameters Chris@0: * Chris@0: * @return array The arguments to use when calling the action Chris@0: * Chris@0: * @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: */ Chris@0: protected function doGetArguments(Request $request, $controller, array $parameters) Chris@0: { Chris@17: @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: Chris@0: $attributes = $request->attributes->all(); Chris@17: $arguments = []; Chris@0: foreach ($parameters as $param) { Chris@18: if (\array_key_exists($param->name, $attributes)) { Chris@17: if ($this->supportsVariadic && $param->isVariadic() && \is_array($attributes[$param->name])) { Chris@0: $arguments = array_merge($arguments, array_values($attributes[$param->name])); Chris@0: } else { Chris@0: $arguments[] = $attributes[$param->name]; Chris@0: } Chris@0: } elseif ($param->getClass() && $param->getClass()->isInstance($request)) { Chris@0: $arguments[] = $request; Chris@0: } elseif ($param->isDefaultValueAvailable()) { Chris@0: $arguments[] = $param->getDefaultValue(); Chris@0: } elseif ($this->supportsScalarTypes && $param->hasType() && $param->allowsNull()) { Chris@0: $arguments[] = null; Chris@0: } else { Chris@17: if (\is_array($controller)) { Chris@17: $repr = sprintf('%s::%s()', \get_class($controller[0]), $controller[1]); Chris@17: } elseif (\is_object($controller)) { Chris@17: $repr = \get_class($controller); Chris@0: } else { Chris@0: $repr = $controller; Chris@0: } Chris@0: Chris@0: 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: } Chris@0: } Chris@0: Chris@0: return $arguments; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a callable for the given controller. Chris@0: * Chris@0: * @param string $controller A Controller string Chris@0: * Chris@0: * @return callable A PHP callable Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: */ Chris@0: protected function createController($controller) Chris@0: { Chris@0: if (false === strpos($controller, '::')) { Chris@0: throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller)); Chris@0: } Chris@0: Chris@0: list($class, $method) = explode('::', $controller, 2); Chris@0: Chris@0: if (!class_exists($class)) { Chris@0: throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); Chris@0: } Chris@0: Chris@17: return [$this->instantiateController($class), $method]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an instantiated controller. Chris@0: * Chris@0: * @param string $class A class name Chris@0: * Chris@0: * @return object Chris@0: */ Chris@0: protected function instantiateController($class) Chris@0: { Chris@0: return new $class(); Chris@0: } Chris@0: Chris@0: private function getControllerError($callable) Chris@0: { Chris@17: if (\is_string($callable)) { Chris@0: if (false !== strpos($callable, '::')) { Chris@0: $callable = explode('::', $callable); Chris@0: } Chris@0: Chris@0: if (class_exists($callable) && !method_exists($callable, '__invoke')) { Chris@0: return sprintf('Class "%s" does not have a method "__invoke".', $callable); Chris@0: } Chris@0: Chris@17: if (!\function_exists($callable)) { Chris@0: return sprintf('Function "%s" does not exist.', $callable); Chris@0: } Chris@0: } Chris@0: Chris@17: if (!\is_array($callable)) { Chris@17: return sprintf('Invalid type for controller given, expected string or array, got "%s".', \gettype($callable)); Chris@0: } Chris@0: Chris@17: if (2 !== \count($callable)) { Chris@17: return 'Invalid format for controller, expected [controller, method] or controller::method.'; Chris@0: } Chris@0: Chris@0: list($controller, $method) = $callable; Chris@0: Chris@17: if (\is_string($controller) && !class_exists($controller)) { Chris@0: return sprintf('Class "%s" does not exist.', $controller); Chris@0: } Chris@0: Chris@17: $className = \is_object($controller) ? \get_class($controller) : $controller; Chris@0: Chris@0: if (method_exists($controller, $method)) { Chris@0: return sprintf('Method "%s" on class "%s" should be public and non-abstract.', $method, $className); Chris@0: } Chris@0: Chris@0: $collection = get_class_methods($controller); Chris@0: Chris@17: $alternatives = []; Chris@0: Chris@0: foreach ($collection as $item) { Chris@0: $lev = levenshtein($method, $item); Chris@0: Chris@17: if ($lev <= \strlen($method) / 3 || false !== strpos($item, $method)) { Chris@0: $alternatives[] = $item; Chris@0: } Chris@0: } Chris@0: Chris@0: asort($alternatives); Chris@0: Chris@0: $message = sprintf('Expected method "%s" on class "%s"', $method, $className); Chris@0: Chris@17: if (\count($alternatives) > 0) { Chris@0: $message .= sprintf(', did you mean "%s"?', implode('", "', $alternatives)); Chris@0: } else { Chris@0: $message .= sprintf('. Available methods: "%s".', implode('", "', $collection)); Chris@0: } Chris@0: Chris@0: return $message; Chris@0: } Chris@0: }