1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoS\ResourceBundle\Twig\Extension; |
4
|
|
|
|
5
|
|
|
use Sylius\Bundle\SettingsBundle\Templating\Helper\SettingsHelper; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
7
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
8
|
|
|
|
9
|
|
|
class Generic extends \Twig_Extension |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ContainerInterface |
13
|
|
|
*/ |
14
|
|
|
protected $container; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var SettingsHelper |
18
|
|
|
*/ |
19
|
|
|
protected $settingsHelper; |
20
|
|
|
|
21
|
|
|
public function __construct(ContainerInterface $container) |
22
|
|
|
{ |
23
|
|
|
$this->container = $container; |
24
|
|
|
$this->settingsHelper = $container->get('sylius.templating.helper.settings'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
public function getGlobals() |
31
|
|
|
{ |
32
|
|
|
return array( |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public function getFunctions() |
40
|
|
|
{ |
41
|
|
|
$self = array('is_safe' => array('all')); |
42
|
|
|
|
43
|
|
|
return array( |
44
|
|
|
new \Twig_SimpleFunction('d', '\Kint::dump', $self), |
|
|
|
|
45
|
|
|
new \Twig_SimpleFunction('e', array($this, 'dumpExit'), $self), |
|
|
|
|
46
|
|
|
new \Twig_SimpleFunction('is_string', 'is_string'), |
|
|
|
|
47
|
|
|
new \Twig_SimpleFunction('ui_random_string', array($this, 'getRandomString'), $self), |
|
|
|
|
48
|
|
|
new \Twig_SimpleFunction('ui_percentage', array($this, 'calculatePercent')), |
|
|
|
|
49
|
|
|
new \Twig_SimpleFunction('ui_setting', array($this, 'getSettingsParameter')), |
|
|
|
|
50
|
|
|
new \Twig_SimpleFunction('ui_settings', array($this->settingsHelper, 'getSettings')), |
|
|
|
|
51
|
|
|
new \Twig_SimpleFunction('ui_param', array($this->container, 'getParameter')), |
|
|
|
|
52
|
|
|
new \Twig_SimpleFunction('ui_param_has', array($this->container, 'hasParameter')), |
|
|
|
|
53
|
|
|
new \Twig_SimpleFunction('ui_has', array($this->container, 'has')), |
|
|
|
|
54
|
|
|
new \Twig_SimpleFunction('ui_obfuscated_email', array($this, 'getObfuscatedEmail')), |
|
|
|
|
55
|
|
|
new \Twig_SimpleFunction('ui_property', array($this, 'getContextValue')), |
|
|
|
|
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getFilters() |
60
|
|
|
{ |
61
|
|
|
//$self = array('is_safe' => array('all')); |
|
|
|
|
62
|
|
|
|
63
|
|
|
return array( |
64
|
|
|
new \Twig_SimpleFilter('is_match', array($this, 'match')), |
|
|
|
|
65
|
|
|
new \Twig_SimpleFilter('ui_no_space', array($this, 'cleanWhiteSpace')), |
|
|
|
|
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $key |
71
|
|
|
* @param string $default |
72
|
|
|
* |
73
|
|
|
* @return null|mixed |
74
|
|
|
*/ |
75
|
|
|
public function getSettingsParameter($key, $default = null) |
76
|
|
|
{ |
77
|
|
|
list($alias, $key) = explode('.', $key); |
78
|
|
|
|
79
|
|
|
$settings = $this->container->get('sylius.templating.helper.settings') |
80
|
|
|
->getSettings($alias) |
81
|
|
|
; |
82
|
|
|
|
83
|
|
|
if (array_key_exists($key, $settings)) { |
84
|
|
|
return $settings[$key]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $default; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getRandomString($length = 8) |
91
|
|
|
{ |
92
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
93
|
|
|
$randomString = ''; |
94
|
|
|
|
95
|
|
|
for ($i = 0; $i < $length; $i++) { |
96
|
|
|
$randomString .= $characters[rand(0, strlen($characters) - 1)]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $randomString; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $total |
104
|
|
|
* @param $pie |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function calculatePercent($total, $pie) |
109
|
|
|
{ |
110
|
|
|
if (empty($total)) { |
111
|
|
|
return '0%'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$total = ($pie / $total) * 100; |
115
|
|
|
|
116
|
|
|
if (is_float($total)) { |
117
|
|
|
return number_format($total, 1).'%'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $total.'%'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $email |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getObfuscatedEmail($email) |
129
|
|
|
{ |
130
|
|
|
if (false !== $pos = strpos($email, '@')) { |
131
|
|
|
$email = '...'.substr($email, $pos); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $email; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param $context |
139
|
|
|
* @param $path |
140
|
|
|
* |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
public function getContextValue($context, $path) |
144
|
|
|
{ |
145
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
146
|
|
|
|
147
|
|
|
return $accessor->getValue($context, $path); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $string |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function cleanWhiteSpace($string) |
156
|
|
|
{ |
157
|
|
|
return preg_replace('/ /', '', $string); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function match($subject, $pattern) |
161
|
|
|
{ |
162
|
|
|
return preg_match($pattern, $subject); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function dumpExit($val) |
166
|
|
|
{ |
167
|
|
|
\Kint::dump($val); |
168
|
|
|
exit; |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @api |
173
|
|
|
*/ |
174
|
|
|
public function getName() |
175
|
|
|
{ |
176
|
|
|
return 'ui_generic'; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.