Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/http-kernel/HttpKernel.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of the Symfony package. | |
5 * | |
6 * (c) Fabien Potencier <fabien@symfony.com> | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Symfony\Component\HttpKernel; | |
13 | |
14 use Symfony\Component\HttpKernel\Controller\ArgumentResolver; | |
15 use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface; | |
16 use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; | |
17 use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent; | |
18 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | |
19 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
20 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; | |
21 use Symfony\Component\HttpKernel\Event\FilterControllerEvent; | |
22 use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | |
23 use Symfony\Component\HttpKernel\Event\FinishRequestEvent; | |
24 use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
25 use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; | |
26 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; | |
27 use Symfony\Component\HttpKernel\Event\PostResponseEvent; | |
28 use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException; | |
29 use Symfony\Component\HttpFoundation\Request; | |
30 use Symfony\Component\HttpFoundation\RequestStack; | |
31 use Symfony\Component\HttpFoundation\Response; | |
32 use Symfony\Component\EventDispatcher\EventDispatcherInterface; | |
33 | |
34 /** | |
35 * HttpKernel notifies events to convert a Request object to a Response one. | |
36 * | |
37 * @author Fabien Potencier <fabien@symfony.com> | |
38 */ | |
39 class HttpKernel implements HttpKernelInterface, TerminableInterface | |
40 { | |
41 protected $dispatcher; | |
42 protected $resolver; | |
43 protected $requestStack; | |
44 private $argumentResolver; | |
45 | |
46 public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver, RequestStack $requestStack = null, ArgumentResolverInterface $argumentResolver = null) | |
47 { | |
48 $this->dispatcher = $dispatcher; | |
49 $this->resolver = $resolver; | |
50 $this->requestStack = $requestStack ?: new RequestStack(); | |
51 $this->argumentResolver = $argumentResolver; | |
52 | |
53 if (null === $this->argumentResolver) { | |
54 @trigger_error(sprintf('As of 3.1 an %s is used to resolve arguments. In 4.0 the $argumentResolver becomes the %s if no other is provided instead of using the $resolver argument.', ArgumentResolverInterface::class, ArgumentResolver::class), E_USER_DEPRECATED); | |
55 // fallback in case of deprecations | |
56 $this->argumentResolver = $resolver; | |
57 } | |
58 } | |
59 | |
60 /** | |
61 * {@inheritdoc} | |
62 */ | |
63 public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) | |
64 { | |
65 $request->headers->set('X-Php-Ob-Level', ob_get_level()); | |
66 | |
67 try { | |
68 return $this->handleRaw($request, $type); | |
69 } catch (\Exception $e) { | |
70 if ($e instanceof ConflictingHeadersException) { | |
71 $e = new BadRequestHttpException('The request headers contain conflicting information regarding the origin of this request.', $e); | |
72 } | |
73 if (false === $catch) { | |
74 $this->finishRequest($request, $type); | |
75 | |
76 throw $e; | |
77 } | |
78 | |
79 return $this->handleException($e, $request, $type); | |
80 } | |
81 } | |
82 | |
83 /** | |
84 * {@inheritdoc} | |
85 */ | |
86 public function terminate(Request $request, Response $response) | |
87 { | |
88 $this->dispatcher->dispatch(KernelEvents::TERMINATE, new PostResponseEvent($this, $request, $response)); | |
89 } | |
90 | |
91 /** | |
92 * @throws \LogicException If the request stack is empty | |
93 * | |
94 * @internal | |
95 */ | |
96 public function terminateWithException(\Exception $exception) | |
97 { | |
98 if (!$request = $this->requestStack->getMasterRequest()) { | |
99 throw new \LogicException('Request stack is empty', 0, $exception); | |
100 } | |
101 | |
102 $response = $this->handleException($exception, $request, self::MASTER_REQUEST); | |
103 | |
104 $response->sendHeaders(); | |
105 $response->sendContent(); | |
106 | |
107 $this->terminate($request, $response); | |
108 } | |
109 | |
110 /** | |
111 * Handles a request to convert it to a response. | |
112 * | |
113 * Exceptions are not caught. | |
114 * | |
115 * @param Request $request A Request instance | |
116 * @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST) | |
117 * | |
118 * @return Response A Response instance | |
119 * | |
120 * @throws \LogicException If one of the listener does not behave as expected | |
121 * @throws NotFoundHttpException When controller cannot be found | |
122 */ | |
123 private function handleRaw(Request $request, $type = self::MASTER_REQUEST) | |
124 { | |
125 $this->requestStack->push($request); | |
126 | |
127 // request | |
128 $event = new GetResponseEvent($this, $request, $type); | |
129 $this->dispatcher->dispatch(KernelEvents::REQUEST, $event); | |
130 | |
131 if ($event->hasResponse()) { | |
132 return $this->filterResponse($event->getResponse(), $request, $type); | |
133 } | |
134 | |
135 // load controller | |
136 if (false === $controller = $this->resolver->getController($request)) { | |
137 throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo())); | |
138 } | |
139 | |
140 $event = new FilterControllerEvent($this, $controller, $request, $type); | |
141 $this->dispatcher->dispatch(KernelEvents::CONTROLLER, $event); | |
142 $controller = $event->getController(); | |
143 | |
144 // controller arguments | |
145 $arguments = $this->argumentResolver->getArguments($request, $controller); | |
146 | |
147 $event = new FilterControllerArgumentsEvent($this, $controller, $arguments, $request, $type); | |
148 $this->dispatcher->dispatch(KernelEvents::CONTROLLER_ARGUMENTS, $event); | |
149 $controller = $event->getController(); | |
150 $arguments = $event->getArguments(); | |
151 | |
152 // call controller | |
153 $response = call_user_func_array($controller, $arguments); | |
154 | |
155 // view | |
156 if (!$response instanceof Response) { | |
157 $event = new GetResponseForControllerResultEvent($this, $request, $type, $response); | |
158 $this->dispatcher->dispatch(KernelEvents::VIEW, $event); | |
159 | |
160 if ($event->hasResponse()) { | |
161 $response = $event->getResponse(); | |
162 } | |
163 | |
164 if (!$response instanceof Response) { | |
165 $msg = sprintf('The controller must return a response (%s given).', $this->varToString($response)); | |
166 | |
167 // the user may have forgotten to return something | |
168 if (null === $response) { | |
169 $msg .= ' Did you forget to add a return statement somewhere in your controller?'; | |
170 } | |
171 throw new \LogicException($msg); | |
172 } | |
173 } | |
174 | |
175 return $this->filterResponse($response, $request, $type); | |
176 } | |
177 | |
178 /** | |
179 * Filters a response object. | |
180 * | |
181 * @param Response $response A Response instance | |
182 * @param Request $request An error message in case the response is not a Response object | |
183 * @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST) | |
184 * | |
185 * @return Response The filtered Response instance | |
186 * | |
187 * @throws \RuntimeException if the passed object is not a Response instance | |
188 */ | |
189 private function filterResponse(Response $response, Request $request, $type) | |
190 { | |
191 $event = new FilterResponseEvent($this, $request, $type, $response); | |
192 | |
193 $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event); | |
194 | |
195 $this->finishRequest($request, $type); | |
196 | |
197 return $event->getResponse(); | |
198 } | |
199 | |
200 /** | |
201 * Publishes the finish request event, then pop the request from the stack. | |
202 * | |
203 * Note that the order of the operations is important here, otherwise | |
204 * operations such as {@link RequestStack::getParentRequest()} can lead to | |
205 * weird results. | |
206 * | |
207 * @param Request $request | |
208 * @param int $type | |
209 */ | |
210 private function finishRequest(Request $request, $type) | |
211 { | |
212 $this->dispatcher->dispatch(KernelEvents::FINISH_REQUEST, new FinishRequestEvent($this, $request, $type)); | |
213 $this->requestStack->pop(); | |
214 } | |
215 | |
216 /** | |
217 * Handles an exception by trying to convert it to a Response. | |
218 * | |
219 * @param \Exception $e An \Exception instance | |
220 * @param Request $request A Request instance | |
221 * @param int $type The type of the request | |
222 * | |
223 * @return Response A Response instance | |
224 * | |
225 * @throws \Exception | |
226 */ | |
227 private function handleException(\Exception $e, $request, $type) | |
228 { | |
229 $event = new GetResponseForExceptionEvent($this, $request, $type, $e); | |
230 $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event); | |
231 | |
232 // a listener might have replaced the exception | |
233 $e = $event->getException(); | |
234 | |
235 if (!$event->hasResponse()) { | |
236 $this->finishRequest($request, $type); | |
237 | |
238 throw $e; | |
239 } | |
240 | |
241 $response = $event->getResponse(); | |
242 | |
243 // the developer asked for a specific status code | |
244 if ($response->headers->has('X-Status-Code')) { | |
245 $response->setStatusCode($response->headers->get('X-Status-Code')); | |
246 | |
247 $response->headers->remove('X-Status-Code'); | |
248 } elseif (!$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) { | |
249 // ensure that we actually have an error response | |
250 if ($e instanceof HttpExceptionInterface) { | |
251 // keep the HTTP status code and headers | |
252 $response->setStatusCode($e->getStatusCode()); | |
253 $response->headers->add($e->getHeaders()); | |
254 } else { | |
255 $response->setStatusCode(500); | |
256 } | |
257 } | |
258 | |
259 try { | |
260 return $this->filterResponse($response, $request, $type); | |
261 } catch (\Exception $e) { | |
262 return $response; | |
263 } | |
264 } | |
265 | |
266 private function varToString($var) | |
267 { | |
268 if (is_object($var)) { | |
269 return sprintf('Object(%s)', get_class($var)); | |
270 } | |
271 | |
272 if (is_array($var)) { | |
273 $a = array(); | |
274 foreach ($var as $k => $v) { | |
275 $a[] = sprintf('%s => %s', $k, $this->varToString($v)); | |
276 } | |
277 | |
278 return sprintf('Array(%s)', implode(', ', $a)); | |
279 } | |
280 | |
281 if (is_resource($var)) { | |
282 return sprintf('Resource(%s)', get_resource_type($var)); | |
283 } | |
284 | |
285 if (null === $var) { | |
286 return 'null'; | |
287 } | |
288 | |
289 if (false === $var) { | |
290 return 'false'; | |
291 } | |
292 | |
293 if (true === $var) { | |
294 return 'true'; | |
295 } | |
296 | |
297 return (string) $var; | |
298 } | |
299 } |