annotate core/lib/Drupal/Core/Controller/ControllerResolver.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Controller;
Chris@0 4
Chris@0 5 use Drupal\Core\DependencyInjection\ClassResolverInterface;
Chris@0 6 use Drupal\Core\Routing\RouteMatch;
Chris@0 7 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 8 use Psr\Http\Message\ServerRequestInterface;
Chris@0 9 use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
Chris@0 10 use Symfony\Component\HttpFoundation\Request;
Chris@0 11 use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * ControllerResolver to enhance controllers beyond Symfony's basic handling.
Chris@0 15 *
Chris@0 16 * It adds two behaviors:
Chris@0 17 *
Chris@0 18 * - When creating a new object-based controller that implements
Chris@0 19 * ContainerAwareInterface, inject the container into it. While not always
Chris@0 20 * necessary, that allows a controller to vary the services it needs at
Chris@0 21 * runtime.
Chris@0 22 *
Chris@0 23 * - By default, a controller name follows the class::method notation. This
Chris@0 24 * class adds the possibility to use a service from the container as a
Chris@0 25 * controller by using a service:method notation (Symfony uses the same
Chris@0 26 * convention).
Chris@0 27 */
Chris@0 28 class ControllerResolver extends BaseControllerResolver implements ControllerResolverInterface {
Chris@0 29
Chris@0 30 /**
Chris@0 31 * The class resolver.
Chris@0 32 *
Chris@0 33 * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
Chris@0 34 */
Chris@0 35 protected $classResolver;
Chris@0 36
Chris@0 37 /**
Chris@0 38 * The PSR-7 converter.
Chris@0 39 *
Chris@0 40 * @var \Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface
Chris@0 41 */
Chris@0 42 protected $httpMessageFactory;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * Constructs a new ControllerResolver.
Chris@0 46 *
Chris@0 47 * @param \Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface $http_message_factory
Chris@0 48 * The PSR-7 converter.
Chris@0 49 * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
Chris@0 50 * The class resolver.
Chris@0 51 */
Chris@0 52 public function __construct(HttpMessageFactoryInterface $http_message_factory, ClassResolverInterface $class_resolver) {
Chris@0 53 $this->httpMessageFactory = $http_message_factory;
Chris@0 54 $this->classResolver = $class_resolver;
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * {@inheritdoc}
Chris@0 59 */
Chris@0 60 public function getControllerFromDefinition($controller, $path = '') {
Chris@0 61 if (is_array($controller) || (is_object($controller) && method_exists($controller, '__invoke'))) {
Chris@0 62 return $controller;
Chris@0 63 }
Chris@0 64
Chris@0 65 if (strpos($controller, ':') === FALSE) {
Chris@0 66 if (function_exists($controller)) {
Chris@0 67 return $controller;
Chris@0 68 }
Chris@18 69 return $this->classResolver->getInstanceFromDefinition($controller);
Chris@0 70 }
Chris@0 71
Chris@0 72 $callable = $this->createController($controller);
Chris@0 73
Chris@0 74 if (!is_callable($callable)) {
Chris@0 75 throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable.', $path));
Chris@0 76 }
Chris@0 77
Chris@0 78 return $callable;
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * {@inheritdoc}
Chris@0 83 */
Chris@0 84 public function getController(Request $request) {
Chris@0 85 if (!$controller = $request->attributes->get('_controller')) {
Chris@0 86 return FALSE;
Chris@0 87 }
Chris@0 88 return $this->getControllerFromDefinition($controller, $request->getPathInfo());
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * Returns a callable for the given controller.
Chris@0 93 *
Chris@0 94 * @param string $controller
Chris@0 95 * A Controller string.
Chris@0 96 *
Chris@0 97 * @return mixed
Chris@0 98 * A PHP callable.
Chris@0 99 *
Chris@0 100 * @throws \LogicException
Chris@0 101 * If the controller cannot be parsed.
Chris@0 102 *
Chris@0 103 * @throws \InvalidArgumentException
Chris@0 104 * If the controller class does not exist.
Chris@0 105 */
Chris@0 106 protected function createController($controller) {
Chris@0 107 // Controller in the service:method notation.
Chris@0 108 $count = substr_count($controller, ':');
Chris@0 109 if ($count == 1) {
Chris@0 110 list($class_or_service, $method) = explode(':', $controller, 2);
Chris@0 111 }
Chris@0 112 // Controller in the class::method notation.
Chris@0 113 elseif (strpos($controller, '::') !== FALSE) {
Chris@0 114 list($class_or_service, $method) = explode('::', $controller, 2);
Chris@0 115 }
Chris@0 116 else {
Chris@0 117 throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
Chris@0 118 }
Chris@0 119
Chris@0 120 $controller = $this->classResolver->getInstanceFromDefinition($class_or_service);
Chris@0 121
Chris@0 122 return [$controller, $method];
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * {@inheritdoc}
Chris@0 127 */
Chris@0 128 protected function doGetArguments(Request $request, $controller, array $parameters) {
Chris@17 129 // Note this duplicates the deprecation message of
Chris@17 130 // Symfony\Component\HttpKernel\Controller\ControllerResolver::getArguments()
Chris@17 131 // to ensure it is removed in Drupal 9.
Chris@17 132 @trigger_error(sprintf('%s is deprecated as of 8.6.0 and will be removed in 9.0. Inject the "http_kernel.controller.argument_resolver" service instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
Chris@0 133 $attributes = $request->attributes->all();
Chris@0 134 $raw_parameters = $request->attributes->has('_raw_variables') ? $request->attributes->get('_raw_variables') : [];
Chris@0 135 $arguments = [];
Chris@0 136 foreach ($parameters as $param) {
Chris@0 137 if (array_key_exists($param->name, $attributes)) {
Chris@0 138 $arguments[] = $attributes[$param->name];
Chris@0 139 }
Chris@0 140 elseif (array_key_exists($param->name, $raw_parameters)) {
Chris@0 141 $arguments[] = $attributes[$param->name];
Chris@0 142 }
Chris@0 143 elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
Chris@0 144 $arguments[] = $request;
Chris@0 145 }
Chris@0 146 elseif ($param->getClass() && $param->getClass()->name === ServerRequestInterface::class) {
Chris@0 147 $arguments[] = $this->httpMessageFactory->createRequest($request);
Chris@0 148 }
Chris@0 149 elseif ($param->getClass() && ($param->getClass()->name == RouteMatchInterface::class || is_subclass_of($param->getClass()->name, RouteMatchInterface::class))) {
Chris@0 150 $arguments[] = RouteMatch::createFromRequest($request);
Chris@0 151 }
Chris@0 152 elseif ($param->isDefaultValueAvailable()) {
Chris@0 153 $arguments[] = $param->getDefaultValue();
Chris@0 154 }
Chris@0 155 else {
Chris@0 156 if (is_array($controller)) {
Chris@0 157 $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
Chris@0 158 }
Chris@0 159 elseif (is_object($controller)) {
Chris@0 160 $repr = get_class($controller);
Chris@0 161 }
Chris@0 162 else {
Chris@0 163 $repr = $controller;
Chris@0 164 }
Chris@0 165
Chris@0 166 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));
Chris@0 167 }
Chris@0 168 }
Chris@0 169 return $arguments;
Chris@0 170 }
Chris@0 171
Chris@0 172 }