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