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