annotate vendor/symfony/http-kernel/Fragment/HIncludeFragmentRenderer.php @ 19:fa3358dc1485 tip

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