Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

HttpKernel.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 7.93 KiB


 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\NotFoundHttpException;
 016  use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
 017  use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
 018  use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
 019  use Symfony\Component\HttpKernel\Event\GetResponseEvent;
 020  use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
 021  use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
 022  use Symfony\Component\HttpKernel\Event\PostResponseEvent;
 023  use Symfony\Component\HttpFoundation\Request;
 024  use Symfony\Component\HttpFoundation\Response;
 025  use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 026   
 027  /**
 028   * HttpKernel notifies events to convert a Request object to a Response one.
 029   *
 030   * @author Fabien Potencier <fabien@symfony.com>
 031   *
 032   * @api
 033   */
 034  class HttpKernel implements HttpKernelInterface, TerminableInterface
 035  {
 036      protected $dispatcher;
 037      protected $resolver;
 038   
 039      /**
 040       * Constructor
 041       *
 042       * @param EventDispatcherInterface    $dispatcher An EventDispatcherInterface instance
 043       * @param ControllerResolverInterface $resolver   A ControllerResolverInterface instance
 044       *
 045       * @api
 046       */
 047      public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver)
 048      {
 049          $this->dispatcher = $dispatcher;
 050          $this->resolver = $resolver;
 051      }
 052   
 053      /**
 054       * {@inheritdoc}
 055       *
 056       * @api
 057       */
 058      public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
 059      {
 060          try {
 061              return $this->handleRaw($request, $type);
 062          } catch (\Exception $e) {
 063              if (false === $catch) {
 064                  throw $e;
 065              }
 066   
 067              return $this->handleException($e, $request, $type);
 068          }
 069      }
 070   
 071      /**
 072       * {@inheritdoc}
 073       *
 074       * @api
 075       */
 076      public function terminate(Request $request, Response $response)
 077      {
 078          $this->dispatcher->dispatch(KernelEvents::TERMINATE, new PostResponseEvent($this, $request, $response));
 079      }
 080   
 081      /**
 082       * Handles a request to convert it to a response.
 083       *
 084       * Exceptions are not caught.
 085       *
 086       * @param Request $request A Request instance
 087       * @param int     $type    The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
 088       *
 089       * @return Response A Response instance
 090       *
 091       * @throws \LogicException If one of the listener does not behave as expected
 092       * @throws NotFoundHttpException When controller cannot be found
 093       */
 094      private function handleRaw(Request $request, $type = self::MASTER_REQUEST)
 095      {
 096          // request
 097          $event = new GetResponseEvent($this, $request, $type);
 098          $this->dispatcher->dispatch(KernelEvents::REQUEST, $event);
 099   
 100          if ($event->hasResponse()) {
 101              return $this->filterResponse($event->getResponse(), $request, $type);
 102          }
 103   
 104          // load controller
 105          if (false === $controller = $this->resolver->getController($request)) {
 106              throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". Maybe you forgot to add the matching route in your routing configuration?', $request->getPathInfo()));
 107          }
 108   
 109          $event = new FilterControllerEvent($this, $controller, $request, $type);
 110          $this->dispatcher->dispatch(KernelEvents::CONTROLLER, $event);
 111          $controller = $event->getController();
 112   
 113          // controller arguments
 114          $arguments = $this->resolver->getArguments($request, $controller);
 115   
 116          // call controller
 117          $response = call_user_func_array($controller, $arguments);
 118   
 119          // view
 120          if (!$response instanceof Response) {
 121              $event = new GetResponseForControllerResultEvent($this, $request, $type, $response);
 122              $this->dispatcher->dispatch(KernelEvents::VIEW, $event);
 123   
 124              if ($event->hasResponse()) {
 125                  $response = $event->getResponse();
 126              }
 127   
 128              if (!$response instanceof Response) {
 129                  $msg = sprintf('The controller must return a response (%s given).', $this->varToString($response));
 130   
 131                  // the user may have forgotten to return something
 132                  if (null === $response) {
 133                      $msg .= ' Did you forget to add a return statement somewhere in your controller?';
 134                  }
 135                  throw new \LogicException($msg);
 136              }
 137          }
 138   
 139          return $this->filterResponse($response, $request, $type);
 140      }
 141   
 142      /**
 143       * Filters a response object.
 144       *
 145       * @param Response $response A Response instance
 146       * @param Request  $request  An error message in case the response is not a Response object
 147       * @param int      $type     The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
 148       *
 149       * @return Response The filtered Response instance
 150       *
 151       * @throws \RuntimeException if the passed object is not a Response instance
 152       */
 153      private function filterResponse(Response $response, Request $request, $type)
 154      {
 155          $event = new FilterResponseEvent($this, $request, $type, $response);
 156   
 157          $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);
 158   
 159          return $event->getResponse();
 160      }
 161   
 162      /**
 163       * Handles an exception by trying to convert it to a Response.
 164       *
 165       * @param \Exception $e       An \Exception instance
 166       * @param Request    $request A Request instance
 167       * @param int        $type    The type of the request
 168       *
 169       * @return Response A Response instance
 170       *
 171       * @throws \Exception
 172       */
 173      private function handleException(\Exception $e, $request, $type)
 174      {
 175          $event = new GetResponseForExceptionEvent($this, $request, $type, $e);
 176          $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
 177   
 178          // a listener might have replaced the exception
 179          $e = $event->getException();
 180   
 181          if (!$event->hasResponse()) {
 182              throw $e;
 183          }
 184   
 185          $response = $event->getResponse();
 186   
 187          // the developer asked for a specific status code
 188          if ($response->headers->has('X-Status-Code')) {
 189              $response->setStatusCode($response->headers->get('X-Status-Code'));
 190   
 191              $response->headers->remove('X-Status-Code');
 192          } elseif (!$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
 193              // ensure that we actually have an error response
 194              if ($e instanceof HttpExceptionInterface) {
 195                  // keep the HTTP status code and headers
 196                  $response->setStatusCode($e->getStatusCode());
 197                  $response->headers->add($e->getHeaders());
 198              } else {
 199                  $response->setStatusCode(500);
 200              }
 201          }
 202   
 203          try {
 204              return $this->filterResponse($response, $request, $type);
 205          } catch (\Exception $e) {
 206              return $response;
 207          }
 208      }
 209   
 210      private function varToString($var)
 211      {
 212          if (is_object($var)) {
 213              return sprintf('Object(%s)', get_class($var));
 214          }
 215   
 216          if (is_array($var)) {
 217              $a = array();
 218              foreach ($var as $k => $v) {
 219                  $a[] = sprintf('%s => %s', $k, $this->varToString($v));
 220              }
 221   
 222              return sprintf("Array(%s)", implode(', ', $a));
 223          }
 224   
 225          if (is_resource($var)) {
 226              return sprintf('Resource(%s)', get_resource_type($var));
 227          }
 228   
 229          if (null === $var) {
 230              return 'null';
 231          }
 232   
 233          if (false === $var) {
 234              return 'false';
 235          }
 236   
 237          if (true === $var) {
 238              return 'true';
 239          }
 240   
 241          return (string) $var;
 242      }
 243  }
 244