1
|
|
|
<?php |
2
|
|
|
namespace Strontium\PjaxBundle\Twig; |
3
|
|
|
|
4
|
|
|
use Strontium\PjaxBundle\Helper\PjaxHelperInterface; |
5
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
8
|
|
|
|
9
|
|
|
class PjaxExtension extends \Twig_Extension |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var PjaxHelperInterface |
14
|
|
|
*/ |
15
|
|
|
protected $pjax; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $sections = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var RequestStack |
24
|
|
|
*/ |
25
|
|
|
protected $requestStack; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param PjaxHelperInterface $pjax |
29
|
|
|
* @param RequestStack $requestStack |
30
|
|
|
*/ |
31
|
|
|
public function __construct(PjaxHelperInterface $pjax, RequestStack $requestStack) |
32
|
|
|
{ |
33
|
|
|
$this->pjax = $pjax; |
34
|
|
|
$this->requestStack = $requestStack; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
|
|
public function getFunctions() |
41
|
|
|
{ |
42
|
|
|
return array( |
43
|
|
|
new \Twig_SimpleFunction('is_pjax', [$this, 'isPjax'], ['is_safe' => ['html']]), |
44
|
|
|
new \Twig_SimpleFunction('pjax_attr', [$this, 'generatePjaxAttributes'], ['is_safe' => ['html']]), |
45
|
|
|
new \Twig_SimpleFunction('pjax_version', [$this, 'pjaxVersion'], ['is_safe' => ['html']]), |
46
|
|
|
new \Twig_SimpleFunction('pjax_target', [$this, 'getPjaxTarget'], ['is_safe' => ['html']]), |
47
|
|
|
new \Twig_SimpleFunction('pjax_layout', [$this, 'getLayout'], ['is_safe' => ['html']]), |
48
|
|
|
new \Twig_SimpleFunction('pjax_base', [$this, 'getBase'], ['is_safe' => ['html']]), |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function getFilters() |
56
|
|
|
{ |
57
|
|
|
return array( |
58
|
|
|
new \Twig_SimpleFilter('to_attr', [$this, 'toAttributes'], ['is_safe' => ['html']]), |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getLayout($section = 'default', $layout = null) |
66
|
|
|
{ |
67
|
|
|
$sectionConfig = $this->getSectionConfig($section); |
68
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
69
|
|
|
|
70
|
|
|
if (null !== $layout) { |
71
|
|
|
return $sectionConfig['layouts'][$layout]; |
72
|
|
|
} |
73
|
|
|
if ($this->pjax->isPjaxRequest($request)) { |
|
|
|
|
74
|
|
|
$target = $this->pjax->getTarget($request); |
|
|
|
|
75
|
|
|
if (isset($sectionConfig['layouts'][$target])) { |
76
|
|
|
return $sectionConfig['layouts'][$target]; |
77
|
|
|
} else { |
78
|
|
|
return $sectionConfig['layouts'][$sectionConfig['default_layout']]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $sectionConfig['layouts'][$sectionConfig['default_layout']]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $section |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getBase($section = 'default') |
91
|
|
|
{ |
92
|
|
|
$sectionConfig = $this->getSectionConfig($section); |
93
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
94
|
|
|
if ((null !== $request && $this->pjax->isPjaxRequest($request)) |
95
|
|
|
|| null !== $this->requestStack->getParentRequest() |
96
|
|
|
) { |
97
|
|
|
return $sectionConfig['pjax_template']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $sectionConfig['base_template']; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $sections |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function setSections(array $sections) |
109
|
|
|
{ |
110
|
|
|
$this->sections = $sections; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param Request $request |
117
|
|
|
* |
118
|
|
|
* @return string|null |
119
|
|
|
*/ |
120
|
|
|
public function pjaxVersion(Request $request) |
121
|
|
|
{ |
122
|
|
|
$version = $this->pjax->generateVersion($request); |
123
|
|
|
|
124
|
|
|
return $version ? sprintf('<meta http-equiv="x-pjax-version" content="%s"/>', $version) : null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Convert array to html attributes |
129
|
|
|
* |
130
|
|
|
* @param array $attributes |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function toAttributes(array $attributes = array()) |
135
|
|
|
{ |
136
|
|
|
$htmlAttr = []; |
137
|
|
|
|
138
|
|
|
foreach ($attributes as $key => $value) { |
139
|
|
|
if (is_bool($value)) { |
140
|
|
|
$value = $value ? 'true' : 'false'; |
141
|
|
|
} |
142
|
|
|
$htmlAttr[] = sprintf(' %s="%s"', $key, $value); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!count($htmlAttr)) { |
146
|
|
|
return ''; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return implode('', $htmlAttr); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Has Request been made by PJAX? |
154
|
|
|
* |
155
|
|
|
* @param Request $request |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function isPjax(Request $request) |
160
|
|
|
{ |
161
|
|
|
return $this->pjax->isPjaxRequest($request); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param Request $request |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function getPjaxTarget(Request $request) |
170
|
|
|
{ |
171
|
|
|
return $this->pjax->getTarget($request); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Generate PJAX attributes |
176
|
|
|
* |
177
|
|
|
* @param string $target data-pjax-container="$target" where content will load |
178
|
|
|
* @param string $redirectTarget data-pjax-redirect-target="$redirectTarget" where content will load after redirect |
179
|
|
|
* |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
public function generatePjaxAttributes($target = null, $redirectTarget = null) |
183
|
|
|
{ |
184
|
|
|
$attributes = []; |
185
|
|
|
if (null !== $target) { |
186
|
|
|
$attributes['data-pjax'] = (string)$target; |
187
|
|
|
} |
188
|
|
|
if (null !== $redirectTarget) { |
189
|
|
|
$attributes['data-pjax-redirect-target'] = (string)$redirectTarget; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $attributes; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritDoc} |
197
|
|
|
*/ |
198
|
|
|
public function getName() |
199
|
|
|
{ |
200
|
|
|
return 'pjax_extension'; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $section |
205
|
|
|
* |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
protected function getSectionConfig($section) |
209
|
|
|
{ |
210
|
|
|
if (!isset($this->sections[$section])) { |
211
|
|
|
throw new InvalidConfigurationException(sprintf('Section "%s" does not configured', $section)); |
212
|
|
|
} |
213
|
|
|
$sectionConfig = $this->sections[$section]; |
214
|
|
|
|
215
|
|
|
return $sectionConfig; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: