[ Index ]

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


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