This repository has been archived by the owner on Jan 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compiler.php
266 lines (239 loc) · 6.61 KB
/
Compiler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
namespace Kasseler\Component\Compiler;
class Compiler
{
/**
* @var string
*/
private $function;
/**
* @var array
*/
private $types = ['assoc_array', 'array', 'string'];
private $arrayTypes = [
'assoc_array' => ['open' => '{', 'close' => '}'],
'array' => ['open' => '[', 'close' => ']'],
];
/**
* @param $function
*/
public function __construct($function)
{
$this->function = $function;
}
/**
* @param $string
*
* @return mixed
*/
private function decode($string)
{
$result = json_decode($string, true);
return json_last_error() == JSON_ERROR_NONE ? $result : false;
}
/**
* @param $string
*
* @return int
*/
private function isFunction($string)
{
return preg_match_all('/^'.$this->function.'\(.+\)$/', $string);
}
/**
* @param $string
*
* @return bool
*/
private function getType($string)
{
foreach ($this->types as $type) {
if (stripos($string, $type) !== false) {
return $type;
}
}
return false;
}
/**
* @param $string
* @param $parameters
* @param $replace
* @param $name
*/
private function replaceParameters(&$string, &$parameters, $replace, $name)
{
if (!empty($replace)) {
$stringArray = [];
$start = 0;
foreach ($replace as $num) {
$parameters[$name][] = $name == 'string'
? substr($string, $num[0]+1, $num[1]-$num[0]-1)
: substr($string, $num[0], $num[1]-$num[0]+1)
;
$stringArray[] = substr($string, $start, $num[0]-$start);
$start = $num[1]+1;
}
if (strlen($string) > $start) {
$stringArray[] = substr($string, $start, strlen($string)-$start);
}
$count = count($stringArray);
if ($count > 0) {
$string = $count > 1 ? implode('%:'.strtoupper($name).':%', $stringArray) : '%:'.strtoupper($name).':%';
}
}
}
/**
* @param $string
*
* @return array
*/
private function compileParameters($string)
{
$parameters = [];
foreach ($this->arrayTypes as $name => $tag) {
$replace = $this->searchArrays($string, $tag['open'], $tag['close']);
$this->replaceParameters($string, $parameters, $replace, $name);
}
$this->replaceParameters($string, $parameters, $this->searchString($string), 'string');
return $this->parametersSplit($string, $parameters);
}
/**
* @param $var
* @param $value
*
* @return mixed
*/
private function replaceParameter($var, $value)
{
return $var !== false && $var !== null
? $var
: $value;
}
/**
* @param $string
* @param $parameters
*
* @return array
*/
private function parametersSplit($string, &$parameters)
{
$result = preg_split('/[\s,]+/', $string);
foreach($result as &$v) {
$replaced = false;
while (is_string($v) && ($type = $this->getType($v))) {
if ($type) {
$v = preg_replace_callback("/%:{$type}:%/i", function() use ($parameters, $type) {
return $parameters[$type][0];
}, $v);
array_shift($parameters[$type]);
}
$v = $this->replaceParameter($this->decode($v), $v);
$replaced = true;
}
if (empty($type) && !$replaced) {
$v = $this->replaceParameter($this->decode($v), $v);
}
}
return $result;
}
/**
* @param $string
*
* @return array
*/
private function searchString($string)
{
$quotes = ['"', "'"];
$strings = [];
$open = false;
$start = 0;
$openSymbol = '';
for ($i = 0; $i < strlen($string); $i++) {
if ($string[$i] == $openSymbol && $open && $string[$i-1] != '\\') {
$open = false;
$strings[] = [$start, $i, $openSymbol];
continue;
}
if (in_array($string[$i], $quotes) && !$open) {
$openSymbol = $string[$i];
$open = true;
$start = $i;
}
}
return $strings;
}
/**
* @param $string
* @param $openSymbol
* @param $closeSymbol
*
* @return array
*/
private function searchArrays($string, $openSymbol, $closeSymbol)
{
$objects = [];
$open = $start = -1;
for ($i = 0; $i < strlen($string); $i++) {
if ($string[$i] == $openSymbol) {
$start = $open == -1 ? $i : $start;
$open++;
}
if ($string[$i] == $closeSymbol && $open > -1) {
$open--;
if ($open == -1) {
$objects[] = [$start, $i];
$open = -1;
}
}
}
return $objects;
}
/**
* @param $string
*
* @return mixed
*/
private function getData($string)
{
if($this->isFunction($string)){
$function = json_decode(
preg_replace_callback('/^(.+?)\((.*)\)$/i', function($match) {
return json_encode(['type' => 'function', 'name' => $match[1], 'attributes' => $match[2]]);
},
$string
),
true
);
$function['attributes'] = $this->compileParameters($function['attributes']);
return $function;
} else {
if (($var = $this->decode($string))) {
$type = gettype($var);
$return = [
'type' => $type == 'double' ? 'float' : $type,
'data' => $var,
];
}
return isset($return) ? $return : [
'type' => 'string',
'data' => $string,
];
}
}
/**
* @param $string
*
* @return mixed
*/
public function run($string)
{
$data = $this->getData($string);
if ($data['type'] != 'function') {
return $data['data'];
}
if (function_exists($data['name'])) {
return call_user_func_array($data['name'], $data['attributes']);
}
return false;
}
}