class ControllerResolver

Same name in this branch

ControllerResolver to enhance controllers beyond Symfony's basic handling.

It adds two behaviors:

  • When creating a new object-based controller that implements ContainerAwareInterface, inject the container into it. While not always necessary, that allows a controller to vary the services it needs at runtime.
  • By default, a controller name follows the class::method notation. This class adds the possibility to use a service from the container as a controller by using a service:method notation (Symfony uses the same convention).

Hierarchy

Expanded class hierarchy of ControllerResolver

2 files declare their use of ControllerResolver
ControllerResolverTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Routing/ControllerResolverTest.php
Definition of Drupal\system\Tests\Routing\ControllerResolverTest.
HttpKernelTest.php in drupal/core/tests/Drupal/Tests/Core/HttpKernelTest.php
Contains \Drupal\Tests\Core\HttpKernelTest.
1 string reference to 'ControllerResolver'
core.services.yml in drupal/core/core.services.yml
drupal/core/core.services.yml
1 service uses ControllerResolver

File

drupal/core/lib/Drupal/Core/Controller/ControllerResolver.php, line 30
Contains \Drupal\Core\Controller\ControllerResolver.

Namespace

Drupal\Core\Controller
View source
class ControllerResolver extends BaseControllerResolver {

  /**
   * The injection container that should be injected into all controllers.
   *
   * @var Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * Constructs a new ControllerResolver.
   *
   * @param Symfony\Component\DependencyInjection\ContainerInterface $container
   *   A ContainerInterface instance.
   * @param Symfony\Component\HttpKernel\Log\LoggerInterface $logger
   *   (optional) A LoggerInterface instance.
   */
  public function __construct(ContainerInterface $container, LoggerInterface $logger = NULL) {
    $this->container = $container;
    parent::__construct($logger);
  }

  /**
   * Returns a callable for the given controller.
   *
   * @param string $controller
   *   A Controller string.
   *
   * @return mixed
   *   A PHP callable.
   *
   * @throws \LogicException
   *   If the controller cannot be parsed
   *
   * @throws \InvalidArgumentException
   *   If the controller class does not exist
   */
  protected function createController($controller) {

    // Controller in the service:method notation.
    $count = substr_count($controller, ':');
    if ($count == 1) {
      list($service, $method) = explode(':', $controller, 2);
      return array(
        $this->container
          ->get($service),
        $method,
      );
    }

    // Controller in the class::method notation.
    if (strpos($controller, '::') !== FALSE) {
      list($class, $method) = explode('::', $controller, 2);
      if (!class_exists($class)) {
        throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
      }

      // @todo Remove the second in_array() once that interface has been removed.
      if (in_array('Drupal\\Core\\Controller\\ControllerInterface', class_implements($class))) {
        $controller = $class::create($this->container);
      }
      else {
        $controller = new $class();
      }
    }
    else {
      throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
    }
    if ($controller instanceof ContainerAwareInterface) {
      $controller
        ->setContainer($this->container);
    }
    return array(
      $controller,
      $method,
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerResolver::$container protected property The injection container that should be injected into all controllers.
ControllerResolver::$logger private property
ControllerResolver::createController protected function Returns a callable for the given controller. Overrides ControllerResolver::createController
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 Constructs a new ControllerResolver. Overrides ControllerResolver::__construct