[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Controller/ -> ControllerResolver.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\Log\LoggerInterface;
  15  use Symfony\Component\HttpFoundation\Request;
  16  
  17  /**
  18   * This implementation uses the '_controller' request attribute to determine
  19   * the controller to execute and uses the request attributes to determine
  20   * the controller method arguments.
  21   *
  22   * @author Fabien Potencier <fabien@symfony.com>
  23   */
  24  class ControllerResolver implements ControllerResolverInterface
  25  {
  26      private $logger;
  27  
  28      /**
  29       * If the ...$arg functionality is available.
  30       *
  31       * Requires at least PHP 5.6.0 or HHVM 3.9.1
  32       *
  33       * @var bool
  34       */
  35      private $supportsVariadic;
  36  
  37      /**
  38       * If scalar types exists.
  39       *
  40       * @var bool
  41       */
  42      private $supportsScalarTypes;
  43  
  44      public function __construct(LoggerInterface $logger = null)
  45      {
  46          $this->logger = $logger;
  47  
  48          $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
  49          $this->supportsScalarTypes = method_exists('ReflectionParameter', 'getType');
  50      }
  51  
  52      /**
  53       * {@inheritdoc}
  54       *
  55       * This method looks for a '_controller' request attribute that represents
  56       * the controller name (a string like ClassName::MethodName).
  57       */
  58      public function getController(Request $request)
  59      {
  60          if (!$controller = $request->attributes->get('_controller')) {
  61              if (null !== $this->logger) {
  62                  $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing.');
  63              }
  64  
  65              return false;
  66          }
  67  
  68          if (\is_array($controller)) {
  69              return $controller;
  70          }
  71  
  72          if (\is_object($controller)) {
  73              if (method_exists($controller, '__invoke')) {
  74                  return $controller;
  75              }
  76  
  77              throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', \get_class($controller), $request->getPathInfo()));
  78          }
  79  
  80          if (false === strpos($controller, ':')) {
  81              if (method_exists($controller, '__invoke')) {
  82                  return $this->instantiateController($controller);
  83              } elseif (\function_exists($controller)) {
  84                  return $controller;
  85              }
  86          }
  87  
  88          $callable = $this->createController($controller);
  89  
  90          if (!\is_callable($callable)) {
  91              throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', $controller, $request->getPathInfo()));
  92          }
  93  
  94          return $callable;
  95      }
  96  
  97      /**
  98       * {@inheritdoc}
  99       */
 100      public function getArguments(Request $request, $controller)
 101      {
 102          if (\is_array($controller)) {
 103              $r = new \ReflectionMethod($controller[0], $controller[1]);
 104          } elseif (\is_object($controller) && !$controller instanceof \Closure) {
 105              $r = new \ReflectionObject($controller);
 106              $r = $r->getMethod('__invoke');
 107          } else {
 108              $r = new \ReflectionFunction($controller);
 109          }
 110  
 111          return $this->doGetArguments($request, $controller, $r->getParameters());
 112      }
 113  
 114      /**
 115       * @param Request                $request
 116       * @param callable               $controller
 117       * @param \ReflectionParameter[] $parameters
 118       *
 119       * @return array The arguments to use when calling the action
 120       */
 121      protected function doGetArguments(Request $request, $controller, array $parameters)
 122      {
 123          $attributes = $request->attributes->all();
 124          $arguments = array();
 125          foreach ($parameters as $param) {
 126              if (array_key_exists($param->name, $attributes)) {
 127                  if ($this->supportsVariadic && $param->isVariadic() && \is_array($attributes[$param->name])) {
 128                      $arguments = array_merge($arguments, array_values($attributes[$param->name]));
 129                  } else {
 130                      $arguments[] = $attributes[$param->name];
 131                  }
 132              } elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
 133                  $arguments[] = $request;
 134              } elseif ($param->isDefaultValueAvailable()) {
 135                  $arguments[] = $param->getDefaultValue();
 136              } elseif ($this->supportsScalarTypes && $param->hasType() && $param->allowsNull()) {
 137                  $arguments[] = null;
 138              } else {
 139                  if (\is_array($controller)) {
 140                      $repr = sprintf('%s::%s()', \get_class($controller[0]), $controller[1]);
 141                  } elseif (\is_object($controller)) {
 142                      $repr = \get_class($controller);
 143                  } else {
 144                      $repr = $controller;
 145                  }
 146  
 147                  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));
 148              }
 149          }
 150  
 151          return $arguments;
 152      }
 153  
 154      /**
 155       * Returns a callable for the given controller.
 156       *
 157       * @param string $controller A Controller string
 158       *
 159       * @return callable A PHP callable
 160       *
 161       * @throws \InvalidArgumentException
 162       */
 163      protected function createController($controller)
 164      {
 165          if (false === strpos($controller, '::')) {
 166              throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
 167          }
 168  
 169          list($class, $method) = explode('::', $controller, 2);
 170  
 171          if (!class_exists($class)) {
 172              throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
 173          }
 174  
 175          return array($this->instantiateController($class), $method);
 176      }
 177  
 178      /**
 179       * Returns an instantiated controller.
 180       *
 181       * @param string $class A class name
 182       *
 183       * @return object
 184       */
 185      protected function instantiateController($class)
 186      {
 187          return new $class();
 188      }
 189  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1