class ControllerResolver

Same name in this branch

ControllerResolver.

This implementation uses the '_controller' request attribute to determine the controller to execute and uses the request attributes to determine the controller method arguments.

@author Fabien Potencier <fabien@symfony.com>

@api

Hierarchy

Expanded class hierarchy of ControllerResolver

2 files declare their use of ControllerResolver
ControllerResolver.php in drupal/core/lib/Drupal/Core/ControllerResolver.php
Definition of Drupal\Core\ControllerResolver.
ControllerResolverTest.php in drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/Controller/ControllerResolverTest.php

File

drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Controller/ControllerResolver.php, line 28

Namespace

Symfony\Component\HttpKernel\Controller
View source
class ControllerResolver implements ControllerResolverInterface {
  private $logger;

  /**
   * Constructor.
   *
   * @param LoggerInterface $logger A LoggerInterface instance
   */
  public function __construct(LoggerInterface $logger = null) {
    $this->logger = $logger;
  }

  /**
   * Returns the Controller instance associated with a Request.
   *
   * This method looks for a '_controller' request attribute that represents
   * the controller name (a string like ClassName::MethodName).
   *
   * @param Request $request A Request instance
   *
   * @return mixed|Boolean A PHP callable representing the Controller,
   *                       or false if this resolver is not able to determine the controller
   *
   * @throws \InvalidArgumentException|\LogicException If the controller can't be found
   *
   * @api
   */
  public function getController(Request $request) {
    if (!($controller = $request->attributes
      ->get('_controller'))) {
      if (null !== $this->logger) {
        $this->logger
          ->warn('Unable to look for the controller as the "_controller" parameter is missing');
      }
      return false;
    }
    if (is_array($controller) || is_object($controller) && method_exists($controller, '__invoke')) {
      return $controller;
    }
    if (false === strpos($controller, ':')) {
      if (method_exists($controller, '__invoke')) {
        return new $controller();
      }
      elseif (function_exists($controller)) {
        return $controller;
      }
    }
    list($controller, $method) = $this
      ->createController($controller);
    if (!method_exists($controller, $method)) {
      throw new \InvalidArgumentException(sprintf('Method "%s::%s" does not exist.', get_class($controller), $method));
    }
    return array(
      $controller,
      $method,
    );
  }

  /**
   * Returns the arguments to pass to the controller.
   *
   * @param Request $request    A Request instance
   * @param mixed   $controller A PHP callable
   *
   * @throws \RuntimeException When value for argument given is not provided
   *
   * @api
   */
  public function getArguments(Request $request, $controller) {
    if (is_array($controller)) {
      $r = new \ReflectionMethod($controller[0], $controller[1]);
    }
    elseif (is_object($controller) && !$controller instanceof \Closure) {
      $r = new \ReflectionObject($controller);
      $r = $r
        ->getMethod('__invoke');
    }
    else {
      $r = new \ReflectionFunction($controller);
    }
    return $this
      ->doGetArguments($request, $controller, $r
      ->getParameters());
  }
  protected function doGetArguments(Request $request, $controller, array $parameters) {
    $attributes = $request->attributes
      ->all();
    $arguments = array();
    foreach ($parameters as $param) {
      if (array_key_exists($param->name, $attributes)) {
        $arguments[] = $attributes[$param->name];
      }
      elseif ($param
        ->getClass() && $param
        ->getClass()
        ->isInstance($request)) {
        $arguments[] = $request;
      }
      elseif ($param
        ->isDefaultValueAvailable()) {
        $arguments[] = $param
          ->getDefaultValue();
      }
      else {
        if (is_array($controller)) {
          $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
        }
        elseif (is_object($controller)) {
          $repr = get_class($controller);
        }
        else {
          $repr = $controller;
        }
        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));
      }
    }
    return $arguments;
  }

  /**
   * Returns a callable for the given controller.
   *
   * @param string $controller A Controller string
   *
   * @return mixed A PHP callable
   */
  protected function createController($controller) {
    if (false === strpos($controller, '::')) {
      throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
    }
    list($class, $method) = explode('::', $controller, 2);
    if (!class_exists($class)) {
      throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
    }
    return array(
      new $class(),
      $method,
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerResolver::$logger private property
ControllerResolver::createController protected function Returns a callable for the given controller. 1
ControllerResolver::doGetArguments protected function
ControllerResolver::getArguments public function Returns the arguments to pass to the controller. Overrides ControllerResolverInterface::getArguments
ControllerResolver::getController public function Returns the Controller instance associated with a Request. Overrides ControllerResolverInterface::getController
ControllerResolver::__construct public function Constructor. 1