[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Fragment/ -> FragmentHandler.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\Fragment;
  13  
  14  use Symfony\Component\HttpFoundation\RequestStack;
  15  use Symfony\Component\HttpFoundation\Response;
  16  use Symfony\Component\HttpFoundation\StreamedResponse;
  17  use Symfony\Component\HttpKernel\Controller\ControllerReference;
  18  
  19  /**
  20   * Renders a URI that represents a resource fragment.
  21   *
  22   * This class handles the rendering of resource fragments that are included into
  23   * a main resource. The handling of the rendering is managed by specialized renderers.
  24   *
  25   * @author Fabien Potencier <fabien@symfony.com>
  26   *
  27   * @see FragmentRendererInterface
  28   */
  29  class FragmentHandler
  30  {
  31      private $debug;
  32      private $renderers = [];
  33      private $requestStack;
  34  
  35      /**
  36       * @param RequestStack                $requestStack The Request stack that controls the lifecycle of requests
  37       * @param FragmentRendererInterface[] $renderers    An array of FragmentRendererInterface instances
  38       * @param bool                        $debug        Whether the debug mode is enabled or not
  39       */
  40      public function __construct(RequestStack $requestStack, array $renderers = [], $debug = false)
  41      {
  42          $this->requestStack = $requestStack;
  43          foreach ($renderers as $renderer) {
  44              $this->addRenderer($renderer);
  45          }
  46          $this->debug = $debug;
  47      }
  48  
  49      /**
  50       * Adds a renderer.
  51       */
  52      public function addRenderer(FragmentRendererInterface $renderer)
  53      {
  54          $this->renderers[$renderer->getName()] = $renderer;
  55      }
  56  
  57      /**
  58       * Renders a URI and returns the Response content.
  59       *
  60       * Available options:
  61       *
  62       *  * ignore_errors: true to return an empty string in case of an error
  63       *
  64       * @param string|ControllerReference $uri      A URI as a string or a ControllerReference instance
  65       * @param string                     $renderer The renderer name
  66       * @param array                      $options  An array of options
  67       *
  68       * @return string|null The Response content or null when the Response is streamed
  69       *
  70       * @throws \InvalidArgumentException when the renderer does not exist
  71       * @throws \LogicException           when no master request is being handled
  72       */
  73      public function render($uri, $renderer = 'inline', array $options = [])
  74      {
  75          if (!isset($options['ignore_errors'])) {
  76              $options['ignore_errors'] = !$this->debug;
  77          }
  78  
  79          if (!isset($this->renderers[$renderer])) {
  80              throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
  81          }
  82  
  83          if (!$request = $this->requestStack->getCurrentRequest()) {
  84              throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
  85          }
  86  
  87          return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
  88      }
  89  
  90      /**
  91       * Delivers the Response as a string.
  92       *
  93       * When the Response is a StreamedResponse, the content is streamed immediately
  94       * instead of being returned.
  95       *
  96       * @return string|null The Response content or null when the Response is streamed
  97       *
  98       * @throws \RuntimeException when the Response is not successful
  99       */
 100      protected function deliver(Response $response)
 101      {
 102          if (!$response->isSuccessful()) {
 103              throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %d).', $this->requestStack->getCurrentRequest()->getUri(), $response->getStatusCode()));
 104          }
 105  
 106          if (!$response instanceof StreamedResponse) {
 107              return $response->getContent();
 108          }
 109  
 110          $response->sendContent();
 111  
 112          return null;
 113      }
 114  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1