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