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 EventDispatcher |
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
|
|
|
* |
73
|
|
|
*/ |
74
|
1 |
|
public function __construct( |
75
|
|
|
ParameterBagInterface $datalayer, |
76
|
|
|
EventDispatcherInterface $dispatcher, |
77
|
|
|
$tcVars = 'tc_vars' |
78
|
|
|
) { |
79
|
1 |
|
$this->datalayer = $datalayer; |
80
|
1 |
|
$this->dispatcher = $dispatcher; |
|
|
|
|
81
|
1 |
|
$this->tcVars = $tcVars; |
82
|
1 |
|
$this->serializer = new Serializer( |
83
|
1 |
|
array(new ObjectNormalizer()), |
84
|
1 |
|
array(new JsonEncoder()) |
85
|
|
|
); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string|boolean|integer|double|null |
90
|
|
|
*/ |
91
|
1 |
|
protected function serializeWithValues($values = array()) |
92
|
|
|
{ |
93
|
1 |
|
$datalayer = clone $this->datalayer; |
94
|
|
|
|
95
|
|
|
return $this |
96
|
1 |
|
->serializer |
97
|
1 |
|
->serialize( |
98
|
|
|
array_merge( |
99
|
1 |
|
$datalayer->all(), |
100
|
|
|
$values |
101
|
|
|
), |
102
|
1 |
|
'json' |
103
|
|
|
) |
104
|
|
|
; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Array $values |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
1 |
|
public function tcVars($values = array()) |
112
|
|
|
{ |
113
|
1 |
|
return sprintf( |
114
|
1 |
|
'<script type="text/javascript">var %s = %s;</script>', |
115
|
1 |
|
$this->tcVars, |
116
|
1 |
|
$this->serializeWithValues($values) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $event |
122
|
|
|
* @param boolean $setAsDefault |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
1 |
|
public function addEvent($event, $setAsDefault = false) |
126
|
|
|
{ |
127
|
1 |
|
$this->events[$event['name']] = $event; |
128
|
|
|
|
129
|
1 |
|
if ($setAsDefault || (!$setAsDefault && !$this->defaultEvent)) { |
130
|
1 |
|
$this->defaultEvent = $event['name']; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $eventName |
138
|
|
|
* @param array $values |
139
|
|
|
* @param string|null $tracker |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
1 |
|
public function tcEvent($eventName, $values = array(), $tracker = null) |
143
|
|
|
{ |
144
|
1 |
|
if (is_null($tracker)) { |
145
|
1 |
|
$tracker = $this->defaultEvent; |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
$function = $this->events[$tracker]['function']; |
149
|
|
|
|
150
|
1 |
|
$event = new Track($tracker, $eventName, array_merge($this->datalayer->all(), $values)); |
151
|
1 |
|
$this->dispatcher->dispatch('tc_event', $event); |
152
|
|
|
|
153
|
1 |
|
return sprintf( |
154
|
1 |
|
"%s('%s', %s);", |
155
|
|
|
$function, |
156
|
|
|
$eventName, |
157
|
1 |
|
$this->serializeWithValues($values) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param array $container |
163
|
|
|
* @return self |
164
|
|
|
*/ |
165
|
1 |
|
public function addContainer($container) |
166
|
|
|
{ |
167
|
1 |
|
$this->containers[$container['name']] = $container; |
168
|
|
|
|
169
|
1 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $containerName |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
public function tcContainer($containerName) |
177
|
|
|
{ |
178
|
|
|
$container = $this->containers[$containerName]; |
179
|
|
|
$container_version = $container_alternative = null; |
180
|
|
|
|
181
|
|
|
$container_script = $container['script']; |
182
|
|
|
$src = $container_script; |
183
|
|
|
|
184
|
|
|
if ($container['version']) { |
185
|
|
|
$container_version = $container['version']; |
186
|
|
|
$src .= sprintf('?%s', $container_version); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$result = sprintf('<script type="text/javascript" src="%s"></script>', $src); |
190
|
|
|
|
191
|
|
|
if ($container['alternative']) { |
192
|
|
|
$container_alternative = $container['alternative']; |
193
|
|
|
$result .= sprintf( |
194
|
|
|
'<noscript><iframe src="%s" width="1" height="1" rel="noindex,nofollow"></iframe></noscript>', |
195
|
|
|
$container_alternative |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$event = new DeployContainer( |
200
|
|
|
$containerName, |
201
|
|
|
$container_script, |
202
|
|
|
$container_version, |
203
|
|
|
$container_alternative |
204
|
|
|
); |
205
|
|
|
$this->dispatcher->dispatch('tc_container', $event); |
206
|
|
|
|
207
|
|
|
return $result; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $twigName |
212
|
|
|
* @param string $methodName |
213
|
|
|
* @return \Twig_SimpleFunction |
214
|
|
|
*/ |
215
|
1 |
|
protected function makeFunction($twigName, $methodName) |
216
|
|
|
{ |
217
|
1 |
|
return new \Twig_SimpleFunction( |
218
|
|
|
$twigName, |
219
|
1 |
|
array($this, $methodName), |
220
|
|
|
array( |
221
|
1 |
|
'is_safe' => array('html'), |
222
|
|
|
) |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return \Twig_SimpleFunction[] |
228
|
|
|
*/ |
229
|
1 |
|
public function getFunctions() |
230
|
|
|
{ |
231
|
1 |
|
$result = array(); |
232
|
1 |
|
foreach (self::$twigFunctions as $twigName => $methodName) { |
233
|
1 |
|
$result[] = $this->makeFunction($twigName, $methodName); |
234
|
|
|
} |
235
|
1 |
|
return $result; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
1 |
|
public function getName() |
242
|
|
|
{ |
243
|
1 |
|
return 'tagcommander_extension'; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..