[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/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\Request;
  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 $request;
  34  
  35      /**
  36       * Constructor.
  37       *
  38       * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
  39       * @param bool                        $debug     Whether the debug mode is enabled or not
  40       */
  41      public function __construct(array $renderers = array(), $debug = false)
  42      {
  43          $this->renderers = array();
  44          foreach ($renderers as $renderer) {
  45              $this->addRenderer($renderer);
  46          }
  47          $this->debug = $debug;
  48      }
  49  
  50      /**
  51       * Adds a renderer.
  52       *
  53       * @param FragmentRendererInterface $renderer A FragmentRendererInterface instance
  54       */
  55      public function addRenderer(FragmentRendererInterface $renderer)
  56      {
  57          $this->renderers[$renderer->getName()] = $renderer;
  58      }
  59  
  60      /**
  61       * Sets the current Request.
  62       *
  63       * @param Request $request The current Request
  64       */
  65      public function setRequest(Request $request = null)
  66      {
  67          $this->request = $request;
  68      }
  69  
  70      /**
  71       * Renders a URI and returns the Response content.
  72       *
  73       * Available options:
  74       *
  75       *  * ignore_errors: true to return an empty string in case of an error
  76       *
  77       * @param string|ControllerReference $uri      A URI as a string or a ControllerReference instance
  78       * @param string                     $renderer The renderer name
  79       * @param array                      $options  An array of options
  80       *
  81       * @return string|null The Response content or null when the Response is streamed
  82       *
  83       * @throws \InvalidArgumentException when the renderer does not exist
  84       * @throws \LogicException           when no master request is being handled
  85       * @throws \RuntimeException         when the Response is not successful
  86       */
  87      public function render($uri, $renderer = 'inline', array $options = array())
  88      {
  89          if (!isset($options['ignore_errors'])) {
  90              $options['ignore_errors'] = !$this->debug;
  91          }
  92  
  93          if (!isset($this->renderers[$renderer])) {
  94              throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
  95          }
  96  
  97          if (null === $this->request) {
  98              throw new \LogicException('Rendering a fragment can only be done when handling a master Request.');
  99          }
 100  
 101          return $this->deliver($this->renderers[$renderer]->render($uri, $this->request, $options));
 102      }
 103  
 104      /**
 105       * Delivers the Response as a string.
 106       *
 107       * When the Response is a StreamedResponse, the content is streamed immediately
 108       * instead of being returned.
 109       *
 110       * @param Response $response A Response instance
 111       *
 112       * @return string|null The Response content or null when the Response is streamed
 113       *
 114       * @throws \RuntimeException when the Response is not successful
 115       */
 116      protected function deliver(Response $response)
 117      {
 118          if (!$response->isSuccessful()) {
 119              throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->request->getUri(), $response->getStatusCode()));
 120          }
 121  
 122          if (!$response instanceof StreamedResponse) {
 123              return $response->getContent();
 124          }
 125  
 126          $response->sendContent();
 127      }
 128  }


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