[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1