vendor/symfony/http-kernel/Controller/ContainerControllerResolver.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpKernel\Controller;
  11. use Psr\Container\ContainerInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\DependencyInjection\Container;
  14. /**
  15.  * A controller resolver searching for a controller in a psr-11 container when using the "service::method" notation.
  16.  *
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  19.  */
  20. class ContainerControllerResolver extends ControllerResolver
  21. {
  22.     protected $container;
  23.     public function __construct(ContainerInterface $containerLoggerInterface $logger null)
  24.     {
  25.         $this->container $container;
  26.         parent::__construct($logger);
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     protected function instantiateController(string $class): object
  32.     {
  33.         $class ltrim($class'\\');
  34.         if ($this->container->has($class)) {
  35.             return $this->container->get($class);
  36.         }
  37.         try {
  38.             return parent::instantiateController($class);
  39.         } catch (\Error $e) {
  40.         }
  41.         $this->throwExceptionIfControllerWasRemoved($class$e);
  42.         if ($e instanceof \ArgumentCountError) {
  43.             throw new \InvalidArgumentException(sprintf('Controller "%s" has required constructor arguments and does not exist in the container. Did you forget to define the controller as a service?'$class), 0$e);
  44.         }
  45.         throw new \InvalidArgumentException(sprintf('Controller "%s" does neither exist as service nor as class.'$class), 0$e);
  46.     }
  47.     private function throwExceptionIfControllerWasRemoved(string $controller\Throwable $previous)
  48.     {
  49.         if ($this->container instanceof Container && isset($this->container->getRemovedIds()[$controller])) {
  50.             throw new \InvalidArgumentException(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);
  51.         }
  52.     }
  53. }