4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Controller;
14 use Psr\Container\ContainerInterface;
15 use Psr\Log\LoggerInterface;
16 use Symfony\Component\DependencyInjection\Container;
17 use Symfony\Component\HttpFoundation\Request;
20 * A controller resolver searching for a controller in a psr-11 container when using the "service:method" notation.
22 * @author Fabien Potencier <fabien@symfony.com>
23 * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
25 class ContainerControllerResolver extends ControllerResolver
29 public function __construct(ContainerInterface $container, LoggerInterface $logger = null)
31 $this->container = $container;
33 parent::__construct($logger);
39 public function getController(Request $request)
41 $controller = parent::getController($request);
43 if (is_array($controller) && isset($controller[0]) && is_string($controller[0]) && $this->container->has($controller[0])) {
44 $controller[0] = $this->instantiateController($controller[0]);
51 * Returns a callable for the given controller.
53 * @param string $controller A Controller string
55 * @return mixed A PHP callable
57 * @throws \LogicException When the name could not be parsed
58 * @throws \InvalidArgumentException When the controller class does not exist
60 protected function createController($controller)
62 if (false !== strpos($controller, '::')) {
63 return parent::createController($controller);
67 if (1 == substr_count($controller, ':')) {
68 // controller in the "service:method" notation
69 list($controller, $method) = explode(':', $controller, 2);
72 if (!$this->container->has($controller)) {
73 $this->throwExceptionIfControllerWasRemoved($controller);
75 throw new \LogicException(sprintf('Controller not found: service "%s" does not exist.', $controller));
78 $service = $this->container->get($controller);
79 if (null !== $method) {
80 return array($service, $method);
83 if (!method_exists($service, '__invoke')) {
84 throw new \LogicException(sprintf('Controller "%s" cannot be called without a method name. Did you forget an "__invoke" method?', $controller));
93 protected function instantiateController($class)
95 if ($this->container->has($class)) {
96 return $this->container->get($class);
100 return parent::instantiateController($class);
101 } catch (\ArgumentCountError $e) {
102 } catch (\ErrorException $e) {
103 } catch (\TypeError $e) {
106 $this->throwExceptionIfControllerWasRemoved($class, $e);
112 * @param string $controller
113 * @param \Exception|\Throwable|null $previous
115 private function throwExceptionIfControllerWasRemoved($controller, $previous = null)
117 if ($this->container instanceof Container && isset($this->container->getRemovedIds()[$controller])) {
118 throw new \LogicException(sprintf('Controller "%s" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?', $controller), 0, $previous);