ControllerResolver.php in Drupal 9
File
core/lib/Drupal/Core/Controller/ControllerResolver.php
View source
<?php
namespace Drupal\Core\Controller;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver;
class ControllerResolver extends BaseControllerResolver implements ControllerResolverInterface {
protected $classResolver;
protected $httpMessageFactory;
public function __construct(HttpMessageFactoryInterface $http_message_factory, ClassResolverInterface $class_resolver) {
$this->httpMessageFactory = $http_message_factory;
$this->classResolver = $class_resolver;
}
public function getControllerFromDefinition($controller, $path = '') {
if (is_array($controller) || is_object($controller) && method_exists($controller, '__invoke')) {
return $controller;
}
if (strpos($controller, ':') === FALSE) {
if (function_exists($controller)) {
return $controller;
}
return $this->classResolver
->getInstanceFromDefinition($controller);
}
$callable = $this
->createController($controller);
if (!is_callable($callable)) {
throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable.', $path));
}
return $callable;
}
public function getController(Request $request) {
if (!($controller = $request->attributes
->get('_controller'))) {
return FALSE;
}
return $this
->getControllerFromDefinition($controller, $request
->getPathInfo());
}
protected function createController($controller) {
$count = substr_count($controller, ':');
if ($count == 1) {
list($class_or_service, $method) = explode(':', $controller, 2);
}
elseif (strpos($controller, '::') !== FALSE) {
list($class_or_service, $method) = explode('::', $controller, 2);
}
else {
throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
}
$controller = $this->classResolver
->getInstanceFromDefinition($class_or_service);
return [
$controller,
$method,
];
}
}
Classes
Name |
Description |
ControllerResolver |
ControllerResolver to enhance controllers beyond Symfony's basic handling. |