Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
HttpKernel.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\HttpKernel;
013
014 use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
015 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
016 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
017 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
018 use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
019 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
020 use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
021 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
022 use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
023 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
024 use Symfony\Component\HttpKernel\Event\PostResponseEvent;
025 use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
026 use Symfony\Component\HttpFoundation\Request;
027 use Symfony\Component\HttpFoundation\RequestStack;
028 use Symfony\Component\HttpFoundation\Response;
029 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
030
031 /**
032 * HttpKernel notifies events to convert a Request object to a Response one.
033 *
034 * @author Fabien Potencier <fabien@symfony.com>
035 */
036 class HttpKernel implements HttpKernelInterface, TerminableInterface
037 {
038 protected $dispatcher;
039 protected $resolver;
040 protected $requestStack;
041
042 /**
043 * Constructor.
044 *
045 * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
046 * @param ControllerResolverInterface $resolver A ControllerResolverInterface instance
047 * @param RequestStack $requestStack A stack for master/sub requests
048 */
049 public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver, RequestStack $requestStack = null)
050 {
051 $this->dispatcher = $dispatcher;
052 $this->resolver = $resolver;
053 $this->requestStack = $requestStack ?: new RequestStack();
054 }
055
056 /**
057 * {@inheritdoc}
058 */
059 public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
060 {
061 $request->headers->set('X-Php-Ob-Level', ob_get_level());
062
063 try {
064 return $this->handleRaw($request, $type);
065 } catch (\Exception $e) {
066 if ($e instanceof ConflictingHeadersException) {
067 $e = new BadRequestHttpException('The request headers contain conflicting information regarding the origin of this request.', $e);
068 }
069 if (false === $catch) {
070 $this->finishRequest($request, $type);
071
072 throw $e;
073 }
074
075 return $this->handleException($e, $request, $type);
076 }
077 }
078
079 /**
080 * {@inheritdoc}
081 */
082 public function terminate(Request $request, Response $response)
083 {
084 $this->dispatcher->dispatch(KernelEvents::TERMINATE, new PostResponseEvent($this, $request, $response));
085 }
086
087 /**
088 * @throws \LogicException If the request stack is empty
089 *
090 * @internal
091 */
092 public function terminateWithException(\Exception $exception)
093 {
094 if (!$request = $this->requestStack->getMasterRequest()) {
095 throw new \LogicException('Request stack is empty', 0, $exception);
096 }
097
098 $response = $this->handleException($exception, $request, self::MASTER_REQUEST);
099
100 $response->sendHeaders();
101 $response->sendContent();
102
103 $this->terminate($request, $response);
104 }
105
106 /**
107 * Handles a request to convert it to a response.
108 *
109 * Exceptions are not caught.
110 *
111 * @param Request $request A Request instance
112 * @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
113 *
114 * @return Response A Response instance
115 *
116 * @throws \LogicException If one of the listener does not behave as expected
117 * @throws NotFoundHttpException When controller cannot be found
118 */
119 private function handleRaw(Request $request, $type = self::MASTER_REQUEST)
120 {
121 $this->requestStack->push($request);
122
123 // request
124 $event = new GetResponseEvent($this, $request, $type);
125 $this->dispatcher->dispatch(KernelEvents::REQUEST, $event);
126
127 if ($event->hasResponse()) {
128 return $this->filterResponse($event->getResponse(), $request, $type);
129 }
130
131 // load controller
132 if (false === $controller = $this->resolver->getController($request)) {
133 throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
134 }
135
136 $event = new FilterControllerEvent($this, $controller, $request, $type);
137 $this->dispatcher->dispatch(KernelEvents::CONTROLLER, $event);
138 $controller = $event->getController();
139
140 // controller arguments
141 $arguments = $this->resolver->getArguments($request, $controller);
142
143 // call controller
144 $response = call_user_func_array($controller, $arguments);
145
146 // view
147 if (!$response instanceof Response) {
148 $event = new GetResponseForControllerResultEvent($this, $request, $type, $response);
149 $this->dispatcher->dispatch(KernelEvents::VIEW, $event);
150
151 if ($event->hasResponse()) {
152 $response = $event->getResponse();
153 }
154
155 if (!$response instanceof Response) {
156 $msg = sprintf('The controller must return a response (%s given).', $this->varToString($response));
157
158 // the user may have forgotten to return something
159 if (null === $response) {
160 $msg .= ' Did you forget to add a return statement somewhere in your controller?';
161 }
162 throw new \LogicException($msg);
163 }
164 }
165
166 return $this->filterResponse($response, $request, $type);
167 }
168
169 /**
170 * Filters a response object.
171 *
172 * @param Response $response A Response instance
173 * @param Request $request An error message in case the response is not a Response object
174 * @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
175 *
176 * @return Response The filtered Response instance
177 *
178 * @throws \RuntimeException if the passed object is not a Response instance
179 */
180 private function filterResponse(Response $response, Request $request, $type)
181 {
182 $event = new FilterResponseEvent($this, $request, $type, $response);
183
184 $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);
185
186 $this->finishRequest($request, $type);
187
188 return $event->getResponse();
189 }
190
191 /**
192 * Publishes the finish request event, then pop the request from the stack.
193 *
194 * Note that the order of the operations is important here, otherwise
195 * operations such as {@link RequestStack::getParentRequest()} can lead to
196 * weird results.
197 *
198 * @param Request $request
199 * @param int $type
200 */
201 private function finishRequest(Request $request, $type)
202 {
203 $this->dispatcher->dispatch(KernelEvents::FINISH_REQUEST, new FinishRequestEvent($this, $request, $type));
204 $this->requestStack->pop();
205 }
206
207 /**
208 * Handles an exception by trying to convert it to a Response.
209 *
210 * @param \Exception $e An \Exception instance
211 * @param Request $request A Request instance
212 * @param int $type The type of the request
213 *
214 * @return Response A Response instance
215 *
216 * @throws \Exception
217 */
218 private function handleException(\Exception $e, $request, $type)
219 {
220 $event = new GetResponseForExceptionEvent($this, $request, $type, $e);
221 $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
222
223 // a listener might have replaced the exception
224 $e = $event->getException();
225
226 if (!$event->hasResponse()) {
227 $this->finishRequest($request, $type);
228
229 throw $e;
230 }
231
232 $response = $event->getResponse();
233
234 // the developer asked for a specific status code
235 if ($response->headers->has('X-Status-Code')) {
236 $response->setStatusCode($response->headers->get('X-Status-Code'));
237
238 $response->headers->remove('X-Status-Code');
239 } elseif (!$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
240 // ensure that we actually have an error response
241 if ($e instanceof HttpExceptionInterface) {
242 // keep the HTTP status code and headers
243 $response->setStatusCode($e->getStatusCode());
244 $response->headers->add($e->getHeaders());
245 } else {
246 $response->setStatusCode(500);
247 }
248 }
249
250 try {
251 return $this->filterResponse($response, $request, $type);
252 } catch (\Exception $e) {
253 return $response;
254 }
255 }
256
257 private function varToString($var)
258 {
259 if (is_object($var)) {
260 return sprintf('Object(%s)', get_class($var));
261 }
262
263 if (is_array($var)) {
264 $a = array();
265 foreach ($var as $k => $v) {
266 $a[] = sprintf('%s => %s', $k, $this->varToString($v));
267 }
268
269 return sprintf('Array(%s)', implode(', ', $a));
270 }
271
272 if (is_resource($var)) {
273 return sprintf('Resource(%s)', get_resource_type($var));
274 }
275
276 if (null === $var) {
277 return 'null';
278 }
279
280 if (false === $var) {
281 return 'false';
282 }
283
284 if (true === $var) {
285 return 'true';
286 }
287
288 return (string) $var;
289 }
290 }
291