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