123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\HttpKernel\Controller;
- use Psr\Log\LoggerInterface;
- use Symfony\Component\HttpFoundation\Request;
- /**
- * This implementation uses the '_controller' request attribute to determine
- * the controller to execute.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- * @author Tobias Schultze <http://tobion.de>
- */
- class ControllerResolver implements ControllerResolverInterface
- {
- private $logger;
- public function __construct(LoggerInterface $logger = null)
- {
- $this->logger = $logger;
- }
- /**
- * {@inheritdoc}
- */
- public function getController(Request $request)
- {
- if (!$controller = $request->attributes->get('_controller')) {
- if (null !== $this->logger) {
- $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing.');
- }
- return false;
- }
- if (\is_array($controller)) {
- if (isset($controller[0]) && \is_string($controller[0]) && isset($controller[1])) {
- try {
- $controller[0] = $this->instantiateController($controller[0]);
- } catch (\Error|\LogicException $e) {
- try {
- // We cannot just check is_callable but have to use reflection because a non-static method
- // can still be called statically in PHP but we don't want that. This is deprecated in PHP 7, so we
- // could simplify this with PHP 8.
- if ((new \ReflectionMethod($controller[0], $controller[1]))->isStatic()) {
- return $controller;
- }
- } catch (\ReflectionException $reflectionException) {
- throw $e;
- }
- throw $e;
- }
- }
- if (!\is_callable($controller)) {
- throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable: ', $request->getPathInfo()).$this->getControllerError($controller));
- }
- return $controller;
- }
- if (\is_object($controller)) {
- if (!\is_callable($controller)) {
- throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable: '.$this->getControllerError($controller), $request->getPathInfo()));
- }
- return $controller;
- }
- if (\function_exists($controller)) {
- return $controller;
- }
- try {
- $callable = $this->createController($controller);
- } catch (\InvalidArgumentException $e) {
- throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable: ', $request->getPathInfo()).$e->getMessage(), 0, $e);
- }
- if (!\is_callable($callable)) {
- throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable: '.$this->getControllerError($callable), $request->getPathInfo()));
- }
- return $callable;
- }
- /**
- * Returns a callable for the given controller.
- *
- * @param string $controller A Controller string
- *
- * @return callable A PHP callable
- *
- * @throws \InvalidArgumentException When the controller cannot be created
- */
- protected function createController($controller)
- {
- if (!str_contains($controller, '::')) {
- $controller = $this->instantiateController($controller);
- if (!\is_callable($controller)) {
- throw new \InvalidArgumentException($this->getControllerError($controller));
- }
- return $controller;
- }
- [$class, $method] = explode('::', $controller, 2);
- try {
- $controller = [$this->instantiateController($class), $method];
- } catch (\Error|\LogicException $e) {
- try {
- if ((new \ReflectionMethod($class, $method))->isStatic()) {
- return $class.'::'.$method;
- }
- } catch (\ReflectionException $reflectionException) {
- throw $e;
- }
- throw $e;
- }
- if (!\is_callable($controller)) {
- throw new \InvalidArgumentException($this->getControllerError($controller));
- }
- return $controller;
- }
- /**
- * Returns an instantiated controller.
- *
- * @param string $class A class name
- *
- * @return object
- */
- protected function instantiateController($class)
- {
- return new $class();
- }
- private function getControllerError($callable): string
- {
- if (\is_string($callable)) {
- if (str_contains($callable, '::')) {
- $callable = explode('::', $callable, 2);
- } else {
- return sprintf('Function "%s" does not exist.', $callable);
- }
- }
- if (\is_object($callable)) {
- $availableMethods = $this->getClassMethodsWithoutMagicMethods($callable);
- $alternativeMsg = $availableMethods ? sprintf(' or use one of the available methods: "%s"', implode('", "', $availableMethods)) : '';
- return sprintf('Controller class "%s" cannot be called without a method name. You need to implement "__invoke"%s.', \get_class($callable), $alternativeMsg);
- }
- if (!\is_array($callable)) {
- return sprintf('Invalid type for controller given, expected string, array or object, got "%s".', \gettype($callable));
- }
- if (!isset($callable[0]) || !isset($callable[1]) || 2 !== \count($callable)) {
- return 'Invalid array callable, expected [controller, method].';
- }
- [$controller, $method] = $callable;
- if (\is_string($controller) && !class_exists($controller)) {
- return sprintf('Class "%s" does not exist.', $controller);
- }
- $className = \is_object($controller) ? \get_class($controller) : $controller;
- if (method_exists($controller, $method)) {
- return sprintf('Method "%s" on class "%s" should be public and non-abstract.', $method, $className);
- }
- $collection = $this->getClassMethodsWithoutMagicMethods($controller);
- $alternatives = [];
- foreach ($collection as $item) {
- $lev = levenshtein($method, $item);
- if ($lev <= \strlen($method) / 3 || str_contains($item, $method)) {
- $alternatives[] = $item;
- }
- }
- asort($alternatives);
- $message = sprintf('Expected method "%s" on class "%s"', $method, $className);
- if (\count($alternatives) > 0) {
- $message .= sprintf(', did you mean "%s"?', implode('", "', $alternatives));
- } else {
- $message .= sprintf('. Available methods: "%s".', implode('", "', $collection));
- }
- return $message;
- }
- private function getClassMethodsWithoutMagicMethods($classOrObject): array
- {
- $methods = get_class_methods($classOrObject);
- return array_filter($methods, function (string $method) {
- return 0 !== strncmp($method, '__', 2);
- });
- }
- }
|