AbstractSurrogateFragmentRenderer.php
1 year ago
EsiFragmentRenderer.php
2 years ago
FragmentHandler.php
1 year ago
FragmentRendererInterface.php
2 years ago
FragmentUriGenerator.php
1 year ago
FragmentUriGeneratorInterface.php
1 year ago
HIncludeFragmentRenderer.php
1 year ago
InlineFragmentRenderer.php
1 year ago
RoutableFragmentRenderer.php
1 year ago
SsiFragmentRenderer.php
2 years ago
HIncludeFragmentRenderer.php
91 lines
| 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 | namespace Matomo\Dependencies\Symfony\Component\HttpKernel\Fragment; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Request; |
| 14 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Response; |
| 15 | use Matomo\Dependencies\Symfony\Component\HttpKernel\Controller\ControllerReference; |
| 16 | use Matomo\Dependencies\Symfony\Component\HttpKernel\UriSigner; |
| 17 | use Matomo\Dependencies\Twig\Environment; |
| 18 | /** |
| 19 | * Implements the Hinclude rendering strategy. |
| 20 | * |
| 21 | * @author Fabien Potencier <fabien@symfony.com> |
| 22 | */ |
| 23 | class HIncludeFragmentRenderer extends RoutableFragmentRenderer |
| 24 | { |
| 25 | private $globalDefaultTemplate; |
| 26 | private $signer; |
| 27 | private $twig; |
| 28 | private $charset; |
| 29 | /** |
| 30 | * @param string|null $globalDefaultTemplate The global default content (it can be a template name or the content) |
| 31 | */ |
| 32 | public function __construct(?Environment $twig = null, ?UriSigner $signer = null, ?string $globalDefaultTemplate = null, string $charset = 'utf-8') |
| 33 | { |
| 34 | $this->twig = $twig; |
| 35 | $this->globalDefaultTemplate = $globalDefaultTemplate; |
| 36 | $this->signer = $signer; |
| 37 | $this->charset = $charset; |
| 38 | } |
| 39 | /** |
| 40 | * Checks if a templating engine has been set. |
| 41 | * |
| 42 | * @return bool |
| 43 | */ |
| 44 | public function hasTemplating() |
| 45 | { |
| 46 | return null !== $this->twig; |
| 47 | } |
| 48 | /** |
| 49 | * {@inheritdoc} |
| 50 | * |
| 51 | * Additional available options: |
| 52 | * |
| 53 | * * default: The default content (it can be a template name or the content) |
| 54 | * * id: An optional hx:include tag id attribute |
| 55 | * * attributes: An optional array of hx:include tag attributes |
| 56 | */ |
| 57 | public function render($uri, Request $request, array $options = []) |
| 58 | { |
| 59 | if ($uri instanceof ControllerReference) { |
| 60 | $uri = (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request); |
| 61 | } |
| 62 | // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content. |
| 63 | $uri = str_replace('&', '&', $uri); |
| 64 | $template = $options['default'] ?? $this->globalDefaultTemplate; |
| 65 | if (null !== $this->twig && $template && $this->twig->getLoader()->exists($template)) { |
| 66 | $content = $this->twig->render($template); |
| 67 | } else { |
| 68 | $content = $template; |
| 69 | } |
| 70 | $attributes = isset($options['attributes']) && \is_array($options['attributes']) ? $options['attributes'] : []; |
| 71 | if (isset($options['id']) && $options['id']) { |
| 72 | $attributes['id'] = $options['id']; |
| 73 | } |
| 74 | $renderedAttributes = ''; |
| 75 | if (\count($attributes) > 0) { |
| 76 | $flags = \ENT_QUOTES | \ENT_SUBSTITUTE; |
| 77 | foreach ($attributes as $attribute => $value) { |
| 78 | $renderedAttributes .= sprintf(' %s="%s"', htmlspecialchars($attribute, $flags, $this->charset, \false), htmlspecialchars($value, $flags, $this->charset, \false)); |
| 79 | } |
| 80 | } |
| 81 | return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content)); |
| 82 | } |
| 83 | /** |
| 84 | * {@inheritdoc} |
| 85 | */ |
| 86 | public function getName() |
| 87 | { |
| 88 | return 'hinclude'; |
| 89 | } |
| 90 | } |
| 91 |