1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\LeadGenerationBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\LeadGenerationBundle\Entity\Popup\AbstractPopup; |
6
|
|
|
use Kunstmaan\LeadGenerationBundle\Entity\Rule\AbstractRule; |
7
|
|
|
use Kunstmaan\LeadGenerationBundle\Service\PopupManager; |
8
|
|
|
use Kunstmaan\LeadGenerationBundle\Service\RuleServiceInterface; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Twig\Environment; |
11
|
|
|
use Twig\Extension\AbstractExtension; |
12
|
|
|
use Twig\TwigFunction; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @final since 5.4 |
16
|
|
|
*/ |
17
|
|
|
class PopupTwigExtension extends AbstractExtension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var PopupManager |
21
|
|
|
*/ |
22
|
|
|
private $popupManager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ContainerInterface |
26
|
|
|
*/ |
27
|
|
|
private $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $popupTypes; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $debug; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param PopupManager $popupManager |
41
|
|
|
* @param ContainerInterface $container |
42
|
|
|
* @param array $popupTypes |
43
|
|
|
* @param bool $debug |
44
|
|
|
*/ |
45
|
|
|
public function __construct(PopupManager $popupManager, ContainerInterface $container, array $popupTypes, $debug) |
46
|
|
|
{ |
47
|
|
|
$this->popupManager = $popupManager; |
48
|
|
|
$this->container = $container; |
49
|
|
|
$this->popupTypes = $popupTypes; |
50
|
|
|
$this->debug = $debug; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns a list of functions to add to the existing list. |
55
|
|
|
* |
56
|
|
|
* @return array An array of functions |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
public function getFunctions() |
59
|
|
|
{ |
60
|
|
|
return array( |
61
|
|
|
new TwigFunction('lead_generation_render_js_includes', array($this, 'renderJsIncludes'), array('is_safe' => array('html'), 'needs_environment' => true)), |
62
|
|
|
new TwigFunction('lead_generation_render_popups_html', array($this, 'renderPopupsHtml'), array('is_safe' => array('html'), 'needs_environment' => true)), |
63
|
|
|
new TwigFunction('lead_generation_render_initialize_js', array($this, 'renderInitializeJs'), array('is_safe' => array('html'), 'needs_environment' => true)), |
64
|
|
|
new TwigFunction('lead_generation_get_rule_properties', array($this, 'getRuleProperties')), |
65
|
|
|
new TwigFunction('get_available_popup_types', array($this, 'getAvailablePopupTypes')), |
66
|
|
|
new TwigFunction('get_available_rule_types', array($this, 'getAvailableRuleTypes')), |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function renderJsIncludes(Environment $environment) |
74
|
|
|
{ |
75
|
|
|
$files = $this->popupManager->getUniqueJsIncludes(); |
76
|
|
|
|
77
|
|
|
return $environment->render('@KunstmaanLeadGeneration/js-includes.html.twig', array('files' => $files)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function renderPopupsHtml(Environment $environment) |
84
|
|
|
{ |
85
|
|
|
$popups = $this->popupManager->getPopups(); |
86
|
|
|
|
87
|
|
|
return $environment->render('@KunstmaanLeadGeneration/popups-html.html.twig', array('popups' => $popups)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function renderInitializeJs(Environment $environment) |
94
|
|
|
{ |
95
|
|
|
$popups = $this->popupManager->getPopups(); |
96
|
|
|
|
97
|
|
|
return $environment->render('@KunstmaanLeadGeneration/initialize-js.html.twig', array('popups' => $popups, 'debug' => $this->debug)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param AbstractRule $rule |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function getRuleProperties(AbstractRule $rule) |
106
|
|
|
{ |
107
|
|
|
$properties = array(); |
108
|
|
|
if (!\is_null($rule->getService())) { |
109
|
|
|
$service = $this->container->get($rule->getService()); |
|
|
|
|
110
|
|
|
if ($service instanceof RuleServiceInterface) { |
111
|
|
|
$properties = $service->getJsProperties($rule); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return array_merge($rule->getJsProperties(), $properties); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the available popup types. |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function getAvailablePopupTypes() |
124
|
|
|
{ |
125
|
|
|
$popups = array(); |
126
|
|
|
foreach ($this->popupTypes as $popupType) { |
127
|
|
|
$object = new $popupType(); |
128
|
|
|
$popups[$object->getClassname()] = $object->getFullClassname(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $popups; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the available popup types for a specific popup. |
136
|
|
|
* |
137
|
|
|
* @param AbstractPopup $popup |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function getAvailableRuleTypes(AbstractPopup $popup) |
142
|
|
|
{ |
143
|
|
|
$rulesTypes = $this->popupManager->getAvailableRules($popup); |
144
|
|
|
|
145
|
|
|
$rules = array(); |
146
|
|
|
foreach ($rulesTypes as $ruleType) { |
147
|
|
|
$object = new $ruleType(); |
148
|
|
|
$rules[$object->getClassname()] = $object->getFullClassname(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $rules; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.