1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the GoogleTagManagerBundle project |
4
|
|
|
* |
5
|
|
|
* (c) Philipp Braeutigam <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Xynnn\GoogleTagManagerBundle\Twig; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Templating\Helper\HelperInterface; |
14
|
|
|
use Twig_Extension; |
15
|
|
|
use Xynnn\GoogleTagManagerBundle\Helper\GoogleTagManagerHelper; |
16
|
|
|
use Xynnn\GoogleTagManagerBundle\Helper\GoogleTagManagerHelperInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class GoogleTagManagerExtension |
20
|
|
|
* |
21
|
|
|
* @package Xynnn\GoogleTagManagerBundle\Extension |
22
|
|
|
*/ |
23
|
|
|
class GoogleTagManagerExtension extends Twig_Extension |
24
|
|
|
{ |
25
|
|
|
const AREA_FULL = 'full'; |
26
|
|
|
const AREA_HEAD = 'head'; |
27
|
|
|
const AREA_BODY = 'body'; |
28
|
|
|
const AREA_BODY_END = 'body_end'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var GoogleTagManagerHelperInterface |
32
|
|
|
*/ |
33
|
|
|
private $helper; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param GoogleTagManagerHelperInterface $helper |
37
|
|
|
*/ |
38
|
|
|
public function __construct(GoogleTagManagerHelperInterface $helper) |
39
|
|
|
{ |
40
|
|
|
$this->helper = $helper; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function getFunctions() |
47
|
|
|
{ |
48
|
|
|
return array( |
49
|
|
|
new \Twig_SimpleFunction('google_tag_manager', array($this, 'render'), array( |
50
|
|
|
'is_safe' => array('html'), |
51
|
|
|
'needs_environment' => true, |
52
|
|
|
'deprecated' => true, |
53
|
|
|
)), |
54
|
|
|
new \Twig_SimpleFunction('google_tag_manager_body', array($this, 'renderBody'), array( |
55
|
|
|
'is_safe' => array('html'), |
56
|
|
|
'needs_environment' => true, |
57
|
|
|
)), |
58
|
|
|
new \Twig_SimpleFunction('google_tag_manager_head', array($this, 'renderHead'), array( |
59
|
|
|
'is_safe' => array('html'), |
60
|
|
|
'needs_environment' => true, |
61
|
|
|
)), |
62
|
|
|
new \Twig_SimpleFunction('google_tag_manager_body_end', array($this, 'renderBodyEnd'), array( |
63
|
|
|
'is_safe' => array('html'), |
64
|
|
|
'needs_environment' => true, |
65
|
|
|
)), |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param \Twig_Environment $twig |
71
|
|
|
* |
72
|
|
|
* @deprecated Use `renderHead` and `renderBody` |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function render(\Twig_Environment $twig) |
77
|
|
|
{ |
78
|
|
|
return $this->getRenderedTemplate($twig, self::AREA_FULL); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \Twig_Environment $twig |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function renderHead(\Twig_Environment $twig) |
87
|
|
|
{ |
88
|
|
|
return $this->getRenderedTemplate($twig, self::AREA_HEAD); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \Twig_Environment $twig |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function renderBody(\Twig_Environment $twig) |
97
|
|
|
{ |
98
|
|
|
return $this->getRenderedTemplate($twig, self::AREA_BODY); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param \Twig_Environment $twig |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function renderBodyEnd(\Twig_Environment $twig) |
107
|
|
|
{ |
108
|
|
|
return $this->getRenderedTemplate($twig, self::AREA_BODY_END); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getName() |
115
|
|
|
{ |
116
|
|
|
return 'google_tag_manager'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $area |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
private function getTemplate($area) |
124
|
|
|
{ |
125
|
|
|
switch ($area) { |
126
|
|
|
case self::AREA_HEAD: |
127
|
|
|
return 'tagmanager_head'; |
128
|
|
|
case self::AREA_BODY: |
129
|
|
|
return 'tagmanager_body'; |
130
|
|
|
case self::AREA_BODY_END: |
131
|
|
|
return 'tagmanager_body_end'; |
132
|
|
|
case self::AREA_FULL: |
133
|
|
|
default: |
134
|
|
|
return 'tagmanager'; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param \Twig_Environment $twig |
140
|
|
|
* @param $area |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
private function getRenderedTemplate(\Twig_Environment $twig, $area) |
144
|
|
|
{ |
145
|
|
|
if (!$this->helper->isEnabled()) { |
146
|
|
|
return false; |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $twig->render( |
150
|
|
|
'GoogleTagManagerBundle::' . $this->getTemplate($area) . '.html.twig', array( |
151
|
|
|
'id' => $this->helper->getId(), |
152
|
|
|
'data' => $this->helper->hasData() ? $this->helper->getData() : null, |
153
|
|
|
'push' => $this->helper->getPush() ? $this->helper->getPush() : null, |
154
|
|
|
) |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.