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