[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Fragment/ -> HIncludeFragmentRenderer.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\HttpKernel\Controller\ControllerReference;
  17  use Symfony\Component\HttpKernel\UriSigner;
  18  use Symfony\Component\Templating\EngineInterface;
  19  use Twig\Environment;
  20  use Twig\Error\LoaderError;
  21  use Twig\Loader\ExistsLoaderInterface;
  22  
  23  /**
  24   * Implements the Hinclude rendering strategy.
  25   *
  26   * @author Fabien Potencier <fabien@symfony.com>
  27   */
  28  class HIncludeFragmentRenderer extends RoutableFragmentRenderer
  29  {
  30      private $globalDefaultTemplate;
  31      private $signer;
  32      private $templating;
  33      private $charset;
  34  
  35      /**
  36       * @param EngineInterface|Environment $templating            An EngineInterface or a Twig instance
  37       * @param UriSigner                   $signer                A UriSigner instance
  38       * @param string                      $globalDefaultTemplate The global default content (it can be a template name or the content)
  39       * @param string                      $charset
  40       */
  41      public function __construct($templating = null, UriSigner $signer = null, $globalDefaultTemplate = null, $charset = 'utf-8')
  42      {
  43          $this->setTemplating($templating);
  44          $this->globalDefaultTemplate = $globalDefaultTemplate;
  45          $this->signer = $signer;
  46          $this->charset = $charset;
  47      }
  48  
  49      /**
  50       * Sets the templating engine to use to render the default content.
  51       *
  52       * @param EngineInterface|Environment|null $templating An EngineInterface or an Environment instance
  53       *
  54       * @throws \InvalidArgumentException
  55       */
  56      public function setTemplating($templating)
  57      {
  58          if (null !== $templating && !$templating instanceof EngineInterface && !$templating instanceof Environment) {
  59              throw new \InvalidArgumentException('The hinclude rendering strategy needs an instance of Twig\Environment or Symfony\Component\Templating\EngineInterface');
  60          }
  61  
  62          $this->templating = $templating;
  63      }
  64  
  65      /**
  66       * Checks if a templating engine has been set.
  67       *
  68       * @return bool true if the templating engine has been set, false otherwise
  69       */
  70      public function hasTemplating()
  71      {
  72          return null !== $this->templating;
  73      }
  74  
  75      /**
  76       * {@inheritdoc}
  77       *
  78       * Additional available options:
  79       *
  80       *  * default:    The default content (it can be a template name or the content)
  81       *  * id:         An optional hx:include tag id attribute
  82       *  * attributes: An optional array of hx:include tag attributes
  83       */
  84      public function render($uri, Request $request, array $options = array())
  85      {
  86          if ($uri instanceof ControllerReference) {
  87              if (null === $this->signer) {
  88                  throw new \LogicException('You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.');
  89              }
  90  
  91              // we need to sign the absolute URI, but want to return the path only.
  92              $uri = substr($this->signer->sign($this->generateFragmentUri($uri, $request, true)), \strlen($request->getSchemeAndHttpHost()));
  93          }
  94  
  95          // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
  96          $uri = str_replace('&', '&amp;', $uri);
  97  
  98          $template = isset($options['default']) ? $options['default'] : $this->globalDefaultTemplate;
  99          if (null !== $this->templating && $template && $this->templateExists($template)) {
 100              $content = $this->templating->render($template);
 101          } else {
 102              $content = $template;
 103          }
 104  
 105          $attributes = isset($options['attributes']) && \is_array($options['attributes']) ? $options['attributes'] : array();
 106          if (isset($options['id']) && $options['id']) {
 107              $attributes['id'] = $options['id'];
 108          }
 109          $renderedAttributes = '';
 110          if (\count($attributes) > 0) {
 111              if (\PHP_VERSION_ID >= 50400) {
 112                  $flags = ENT_QUOTES | ENT_SUBSTITUTE;
 113              } else {
 114                  $flags = ENT_QUOTES;
 115              }
 116              foreach ($attributes as $attribute => $value) {
 117                  $renderedAttributes .= sprintf(
 118                      ' %s="%s"',
 119                      htmlspecialchars($attribute, $flags, $this->charset, false),
 120                      htmlspecialchars($value, $flags, $this->charset, false)
 121                  );
 122              }
 123          }
 124  
 125          return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
 126      }
 127  
 128      /**
 129       * @param string $template
 130       *
 131       * @return bool
 132       */
 133      private function templateExists($template)
 134      {
 135          if ($this->templating instanceof EngineInterface) {
 136              try {
 137                  return $this->templating->exists($template);
 138              } catch (\Exception $e) {
 139                  return false;
 140              }
 141          }
 142  
 143          $loader = $this->templating->getLoader();
 144          if ($loader instanceof ExistsLoaderInterface || method_exists($loader, 'exists')) {
 145              return $loader->exists($template);
 146          }
 147  
 148          try {
 149              if (method_exists($loader, 'getSourceContext')) {
 150                  $loader->getSourceContext($template);
 151              } else {
 152                  $loader->getSource($template);
 153              }
 154  
 155              return true;
 156          } catch (LoaderError $e) {
 157          }
 158  
 159          return false;
 160      }
 161  
 162      /**
 163       * {@inheritdoc}
 164       */
 165      public function getName()
 166      {
 167          return 'hinclude';
 168      }
 169  }


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