View source
<?php
namespace Drupal\Core\Controller;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\RouteMatchInterface;
use Psr\Http\Message\ServerRequestInterface;
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;
}
elseif (method_exists($controller, '__invoke')) {
return new $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 array(
$controller,
$method,
);
}
protected function doGetArguments(Request $request, $controller, array $parameters) {
$attributes = $request->attributes
->all();
$raw_parameters = $request->attributes
->has('_raw_variables') ? $request->attributes
->get('_raw_variables') : [];
$arguments = array();
foreach ($parameters as $param) {
if (array_key_exists($param->name, $attributes)) {
$arguments[] = $attributes[$param->name];
}
elseif (array_key_exists($param->name, $raw_parameters)) {
$arguments[] = $attributes[$param->name];
}
elseif ($param
->getClass() && $param
->getClass()
->isInstance($request)) {
$arguments[] = $request;
}
elseif ($param
->getClass() && $param
->getClass()->name === ServerRequestInterface::class) {
$arguments[] = $this->httpMessageFactory
->createRequest($request);
}
elseif ($param
->getClass() && ($param
->getClass()->name == RouteMatchInterface::class || is_subclass_of($param
->getClass()->name, RouteMatchInterface::class))) {
$arguments[] = RouteMatch::createFromRequest($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;
}
}