protected function ControllerResolver::createController

Same name in this branch
  1. 8.x drupal/core/lib/Drupal/Core/Controller/ControllerResolver.php \Drupal\Core\Controller\ControllerResolver::createController()
  2. 8.x drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Controller/ControllerResolver.php \Symfony\Component\HttpKernel\Controller\ControllerResolver::createController()

Returns a callable for the given controller.

Parameters

string $controller: A Controller string.

Return value

mixed A PHP callable.

Throws

\LogicException If the controller cannot be parsed

\InvalidArgumentException If the controller class does not exist

Overrides ControllerResolver::createController

File

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

Class

ControllerResolver
ControllerResolver to enhance controllers beyond Symfony's basic handling.

Namespace

Drupal\Core\Controller

Code

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,
  );
}