1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Meup TagCommander Bundle. |
5
|
|
|
* |
6
|
|
|
* (c) 1001pharmacies <http://github.com/1001pharmacies/tagcommander-bundle> |
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 Meup\Bundle\TagcommanderBundle\Twig; |
13
|
|
|
|
14
|
|
|
use Meup\Bundle\TagcommanderBundle\EventDispatcher\Event\DeployContainer; |
15
|
|
|
use Meup\Bundle\TagcommanderBundle\EventDispatcher\Event\Track; |
16
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
20
|
|
|
use Symfony\Component\Serializer\Serializer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
class TagcommanderExtension extends \Twig_Extension |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ParameterBagInterface |
29
|
|
|
*/ |
30
|
|
|
protected $datalayer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var EventDispatcherInterface |
34
|
|
|
*/ |
35
|
|
|
protected $dispatcher; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $tcVars; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $events = array(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $containers = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $defaultEvent = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Serializer |
59
|
|
|
*/ |
60
|
|
|
protected $serializer; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
static protected $twigFunctions = array( |
66
|
|
|
'tc_vars' => 'tcVars', |
67
|
|
|
'tc_container' => 'tcContainer', |
68
|
|
|
'tc_event' => 'tcEvent', |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param ParameterBagInterface $datalayer |
73
|
|
|
* @param EventDispatcherInterface $dispatcher |
74
|
|
|
* @param string $tcVars |
75
|
|
|
*/ |
76
|
1 |
|
public function __construct( |
77
|
|
|
ParameterBagInterface $datalayer, |
78
|
|
|
EventDispatcherInterface $dispatcher, |
79
|
|
|
$tcVars = 'tc_vars' |
80
|
|
|
) { |
81
|
1 |
|
$this->datalayer = $datalayer; |
82
|
1 |
|
$this->dispatcher = $dispatcher; |
83
|
1 |
|
$this->tcVars = $tcVars; |
84
|
1 |
|
$this->serializer = new Serializer( |
85
|
1 |
|
array(new ObjectNormalizer()), |
86
|
1 |
|
array(new JsonEncoder()) |
87
|
1 |
|
); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string|boolean|integer|double|null |
92
|
|
|
*/ |
93
|
1 |
|
protected function serializeWithValues($values = array()) |
94
|
|
|
{ |
95
|
1 |
|
$datalayer = clone $this->datalayer; |
96
|
|
|
|
97
|
1 |
|
return $this |
98
|
|
|
->serializer |
99
|
1 |
|
->serialize( |
100
|
1 |
|
array_merge( |
101
|
1 |
|
$datalayer->all(), |
102
|
|
|
$values |
103
|
1 |
|
), |
104
|
|
|
'json' |
105
|
1 |
|
) |
106
|
1 |
|
; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Array $values |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
1 |
|
public function tcVars($values = array()) |
114
|
|
|
{ |
115
|
1 |
|
return sprintf( |
116
|
1 |
|
'<script type="text/javascript" async>var %s = %s;</script>', |
117
|
1 |
|
$this->tcVars, |
118
|
1 |
|
$this->serializeWithValues($values) |
119
|
1 |
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $event |
124
|
|
|
* @param boolean $setAsDefault |
125
|
|
|
* @return self |
126
|
|
|
*/ |
127
|
1 |
|
public function addEvent($event, $setAsDefault = false) |
128
|
|
|
{ |
129
|
1 |
|
$this->events[$event['name']] = $event; |
130
|
|
|
|
131
|
1 |
|
if ($setAsDefault || (!$setAsDefault && !$this->defaultEvent)) { |
132
|
1 |
|
$this->defaultEvent = $event['name']; |
133
|
1 |
|
} |
134
|
|
|
|
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $eventName |
140
|
|
|
* @param array $values |
141
|
|
|
* @param string|null $tracker |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
1 |
|
public function tcEvent($eventName, $values = array(), $tracker = null) |
145
|
|
|
{ |
146
|
1 |
|
if (is_null($tracker)) { |
147
|
1 |
|
$tracker = $this->defaultEvent; |
148
|
1 |
|
} |
149
|
|
|
|
150
|
1 |
|
$function = $this->events[$tracker]['function']; |
151
|
|
|
|
152
|
1 |
|
$event = new Track($tracker, $eventName, array_merge($this->datalayer->all(), $values)); |
153
|
1 |
|
$this->dispatcher->dispatch('tc_event', $event); |
154
|
|
|
|
155
|
1 |
|
return sprintf( |
156
|
1 |
|
"%s('%s', %s);", |
157
|
1 |
|
$function, |
158
|
1 |
|
$eventName, |
159
|
1 |
|
$this->serializeWithValues($values) |
160
|
1 |
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param array $container |
165
|
|
|
* @return self |
166
|
|
|
*/ |
167
|
1 |
|
public function addContainer($container) |
168
|
|
|
{ |
169
|
1 |
|
$this->containers[$container['name']] = $container; |
170
|
|
|
|
171
|
1 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
private function containerHas($container, $parameterName) |
175
|
|
|
{ |
176
|
1 |
|
return array_key_exists($parameterName, $container) && !empty($container[$parameterName]); |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
private function buildSrc($container) |
180
|
|
|
{ |
181
|
1 |
|
$src = $container['script']; |
182
|
1 |
|
if ($this->containerHas($container, 'version')) { |
183
|
1 |
|
$src .= sprintf('?%s', $container['version']); |
184
|
1 |
|
} |
185
|
1 |
|
return sprintf('<script type="text/javascript" src="%s" async></script>', $src); |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
private function buildAlternative($container) |
189
|
|
|
{ |
190
|
1 |
|
$result = ''; |
191
|
1 |
|
if ($this->containerHas($container, 'alternative')) { |
192
|
1 |
|
$result .= sprintf( |
193
|
1 |
|
'<noscript><iframe src="%s" width="1" height="1" rel="noindex,nofollow" sandbox="%s"></iframe></noscript>', |
|
|
|
|
194
|
1 |
|
$container['alternative'], |
195
|
1 |
|
'allow-same-origin allow-scripts' |
196
|
1 |
|
); |
197
|
1 |
|
} |
198
|
|
|
return $result; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $containerName |
203
|
|
|
* @return string |
204
|
1 |
|
*/ |
205
|
|
|
public function tcContainer($containerName) |
206
|
1 |
|
{ |
207
|
|
|
$container = $this->containers[$containerName]; |
208
|
1 |
|
|
209
|
|
|
$this |
210
|
1 |
|
->dispatcher |
211
|
1 |
|
->dispatch( |
212
|
1 |
|
'tc_container', |
213
|
1 |
|
new DeployContainer( |
214
|
1 |
|
$containerName, |
215
|
1 |
|
$container['script'], |
216
|
1 |
|
$container['version'], |
217
|
1 |
|
$container['alternative'] |
218
|
1 |
|
) |
219
|
|
|
) |
220
|
|
|
; |
221
|
1 |
|
|
222
|
|
|
return $this->buildSrc($container).$this->buildAlternative($container); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $twigName |
227
|
|
|
* @param string $methodName |
228
|
|
|
* @return \Twig_SimpleFunction |
229
|
1 |
|
*/ |
230
|
|
|
private function makeFunction($twigName, $methodName) |
231
|
1 |
|
{ |
232
|
1 |
|
return new \Twig_SimpleFunction( |
233
|
1 |
|
$twigName, |
234
|
|
|
array($this, $methodName), |
235
|
1 |
|
array( |
236
|
|
|
'is_safe' => array('html'), |
237
|
1 |
|
) |
238
|
|
|
); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return \Twig_SimpleFunction[] |
243
|
1 |
|
*/ |
244
|
|
|
public function getFunctions() |
245
|
1 |
|
{ |
246
|
1 |
|
$result = array(); |
247
|
1 |
|
foreach (self::$twigFunctions as $twigName => $methodName) { |
248
|
1 |
|
$result[] = $this->makeFunction($twigName, $methodName); |
249
|
1 |
|
} |
250
|
|
|
return $result; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return string |
255
|
1 |
|
*/ |
256
|
|
|
public function getName() |
257
|
1 |
|
{ |
258
|
|
|
return 'tagcommander_extension'; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.