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@0
|
69 elseif (method_exists($controller, '__invoke')) {
|
Chris@0
|
70 return new $controller();
|
Chris@0
|
71 }
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 $callable = $this->createController($controller);
|
Chris@0
|
75
|
Chris@0
|
76 if (!is_callable($callable)) {
|
Chris@0
|
77 throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable.', $path));
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 return $callable;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * {@inheritdoc}
|
Chris@0
|
86 */
|
Chris@0
|
87 public function getController(Request $request) {
|
Chris@0
|
88 if (!$controller = $request->attributes->get('_controller')) {
|
Chris@0
|
89 return FALSE;
|
Chris@0
|
90 }
|
Chris@0
|
91 return $this->getControllerFromDefinition($controller, $request->getPathInfo());
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Returns a callable for the given controller.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @param string $controller
|
Chris@0
|
98 * A Controller string.
|
Chris@0
|
99 *
|
Chris@0
|
100 * @return mixed
|
Chris@0
|
101 * A PHP callable.
|
Chris@0
|
102 *
|
Chris@0
|
103 * @throws \LogicException
|
Chris@0
|
104 * If the controller cannot be parsed.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @throws \InvalidArgumentException
|
Chris@0
|
107 * If the controller class does not exist.
|
Chris@0
|
108 */
|
Chris@0
|
109 protected function createController($controller) {
|
Chris@0
|
110 // Controller in the service:method notation.
|
Chris@0
|
111 $count = substr_count($controller, ':');
|
Chris@0
|
112 if ($count == 1) {
|
Chris@0
|
113 list($class_or_service, $method) = explode(':', $controller, 2);
|
Chris@0
|
114 }
|
Chris@0
|
115 // Controller in the class::method notation.
|
Chris@0
|
116 elseif (strpos($controller, '::') !== FALSE) {
|
Chris@0
|
117 list($class_or_service, $method) = explode('::', $controller, 2);
|
Chris@0
|
118 }
|
Chris@0
|
119 else {
|
Chris@0
|
120 throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 $controller = $this->classResolver->getInstanceFromDefinition($class_or_service);
|
Chris@0
|
124
|
Chris@0
|
125 return [$controller, $method];
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * {@inheritdoc}
|
Chris@0
|
130 */
|
Chris@0
|
131 protected function doGetArguments(Request $request, $controller, array $parameters) {
|
Chris@0
|
132 $attributes = $request->attributes->all();
|
Chris@0
|
133 $raw_parameters = $request->attributes->has('_raw_variables') ? $request->attributes->get('_raw_variables') : [];
|
Chris@0
|
134 $arguments = [];
|
Chris@0
|
135 foreach ($parameters as $param) {
|
Chris@0
|
136 if (array_key_exists($param->name, $attributes)) {
|
Chris@0
|
137 $arguments[] = $attributes[$param->name];
|
Chris@0
|
138 }
|
Chris@0
|
139 elseif (array_key_exists($param->name, $raw_parameters)) {
|
Chris@0
|
140 $arguments[] = $attributes[$param->name];
|
Chris@0
|
141 }
|
Chris@0
|
142 elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
|
Chris@0
|
143 $arguments[] = $request;
|
Chris@0
|
144 }
|
Chris@0
|
145 elseif ($param->getClass() && $param->getClass()->name === ServerRequestInterface::class) {
|
Chris@0
|
146 $arguments[] = $this->httpMessageFactory->createRequest($request);
|
Chris@0
|
147 }
|
Chris@0
|
148 elseif ($param->getClass() && ($param->getClass()->name == RouteMatchInterface::class || is_subclass_of($param->getClass()->name, RouteMatchInterface::class))) {
|
Chris@0
|
149 $arguments[] = RouteMatch::createFromRequest($request);
|
Chris@0
|
150 }
|
Chris@0
|
151 elseif ($param->isDefaultValueAvailable()) {
|
Chris@0
|
152 $arguments[] = $param->getDefaultValue();
|
Chris@0
|
153 }
|
Chris@0
|
154 else {
|
Chris@0
|
155 if (is_array($controller)) {
|
Chris@0
|
156 $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
|
Chris@0
|
157 }
|
Chris@0
|
158 elseif (is_object($controller)) {
|
Chris@0
|
159 $repr = get_class($controller);
|
Chris@0
|
160 }
|
Chris@0
|
161 else {
|
Chris@0
|
162 $repr = $controller;
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 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
|
166 }
|
Chris@0
|
167 }
|
Chris@0
|
168 return $arguments;
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 }
|