[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/ -> HttpKernel.php (source)

   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\ControllerResolverInterface;
  15  use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16  use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  17  use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  18  use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  19  use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  20  use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  21  use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  22  use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  23  use Symfony\Component\HttpFoundation\Request;
  24  use Symfony\Component\HttpFoundation\Response;
  25  use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  26  
  27  /**
  28   * HttpKernel notifies events to convert a Request object to a Response one.
  29   *
  30   * @author Fabien Potencier <fabien@symfony.com>
  31   */
  32  class HttpKernel implements HttpKernelInterface, TerminableInterface
  33  {
  34      protected $dispatcher;
  35      protected $resolver;
  36  
  37      /**
  38       * Constructor.
  39       *
  40       * @param EventDispatcherInterface    $dispatcher An EventDispatcherInterface instance
  41       * @param ControllerResolverInterface $resolver   A ControllerResolverInterface instance
  42       */
  43      public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver)
  44      {
  45          $this->dispatcher = $dispatcher;
  46          $this->resolver = $resolver;
  47      }
  48  
  49      /**
  50       * {@inheritdoc}
  51       */
  52      public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  53      {
  54          try {
  55              return $this->handleRaw($request, $type);
  56          } catch (\Exception $e) {
  57              if (false === $catch) {
  58                  throw $e;
  59              }
  60  
  61              return $this->handleException($e, $request, $type);
  62          }
  63      }
  64  
  65      /**
  66       * {@inheritdoc}
  67       */
  68      public function terminate(Request $request, Response $response)
  69      {
  70          $this->dispatcher->dispatch(KernelEvents::TERMINATE, new PostResponseEvent($this, $request, $response));
  71      }
  72  
  73      /**
  74       * Handles a request to convert it to a response.
  75       *
  76       * Exceptions are not caught.
  77       *
  78       * @param Request $request A Request instance
  79       * @param int     $type    The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  80       *
  81       * @return Response A Response instance
  82       *
  83       * @throws \LogicException       If one of the listener does not behave as expected
  84       * @throws NotFoundHttpException When controller cannot be found
  85       */
  86      private function handleRaw(Request $request, $type = self::MASTER_REQUEST)
  87      {
  88          // request
  89          $event = new GetResponseEvent($this, $request, $type);
  90          $this->dispatcher->dispatch(KernelEvents::REQUEST, $event);
  91  
  92          if ($event->hasResponse()) {
  93              return $this->filterResponse($event->getResponse(), $request, $type);
  94          }
  95  
  96          // load controller
  97          if (false === $controller = $this->resolver->getController($request)) {
  98              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()));
  99          }
 100  
 101          $event = new FilterControllerEvent($this, $controller, $request, $type);
 102          $this->dispatcher->dispatch(KernelEvents::CONTROLLER, $event);
 103          $controller = $event->getController();
 104  
 105          // controller arguments
 106          $arguments = $this->resolver->getArguments($request, $controller);
 107  
 108          // call controller
 109          $response = call_user_func_array($controller, $arguments);
 110  
 111          // view
 112          if (!$response instanceof Response) {
 113              $event = new GetResponseForControllerResultEvent($this, $request, $type, $response);
 114              $this->dispatcher->dispatch(KernelEvents::VIEW, $event);
 115  
 116              if ($event->hasResponse()) {
 117                  $response = $event->getResponse();
 118              }
 119  
 120              if (!$response instanceof Response) {
 121                  $msg = sprintf('The controller must return a response (%s given).', $this->varToString($response));
 122  
 123                  // the user may have forgotten to return something
 124                  if (null === $response) {
 125                      $msg .= ' Did you forget to add a return statement somewhere in your controller?';
 126                  }
 127                  throw new \LogicException($msg);
 128              }
 129          }
 130  
 131          return $this->filterResponse($response, $request, $type);
 132      }
 133  
 134      /**
 135       * Filters a response object.
 136       *
 137       * @param Response $response A Response instance
 138       * @param Request  $request  An error message in case the response is not a Response object
 139       * @param int      $type     The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
 140       *
 141       * @return Response The filtered Response instance
 142       *
 143       * @throws \RuntimeException if the passed object is not a Response instance
 144       */
 145      private function filterResponse(Response $response, Request $request, $type)
 146      {
 147          $event = new FilterResponseEvent($this, $request, $type, $response);
 148  
 149          $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);
 150  
 151          return $event->getResponse();
 152      }
 153  
 154      /**
 155       * Handles an exception by trying to convert it to a Response.
 156       *
 157       * @param \Exception $e       An \Exception instance
 158       * @param Request    $request A Request instance
 159       * @param int        $type    The type of the request
 160       *
 161       * @return Response A Response instance
 162       *
 163       * @throws \Exception
 164       */
 165      private function handleException(\Exception $e, $request, $type)
 166      {
 167          $event = new GetResponseForExceptionEvent($this, $request, $type, $e);
 168          $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
 169  
 170          // a listener might have replaced the exception
 171          $e = $event->getException();
 172  
 173          if (!$event->hasResponse()) {
 174              throw $e;
 175          }
 176  
 177          $response = $event->getResponse();
 178  
 179          // the developer asked for a specific status code
 180          if ($response->headers->has('X-Status-Code')) {
 181              $response->setStatusCode($response->headers->get('X-Status-Code'));
 182  
 183              $response->headers->remove('X-Status-Code');
 184          } elseif (!$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
 185              // ensure that we actually have an error response
 186              if ($e instanceof HttpExceptionInterface) {
 187                  // keep the HTTP status code and headers
 188                  $response->setStatusCode($e->getStatusCode());
 189                  $response->headers->add($e->getHeaders());
 190              } else {
 191                  $response->setStatusCode(500);
 192              }
 193          }
 194  
 195          try {
 196              return $this->filterResponse($response, $request, $type);
 197          } catch (\Exception $e) {
 198              return $response;
 199          }
 200      }
 201  
 202      private function varToString($var)
 203      {
 204          if (is_object($var)) {
 205              return sprintf('Object(%s)', get_class($var));
 206          }
 207  
 208          if (is_array($var)) {
 209              $a = array();
 210              foreach ($var as $k => $v) {
 211                  $a[] = sprintf('%s => %s', $k, $this->varToString($v));
 212              }
 213  
 214              return sprintf('Array(%s)', implode(', ', $a));
 215          }
 216  
 217          if (is_resource($var)) {
 218              return sprintf('Resource(%s)', get_resource_type($var));
 219          }
 220  
 221          if (null === $var) {
 222              return 'null';
 223          }
 224  
 225          if (false === $var) {
 226              return 'false';
 227          }
 228  
 229          if (true === $var) {
 230              return 'true';
 231          }
 232  
 233          return (string) $var;
 234      }
 235  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1