[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Controller/ -> ContainerControllerResolver.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpKernel\Controller;
  13  
  14  use Psr\Container\ContainerInterface;
  15  use Psr\Log\LoggerInterface;
  16  use Symfony\Component\DependencyInjection\Container;
  17  use Symfony\Component\HttpFoundation\Request;
  18  
  19  /**
  20   * A controller resolver searching for a controller in a psr-11 container when using the "service:method" notation.
  21   *
  22   * @author Fabien Potencier <fabien@symfony.com>
  23   * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  24   */
  25  class ContainerControllerResolver extends ControllerResolver
  26  {
  27      protected $container;
  28  
  29      public function __construct(ContainerInterface $container, LoggerInterface $logger = null)
  30      {
  31          $this->container = $container;
  32  
  33          parent::__construct($logger);
  34      }
  35  
  36      /**
  37       * {@inheritdoc}
  38       */
  39      public function getController(Request $request)
  40      {
  41          $controller = parent::getController($request);
  42  
  43          if (\is_array($controller) && isset($controller[0]) && \is_string($controller[0]) && $this->container->has($controller[0])) {
  44              $controller[0] = $this->instantiateController($controller[0]);
  45          }
  46  
  47          return $controller;
  48      }
  49  
  50      /**
  51       * Returns a callable for the given controller.
  52       *
  53       * @param string $controller A Controller string
  54       *
  55       * @return mixed A PHP callable
  56       *
  57       * @throws \LogicException           When the name could not be parsed
  58       * @throws \InvalidArgumentException When the controller class does not exist
  59       */
  60      protected function createController($controller)
  61      {
  62          if (false !== strpos($controller, '::')) {
  63              return parent::createController($controller);
  64          }
  65  
  66          $method = null;
  67          if (1 == substr_count($controller, ':')) {
  68              // controller in the "service:method" notation
  69              list($controller, $method) = explode(':', $controller, 2);
  70          }
  71  
  72          if (!$this->container->has($controller)) {
  73              $this->throwExceptionIfControllerWasRemoved($controller);
  74  
  75              throw new \LogicException(sprintf('Controller not found: service "%s" does not exist.', $controller));
  76          }
  77  
  78          $service = $this->container->get($controller);
  79          if (null !== $method) {
  80              return [$service, $method];
  81          }
  82  
  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));
  85          }
  86  
  87          return $service;
  88      }
  89  
  90      /**
  91       * {@inheritdoc}
  92       */
  93      protected function instantiateController($class)
  94      {
  95          if ($this->container->has($class)) {
  96              return $this->container->get($class);
  97          }
  98  
  99          try {
 100              return parent::instantiateController($class);
 101          } catch (\ArgumentCountError $e) {
 102          } catch (\ErrorException $e) {
 103          } catch (\TypeError $e) {
 104          }
 105  
 106          $this->throwExceptionIfControllerWasRemoved($class, $e);
 107  
 108          throw $e;
 109      }
 110  
 111      /**
 112       * @param string                     $controller
 113       * @param \Exception|\Throwable|null $previous
 114       */
 115      private function throwExceptionIfControllerWasRemoved($controller, $previous = null)
 116      {
 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);
 119          }
 120      }
 121  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1