Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

HIncludeFragmentRenderer.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 5.66 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\HttpKernel\Fragment;
013   
014  use Symfony\Component\HttpFoundation\Request;
015  use Symfony\Component\HttpFoundation\Response;
016  use Symfony\Component\Templating\EngineInterface;
017  use Symfony\Component\HttpKernel\Controller\ControllerReference;
018  use Symfony\Component\HttpKernel\UriSigner;
019   
020  /**
021   * Implements the Hinclude rendering strategy.
022   *
023   * @author Fabien Potencier <fabien@symfony.com>
024   */
025  class HIncludeFragmentRenderer extends RoutableFragmentRenderer
026  {
027      private $globalDefaultTemplate;
028      private $signer;
029      private $templating;
030      private $charset;
031   
032      /**
033       * Constructor.
034       *
035       * @param EngineInterface|\Twig_Environment $templating            An EngineInterface or a \Twig_Environment instance
036       * @param UriSigner                         $signer                A UriSigner instance
037       * @param string                            $globalDefaultTemplate The global default content (it can be a template name or the content)
038       * @param string                            $charset
039       */
040      public function __construct($templating = null, UriSigner $signer = null, $globalDefaultTemplate = null, $charset = 'utf-8')
041      {
042          $this->setTemplating($templating);
043          $this->globalDefaultTemplate = $globalDefaultTemplate;
044          $this->signer = $signer;
045          $this->charset = $charset;
046      }
047   
048      /**
049       * Sets the templating engine to use to render the default content.
050       *
051       * @param EngineInterface|\Twig_Environment|null $templating An EngineInterface or a \Twig_Environment instance
052       *
053       * @throws \InvalidArgumentException
054       */
055      public function setTemplating($templating)
056      {
057          if (null !== $templating && !$templating instanceof EngineInterface && !$templating instanceof \Twig_Environment) {
058              throw new \InvalidArgumentException('The hinclude rendering strategy needs an instance of \Twig_Environment or Symfony\Component\Templating\EngineInterface');
059          }
060   
061          $this->templating = $templating;
062      }
063   
064      /**
065       * Checks if a templating engine has been set.
066       *
067       * @return bool true if the templating engine has been set, false otherwise
068       */
069      public function hasTemplating()
070      {
071          return null !== $this->templating;
072      }
073   
074      /**
075       * {@inheritdoc}
076       *
077       * Additional available options:
078       *
079       *  * default:    The default content (it can be a template name or the content)
080       *  * id:         An optional hx:include tag id attribute
081       *  * attributes: An optional array of hx:include tag attributes
082       */
083      public function render($uri, Request $request, array $options = array())
084      {
085          if ($uri instanceof ControllerReference) {
086              if (null === $this->signer) {
087                  throw new \LogicException('You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.');
088              }
089   
090              // we need to sign the absolute URI, but want to return the path only.
091              $uri = substr($this->signer->sign($this->generateFragmentUri($uri, $request, true)), strlen($request->getSchemeAndHttpHost()));
092          }
093   
094          // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
095          $uri = str_replace('&', '&amp;', $uri);
096   
097          $template = isset($options['default']) ? $options['default'] : $this->globalDefaultTemplate;
098          if (null !== $this->templating && $template && $this->templateExists($template)) {
099              $content = $this->templating->render($template);
100          } else {
101              $content = $template;
102          }
103   
104          $attributes = isset($options['attributes']) && is_array($options['attributes']) ? $options['attributes'] : array();
105          if (isset($options['id']) && $options['id']) {
106              $attributes['id'] = $options['id'];
107          }
108          $renderedAttributes = '';
109          if (count($attributes) > 0) {
110              if (PHP_VERSION_ID >= 50400) {
111                  $flags = ENT_QUOTES | ENT_SUBSTITUTE;
112              } else {
113                  $flags = ENT_QUOTES;
114              }
115              foreach ($attributes as $attribute => $value) {
116                  $renderedAttributes .= sprintf(
117                      ' %s="%s"',
118                      htmlspecialchars($attribute, $flags, $this->charset, false),
119                      htmlspecialchars($value, $flags, $this->charset, false)
120                  );
121              }
122          }
123   
124          return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
125      }
126   
127      /**
128       * @param string $template
129       *
130       * @return bool
131       */
132      private function templateExists($template)
133      {
134          if ($this->templating instanceof EngineInterface) {
135              try {
136                  return $this->templating->exists($template);
137              } catch (\InvalidArgumentException $e) {
138                  return false;
139              }
140          }
141   
142          $loader = $this->templating->getLoader();
143          if ($loader instanceof \Twig_ExistsLoaderInterface || method_exists($loader, 'exists')) {
144              return $loader->exists($template);
145          }
146   
147          try {
148              if (method_exists($loader, 'getSourceContext')) {
149                  $loader->getSourceContext($template);
150              } else {
151                  $loader->getSource($template);
152              }
153   
154              return true;
155          } catch (\Twig_Error_Loader $e) {
156          }
157   
158          return false;
159      }
160   
161      /**
162       * {@inheritdoc}
163       */
164      public function getName()
165      {
166          return 'hinclude';
167      }
168  }
169