Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
HIncludeFragmentRenderer.php
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\HttpKernel\Controller\ControllerReference;
017 use Symfony\Component\HttpKernel\UriSigner;
018 use Symfony\Component\Templating\EngineInterface;
019 use Twig\Environment;
020 use Twig\Error\LoaderError;
021 use Twig\Loader\ExistsLoaderInterface;
022 use Twig\Loader\SourceContextLoaderInterface;
023
024 /**
025 * Implements the Hinclude rendering strategy.
026 *
027 * @author Fabien Potencier <fabien@symfony.com>
028 */
029 class HIncludeFragmentRenderer extends RoutableFragmentRenderer
030 {
031 private $globalDefaultTemplate;
032 private $signer;
033 private $templating;
034 private $charset;
035
036 /**
037 * @param EngineInterface|Environment $templating An EngineInterface or a Twig instance
038 * @param UriSigner $signer A UriSigner instance
039 * @param string $globalDefaultTemplate The global default content (it can be a template name or the content)
040 * @param string $charset
041 */
042 public function __construct($templating = null, UriSigner $signer = null, $globalDefaultTemplate = null, $charset = 'utf-8')
043 {
044 $this->setTemplating($templating);
045 $this->globalDefaultTemplate = $globalDefaultTemplate;
046 $this->signer = $signer;
047 $this->charset = $charset;
048 }
049
050 /**
051 * Sets the templating engine to use to render the default content.
052 *
053 * @param EngineInterface|Environment|null $templating An EngineInterface or an Environment instance
054 *
055 * @throws \InvalidArgumentException
056 */
057 public function setTemplating($templating)
058 {
059 if (null !== $templating && !$templating instanceof EngineInterface && !$templating instanceof Environment) {
060 throw new \InvalidArgumentException('The hinclude rendering strategy needs an instance of Twig\Environment or Symfony\Component\Templating\EngineInterface.');
061 }
062
063 $this->templating = $templating;
064 }
065
066 /**
067 * Checks if a templating engine has been set.
068 *
069 * @return bool true if the templating engine has been set, false otherwise
070 */
071 public function hasTemplating()
072 {
073 return null !== $this->templating;
074 }
075
076 /**
077 * {@inheritdoc}
078 *
079 * Additional available options:
080 *
081 * * default: The default content (it can be a template name or the content)
082 * * id: An optional hx:include tag id attribute
083 * * attributes: An optional array of hx:include tag attributes
084 */
085 public function render($uri, Request $request, array $options = [])
086 {
087 if ($uri instanceof ControllerReference) {
088 if (null === $this->signer) {
089 throw new \LogicException('You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.');
090 }
091
092 // we need to sign the absolute URI, but want to return the path only.
093 $uri = substr($this->signer->sign($this->generateFragmentUri($uri, $request, true)), \strlen($request->getSchemeAndHttpHost()));
094 }
095
096 // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
097 $uri = str_replace('&', '&', $uri);
098
099 $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 }
168