@@ -7,178 +7,178 @@ discard block |
||
7 | 7 | */ |
8 | 8 | class CodeHandler extends CompilerUtils |
9 | 9 | { |
10 | - protected $input; |
|
11 | - protected $name; |
|
12 | - protected $separators; |
|
13 | - |
|
14 | - public function __construct($input, $name) |
|
15 | - { |
|
16 | - if (!is_string($input)) { |
|
17 | - throw new \InvalidArgumentException('Expecting a string of PHP, got: ' . gettype($input), 11); |
|
18 | - } |
|
19 | - |
|
20 | - if (strlen($input) === 0) { |
|
21 | - throw new \InvalidArgumentException('Expecting a string of PHP, empty string received.', 12); |
|
22 | - } |
|
23 | - |
|
24 | - $this->input = trim(preg_replace('/\bvar\b/', '', $input)); |
|
25 | - $this->name = $name; |
|
26 | - $this->separators = array(); |
|
27 | - } |
|
28 | - |
|
29 | - public function innerCode($input, $name) |
|
30 | - { |
|
31 | - $handler = new static($input, $name); |
|
32 | - |
|
33 | - return $handler->parse(); |
|
34 | - } |
|
35 | - |
|
36 | - public function parse() |
|
37 | - { |
|
38 | - if ($this->isQuotedString()) { |
|
39 | - return array($this->input); |
|
40 | - } |
|
41 | - |
|
42 | - if (strpos('=,;?', substr($this->input, 0, 1)) !== false) { |
|
43 | - throw new \ErrorException('Expecting a variable name or an expression, got: ' . $this->input, 13); |
|
44 | - } |
|
45 | - |
|
46 | - preg_match_all( |
|
47 | - '/(?<![<>=!])=(?!>|=)|[\[\]\{\}\(\),;\.]|(?!:):|->/', // punctuation |
|
48 | - preg_replace_callback('/[a-zA-Z0-9\\\\_\\x7f-\\xff]*\((?:[0-9\/%\.\s*+-]++|(?R))*+\)/', function ($match) { |
|
49 | - // no need to keep separators in simple PHP expressions (functions calls, parentheses, calculs) |
|
50 | - return str_repeat(' ', strlen($match[0])); |
|
51 | - }, preg_replace_callback('/([\'"]).*?(?<!\\\\)(?:\\\\{2})*\\1/', function ($match) { |
|
52 | - // do not take separators in strings |
|
53 | - return str_repeat(' ', strlen($match[0])); |
|
54 | - }, $this->input)), |
|
55 | - $separators, |
|
56 | - PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE |
|
57 | - ); |
|
58 | - |
|
59 | - $this->separators = $separators[0]; |
|
60 | - |
|
61 | - if (count($this->separators) === 0) { |
|
62 | - if (strstr('0123456789-+("\'$', substr($this->input, 0, 1)) === false) { |
|
63 | - $this->input = static::addDollarIfNeeded($this->input); |
|
64 | - } |
|
65 | - |
|
66 | - return array($this->input); |
|
67 | - } |
|
68 | - |
|
69 | - // add a pseudo separator for the end of the input |
|
70 | - array_push($this->separators, array(null, strlen($this->input))); |
|
71 | - |
|
72 | - return $this->parseBetweenSeparators(); |
|
73 | - } |
|
74 | - |
|
75 | - protected function isQuotedString() |
|
76 | - { |
|
77 | - $firstChar = substr($this->input, 0, 1); |
|
78 | - $lastChar = substr($this->input, -1); |
|
79 | - |
|
80 | - return false !== strpos('"\'', $firstChar) && $lastChar === $firstChar; |
|
81 | - } |
|
82 | - |
|
83 | - protected function getVarname($separator) |
|
84 | - { |
|
85 | - // do not add $ if it is not like a variable |
|
86 | - $varname = static::convertVarPath(substr($this->input, 0, $separator[1]), '/^%s/'); |
|
87 | - |
|
88 | - return $separator[0] !== '(' && $varname !== '' && strstr('0123456789-+("\'$', substr($varname, 0, 1)) === false |
|
89 | - ? static::addDollarIfNeeded($varname) |
|
90 | - : $varname; |
|
91 | - } |
|
92 | - |
|
93 | - protected function parseArrayString(&$argument, $match, $consume, &$quote, &$key, &$value) |
|
94 | - { |
|
95 | - $quote = $quote |
|
96 | - ? CommonUtils::escapedEnd($match[1]) |
|
97 | - ? $quote |
|
98 | - : null |
|
99 | - : $match[2]; |
|
100 | - ${is_null($value) ? 'key' : 'value'} .= $match[0]; |
|
101 | - $consume($argument, $match[0]); |
|
102 | - } |
|
103 | - |
|
104 | - protected function parseArrayAssign(&$argument, $match, $consume, &$quote, &$key, &$value) |
|
105 | - { |
|
106 | - if ($quote) { |
|
107 | - ${is_null($value) ? 'key' : 'value'} .= $match[0]; |
|
108 | - $consume($argument, $match[0]); |
|
109 | - |
|
110 | - return; |
|
111 | - } |
|
112 | - |
|
113 | - if (!is_null($value)) { |
|
114 | - throw new \ErrorException('Parse error on ' . substr($argument, strlen($match[1])), 15); |
|
115 | - } |
|
116 | - |
|
117 | - $key .= $match[1]; |
|
118 | - $value = ''; |
|
119 | - $consume($argument, $match[0]); |
|
120 | - } |
|
121 | - |
|
122 | - protected function parseArrayElement(&$argument, $match, $consume, &$quote, &$key, &$value) |
|
123 | - { |
|
124 | - switch ($match[2]) { |
|
125 | - case '"': |
|
126 | - case "'": |
|
127 | - $this->parseArrayString($argument, $match, $consume, $quote, $key, $value); |
|
128 | - break; |
|
129 | - case ':': |
|
130 | - case '=>': |
|
131 | - $this->parseArrayAssign($argument, $match, $consume, $quote, $key, $value); |
|
132 | - break; |
|
133 | - case ',': |
|
134 | - ${is_null($value) ? 'key' : 'value'} .= $match[0]; |
|
135 | - $consume($argument, $match[0]); |
|
136 | - break; |
|
137 | - } |
|
138 | - } |
|
139 | - |
|
140 | - protected function parseArray($input, $subCodeHandler) |
|
141 | - { |
|
142 | - $output = array(); |
|
143 | - $key = ''; |
|
144 | - $value = null; |
|
145 | - $addToOutput = $subCodeHandler->addToOutput($output, $key, $value); |
|
146 | - $consume = $subCodeHandler->consume(); |
|
147 | - foreach ($input as $argument) { |
|
148 | - $argument = ltrim($argument, '$'); |
|
149 | - $quote = null; |
|
150 | - while (preg_match('/^(.*?)(=>|[\'",:])/', $argument, $match)) { |
|
151 | - $this->parseArrayElement($argument, $match, $consume, $quote, $key, $value); |
|
152 | - } |
|
153 | - ${is_null($value) ? 'key' : 'value'} .= $argument; |
|
154 | - $addToOutput(); |
|
155 | - } |
|
156 | - |
|
157 | - return 'array(' . implode(', ', $output) . ')'; |
|
158 | - } |
|
159 | - |
|
160 | - protected function parseEqual($sep, &$separators, &$result, $innerName, $subCodeHandler) |
|
161 | - { |
|
162 | - if (preg_match('/^[[:space:]]*$/', $innerName)) { |
|
163 | - next($separators); |
|
164 | - $handleCodeInbetween = $subCodeHandler->handleCodeInbetween($separators, $result); |
|
165 | - |
|
166 | - return implode($handleCodeInbetween()); |
|
167 | - } |
|
168 | - |
|
169 | - $handleRecursion = $subCodeHandler->handleRecursion($result); |
|
170 | - |
|
171 | - return $handleRecursion(array($sep, end($separators))); |
|
172 | - } |
|
173 | - |
|
174 | - protected function parseSeparator($sep, &$separators, &$result, &$varname, $subCodeHandler, $innerName) |
|
175 | - { |
|
176 | - $handleCodeInbetween = $subCodeHandler->handleCodeInbetween($separators, $result); |
|
177 | - $var = '$__' . $this->name; |
|
178 | - |
|
179 | - switch ($sep[0]) { |
|
180 | - // translate the javascript's obj.attr into php's obj->attr or obj['attr'] |
|
181 | - /* |
|
10 | + protected $input; |
|
11 | + protected $name; |
|
12 | + protected $separators; |
|
13 | + |
|
14 | + public function __construct($input, $name) |
|
15 | + { |
|
16 | + if (!is_string($input)) { |
|
17 | + throw new \InvalidArgumentException('Expecting a string of PHP, got: ' . gettype($input), 11); |
|
18 | + } |
|
19 | + |
|
20 | + if (strlen($input) === 0) { |
|
21 | + throw new \InvalidArgumentException('Expecting a string of PHP, empty string received.', 12); |
|
22 | + } |
|
23 | + |
|
24 | + $this->input = trim(preg_replace('/\bvar\b/', '', $input)); |
|
25 | + $this->name = $name; |
|
26 | + $this->separators = array(); |
|
27 | + } |
|
28 | + |
|
29 | + public function innerCode($input, $name) |
|
30 | + { |
|
31 | + $handler = new static($input, $name); |
|
32 | + |
|
33 | + return $handler->parse(); |
|
34 | + } |
|
35 | + |
|
36 | + public function parse() |
|
37 | + { |
|
38 | + if ($this->isQuotedString()) { |
|
39 | + return array($this->input); |
|
40 | + } |
|
41 | + |
|
42 | + if (strpos('=,;?', substr($this->input, 0, 1)) !== false) { |
|
43 | + throw new \ErrorException('Expecting a variable name or an expression, got: ' . $this->input, 13); |
|
44 | + } |
|
45 | + |
|
46 | + preg_match_all( |
|
47 | + '/(?<![<>=!])=(?!>|=)|[\[\]\{\}\(\),;\.]|(?!:):|->/', // punctuation |
|
48 | + preg_replace_callback('/[a-zA-Z0-9\\\\_\\x7f-\\xff]*\((?:[0-9\/%\.\s*+-]++|(?R))*+\)/', function ($match) { |
|
49 | + // no need to keep separators in simple PHP expressions (functions calls, parentheses, calculs) |
|
50 | + return str_repeat(' ', strlen($match[0])); |
|
51 | + }, preg_replace_callback('/([\'"]).*?(?<!\\\\)(?:\\\\{2})*\\1/', function ($match) { |
|
52 | + // do not take separators in strings |
|
53 | + return str_repeat(' ', strlen($match[0])); |
|
54 | + }, $this->input)), |
|
55 | + $separators, |
|
56 | + PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE |
|
57 | + ); |
|
58 | + |
|
59 | + $this->separators = $separators[0]; |
|
60 | + |
|
61 | + if (count($this->separators) === 0) { |
|
62 | + if (strstr('0123456789-+("\'$', substr($this->input, 0, 1)) === false) { |
|
63 | + $this->input = static::addDollarIfNeeded($this->input); |
|
64 | + } |
|
65 | + |
|
66 | + return array($this->input); |
|
67 | + } |
|
68 | + |
|
69 | + // add a pseudo separator for the end of the input |
|
70 | + array_push($this->separators, array(null, strlen($this->input))); |
|
71 | + |
|
72 | + return $this->parseBetweenSeparators(); |
|
73 | + } |
|
74 | + |
|
75 | + protected function isQuotedString() |
|
76 | + { |
|
77 | + $firstChar = substr($this->input, 0, 1); |
|
78 | + $lastChar = substr($this->input, -1); |
|
79 | + |
|
80 | + return false !== strpos('"\'', $firstChar) && $lastChar === $firstChar; |
|
81 | + } |
|
82 | + |
|
83 | + protected function getVarname($separator) |
|
84 | + { |
|
85 | + // do not add $ if it is not like a variable |
|
86 | + $varname = static::convertVarPath(substr($this->input, 0, $separator[1]), '/^%s/'); |
|
87 | + |
|
88 | + return $separator[0] !== '(' && $varname !== '' && strstr('0123456789-+("\'$', substr($varname, 0, 1)) === false |
|
89 | + ? static::addDollarIfNeeded($varname) |
|
90 | + : $varname; |
|
91 | + } |
|
92 | + |
|
93 | + protected function parseArrayString(&$argument, $match, $consume, &$quote, &$key, &$value) |
|
94 | + { |
|
95 | + $quote = $quote |
|
96 | + ? CommonUtils::escapedEnd($match[1]) |
|
97 | + ? $quote |
|
98 | + : null |
|
99 | + : $match[2]; |
|
100 | + ${is_null($value) ? 'key' : 'value'} .= $match[0]; |
|
101 | + $consume($argument, $match[0]); |
|
102 | + } |
|
103 | + |
|
104 | + protected function parseArrayAssign(&$argument, $match, $consume, &$quote, &$key, &$value) |
|
105 | + { |
|
106 | + if ($quote) { |
|
107 | + ${is_null($value) ? 'key' : 'value'} .= $match[0]; |
|
108 | + $consume($argument, $match[0]); |
|
109 | + |
|
110 | + return; |
|
111 | + } |
|
112 | + |
|
113 | + if (!is_null($value)) { |
|
114 | + throw new \ErrorException('Parse error on ' . substr($argument, strlen($match[1])), 15); |
|
115 | + } |
|
116 | + |
|
117 | + $key .= $match[1]; |
|
118 | + $value = ''; |
|
119 | + $consume($argument, $match[0]); |
|
120 | + } |
|
121 | + |
|
122 | + protected function parseArrayElement(&$argument, $match, $consume, &$quote, &$key, &$value) |
|
123 | + { |
|
124 | + switch ($match[2]) { |
|
125 | + case '"': |
|
126 | + case "'": |
|
127 | + $this->parseArrayString($argument, $match, $consume, $quote, $key, $value); |
|
128 | + break; |
|
129 | + case ':': |
|
130 | + case '=>': |
|
131 | + $this->parseArrayAssign($argument, $match, $consume, $quote, $key, $value); |
|
132 | + break; |
|
133 | + case ',': |
|
134 | + ${is_null($value) ? 'key' : 'value'} .= $match[0]; |
|
135 | + $consume($argument, $match[0]); |
|
136 | + break; |
|
137 | + } |
|
138 | + } |
|
139 | + |
|
140 | + protected function parseArray($input, $subCodeHandler) |
|
141 | + { |
|
142 | + $output = array(); |
|
143 | + $key = ''; |
|
144 | + $value = null; |
|
145 | + $addToOutput = $subCodeHandler->addToOutput($output, $key, $value); |
|
146 | + $consume = $subCodeHandler->consume(); |
|
147 | + foreach ($input as $argument) { |
|
148 | + $argument = ltrim($argument, '$'); |
|
149 | + $quote = null; |
|
150 | + while (preg_match('/^(.*?)(=>|[\'",:])/', $argument, $match)) { |
|
151 | + $this->parseArrayElement($argument, $match, $consume, $quote, $key, $value); |
|
152 | + } |
|
153 | + ${is_null($value) ? 'key' : 'value'} .= $argument; |
|
154 | + $addToOutput(); |
|
155 | + } |
|
156 | + |
|
157 | + return 'array(' . implode(', ', $output) . ')'; |
|
158 | + } |
|
159 | + |
|
160 | + protected function parseEqual($sep, &$separators, &$result, $innerName, $subCodeHandler) |
|
161 | + { |
|
162 | + if (preg_match('/^[[:space:]]*$/', $innerName)) { |
|
163 | + next($separators); |
|
164 | + $handleCodeInbetween = $subCodeHandler->handleCodeInbetween($separators, $result); |
|
165 | + |
|
166 | + return implode($handleCodeInbetween()); |
|
167 | + } |
|
168 | + |
|
169 | + $handleRecursion = $subCodeHandler->handleRecursion($result); |
|
170 | + |
|
171 | + return $handleRecursion(array($sep, end($separators))); |
|
172 | + } |
|
173 | + |
|
174 | + protected function parseSeparator($sep, &$separators, &$result, &$varname, $subCodeHandler, $innerName) |
|
175 | + { |
|
176 | + $handleCodeInbetween = $subCodeHandler->handleCodeInbetween($separators, $result); |
|
177 | + $var = '$__' . $this->name; |
|
178 | + |
|
179 | + switch ($sep[0]) { |
|
180 | + // translate the javascript's obj.attr into php's obj->attr or obj['attr'] |
|
181 | + /* |
|
182 | 182 | case '.': |
183 | 183 | $result[] = sprintf("%s=is_array(%s)?%s['%s']:%s->%s", |
184 | 184 | $var, $varname, $varname, $innerName, $varname, $innerName |
@@ -187,61 +187,61 @@ discard block |
||
187 | 187 | break; |
188 | 188 | //*/ |
189 | 189 | |
190 | - // funcall |
|
191 | - case '(': |
|
192 | - $arguments = $handleCodeInbetween(); |
|
193 | - $call = $varname . '(' . implode(', ', $arguments) . ')'; |
|
194 | - $call = static::addDollarIfNeeded($call); |
|
195 | - $varname = $var; |
|
196 | - array_push($result, "{$var}={$call}"); |
|
197 | - break; |
|
198 | - |
|
199 | - case '[': |
|
200 | - if (preg_match('/[a-zA-Z0-9\\\\_\\x7f-\\xff]$/', $varname)) { |
|
201 | - $varname .= $sep[0] . $innerName; |
|
202 | - break; |
|
203 | - } |
|
204 | - case '{': |
|
205 | - $varname .= $this->parseArray($handleCodeInbetween(), $subCodeHandler); |
|
206 | - break; |
|
207 | - |
|
208 | - case '=': |
|
209 | - $varname .= '=' . $this->parseEqual($sep, $separators, $result, $innerName, $subCodeHandler); |
|
210 | - break; |
|
211 | - |
|
212 | - default: |
|
213 | - if (($innerName !== false && $innerName !== '') || $sep[0] !== ')') { |
|
214 | - $varname .= $sep[0] . $innerName; |
|
215 | - } |
|
216 | - break; |
|
217 | - } |
|
218 | - } |
|
219 | - |
|
220 | - protected function parseBetweenSeparators() |
|
221 | - { |
|
222 | - $separators = $this->separators; |
|
223 | - |
|
224 | - $result = array(); |
|
225 | - |
|
226 | - $varname = $this->getVarname($separators[0]); |
|
227 | - |
|
228 | - $subCodeHandler = new SubCodeHandler($this, $this->input, $this->name); |
|
229 | - $getMiddleString = $subCodeHandler->getMiddleString(); |
|
230 | - $getNext = $subCodeHandler->getNext($separators); |
|
231 | - |
|
232 | - // using next() ourselves so that we can advance the array pointer inside inner loops |
|
233 | - while (($sep = current($separators)) && $sep[0] !== null) { |
|
234 | - // $sep[0] - the separator string due to PREG_SPLIT_OFFSET_CAPTURE flag or null if end of string |
|
235 | - // $sep[1] - the offset due to PREG_SPLIT_OFFSET_CAPTURE |
|
236 | - |
|
237 | - $innerName = $getMiddleString($sep, $getNext(key($separators))); |
|
238 | - |
|
239 | - $this->parseSeparator($sep, $separators, $result, $varname, $subCodeHandler, $innerName); |
|
240 | - |
|
241 | - next($separators); |
|
242 | - } |
|
243 | - array_push($result, $varname); |
|
244 | - |
|
245 | - return $result; |
|
246 | - } |
|
190 | + // funcall |
|
191 | + case '(': |
|
192 | + $arguments = $handleCodeInbetween(); |
|
193 | + $call = $varname . '(' . implode(', ', $arguments) . ')'; |
|
194 | + $call = static::addDollarIfNeeded($call); |
|
195 | + $varname = $var; |
|
196 | + array_push($result, "{$var}={$call}"); |
|
197 | + break; |
|
198 | + |
|
199 | + case '[': |
|
200 | + if (preg_match('/[a-zA-Z0-9\\\\_\\x7f-\\xff]$/', $varname)) { |
|
201 | + $varname .= $sep[0] . $innerName; |
|
202 | + break; |
|
203 | + } |
|
204 | + case '{': |
|
205 | + $varname .= $this->parseArray($handleCodeInbetween(), $subCodeHandler); |
|
206 | + break; |
|
207 | + |
|
208 | + case '=': |
|
209 | + $varname .= '=' . $this->parseEqual($sep, $separators, $result, $innerName, $subCodeHandler); |
|
210 | + break; |
|
211 | + |
|
212 | + default: |
|
213 | + if (($innerName !== false && $innerName !== '') || $sep[0] !== ')') { |
|
214 | + $varname .= $sep[0] . $innerName; |
|
215 | + } |
|
216 | + break; |
|
217 | + } |
|
218 | + } |
|
219 | + |
|
220 | + protected function parseBetweenSeparators() |
|
221 | + { |
|
222 | + $separators = $this->separators; |
|
223 | + |
|
224 | + $result = array(); |
|
225 | + |
|
226 | + $varname = $this->getVarname($separators[0]); |
|
227 | + |
|
228 | + $subCodeHandler = new SubCodeHandler($this, $this->input, $this->name); |
|
229 | + $getMiddleString = $subCodeHandler->getMiddleString(); |
|
230 | + $getNext = $subCodeHandler->getNext($separators); |
|
231 | + |
|
232 | + // using next() ourselves so that we can advance the array pointer inside inner loops |
|
233 | + while (($sep = current($separators)) && $sep[0] !== null) { |
|
234 | + // $sep[0] - the separator string due to PREG_SPLIT_OFFSET_CAPTURE flag or null if end of string |
|
235 | + // $sep[1] - the offset due to PREG_SPLIT_OFFSET_CAPTURE |
|
236 | + |
|
237 | + $innerName = $getMiddleString($sep, $getNext(key($separators))); |
|
238 | + |
|
239 | + $this->parseSeparator($sep, $separators, $result, $varname, $subCodeHandler, $innerName); |
|
240 | + |
|
241 | + next($separators); |
|
242 | + } |
|
243 | + array_push($result, $varname); |
|
244 | + |
|
245 | + return $result; |
|
246 | + } |
|
247 | 247 | } |
@@ -8,55 +8,55 @@ |
||
8 | 8 | */ |
9 | 9 | abstract class Indenter extends CompilerConfig |
10 | 10 | { |
11 | - /** |
|
12 | - * @var bool |
|
13 | - */ |
|
14 | - protected $prettyprint = false; |
|
15 | - /** |
|
16 | - * @var int |
|
17 | - */ |
|
18 | - protected $indents = 0; |
|
19 | - /** |
|
20 | - * @var int |
|
21 | - */ |
|
22 | - protected $indentSize = 2; |
|
23 | - /** |
|
24 | - * @var string (chr) |
|
25 | - */ |
|
26 | - protected $indentChar = ' '; |
|
11 | + /** |
|
12 | + * @var bool |
|
13 | + */ |
|
14 | + protected $prettyprint = false; |
|
15 | + /** |
|
16 | + * @var int |
|
17 | + */ |
|
18 | + protected $indents = 0; |
|
19 | + /** |
|
20 | + * @var int |
|
21 | + */ |
|
22 | + protected $indentSize = 2; |
|
23 | + /** |
|
24 | + * @var string (chr) |
|
25 | + */ |
|
26 | + protected $indentChar = ' '; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - protected function indent() |
|
32 | - { |
|
33 | - return $this->prettyprint ? str_repeat(str_repeat($this->indentChar, $this->indentSize), $this->indents) : ''; |
|
34 | - } |
|
28 | + /** |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + protected function indent() |
|
32 | + { |
|
33 | + return $this->prettyprint ? str_repeat(str_repeat($this->indentChar, $this->indentSize), $this->indents) : ''; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - protected function newline() |
|
40 | - { |
|
41 | - return $this->prettyprint ? "\n" : ''; |
|
42 | - } |
|
36 | + /** |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + protected function newline() |
|
40 | + { |
|
41 | + return $this->prettyprint ? "\n" : ''; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Disable or enable temporary the prettyprint setting then come back |
|
46 | - * to previous setting. |
|
47 | - * |
|
48 | - * @param bool prettyprint temporary setting |
|
49 | - * @param callable action to execute with the prettyprint setting |
|
50 | - * |
|
51 | - * @return mixed |
|
52 | - */ |
|
53 | - protected function tempPrettyPrint($newSetting, $method) |
|
54 | - { |
|
55 | - $previousSetting = $this->prettyprint; |
|
56 | - $this->prettyprint = $newSetting; |
|
57 | - $result = call_user_func_array(array($this, $method), array_slice(func_get_args(), 2)); |
|
58 | - $this->prettyprint = $previousSetting; |
|
44 | + /** |
|
45 | + * Disable or enable temporary the prettyprint setting then come back |
|
46 | + * to previous setting. |
|
47 | + * |
|
48 | + * @param bool prettyprint temporary setting |
|
49 | + * @param callable action to execute with the prettyprint setting |
|
50 | + * |
|
51 | + * @return mixed |
|
52 | + */ |
|
53 | + protected function tempPrettyPrint($newSetting, $method) |
|
54 | + { |
|
55 | + $previousSetting = $this->prettyprint; |
|
56 | + $this->prettyprint = $newSetting; |
|
57 | + $result = call_user_func_array(array($this, $method), array_slice(func_get_args(), 2)); |
|
58 | + $this->prettyprint = $previousSetting; |
|
59 | 59 | |
60 | - return $result; |
|
61 | - } |
|
60 | + return $result; |
|
61 | + } |
|
62 | 62 | } |
@@ -8,61 +8,61 @@ |
||
8 | 8 | */ |
9 | 9 | class FilterHelper |
10 | 10 | { |
11 | - protected $filters = array(); |
|
12 | - protected $filterAutoLoad = true; |
|
11 | + protected $filters = array(); |
|
12 | + protected $filterAutoLoad = true; |
|
13 | 13 | |
14 | - public function __construct(array $filters, $filterAutoLoad) |
|
15 | - { |
|
16 | - $this->filters = $filters; |
|
17 | - $this->filterAutoLoad = $filterAutoLoad; |
|
18 | - } |
|
14 | + public function __construct(array $filters, $filterAutoLoad) |
|
15 | + { |
|
16 | + $this->filters = $filters; |
|
17 | + $this->filterAutoLoad = $filterAutoLoad; |
|
18 | + } |
|
19 | 19 | |
20 | - /** |
|
21 | - * @param $name |
|
22 | - * |
|
23 | - * @return bool |
|
24 | - */ |
|
25 | - public function hasFilter($name) |
|
26 | - { |
|
27 | - return !is_null($this->getFilter($name)); |
|
28 | - } |
|
20 | + /** |
|
21 | + * @param $name |
|
22 | + * |
|
23 | + * @return bool |
|
24 | + */ |
|
25 | + public function hasFilter($name) |
|
26 | + { |
|
27 | + return !is_null($this->getFilter($name)); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param $name |
|
32 | - * |
|
33 | - * @return callable |
|
34 | - */ |
|
35 | - public function getFilter($name) |
|
36 | - { |
|
37 | - // Check that filter is registered |
|
38 | - if (array_key_exists($name, $this->filters)) { |
|
39 | - return $this->filters[$name]; |
|
40 | - } |
|
30 | + /** |
|
31 | + * @param $name |
|
32 | + * |
|
33 | + * @return callable |
|
34 | + */ |
|
35 | + public function getFilter($name) |
|
36 | + { |
|
37 | + // Check that filter is registered |
|
38 | + if (array_key_exists($name, $this->filters)) { |
|
39 | + return $this->filters[$name]; |
|
40 | + } |
|
41 | 41 | |
42 | - if (!$this->filterAutoLoad) { |
|
43 | - return; |
|
44 | - } |
|
42 | + if (!$this->filterAutoLoad) { |
|
43 | + return; |
|
44 | + } |
|
45 | 45 | |
46 | - // Else check if a class with a name that match can be loaded |
|
47 | - foreach (array('Pug', 'Jade') as $namespace) { |
|
48 | - $filter = $namespace . '\\Filter\\' . implode('', array_map('ucfirst', explode('-', $name))); |
|
49 | - if (class_exists($filter)) { |
|
50 | - return $filter; |
|
51 | - } |
|
52 | - } |
|
53 | - } |
|
46 | + // Else check if a class with a name that match can be loaded |
|
47 | + foreach (array('Pug', 'Jade') as $namespace) { |
|
48 | + $filter = $namespace . '\\Filter\\' . implode('', array_map('ucfirst', explode('-', $name))); |
|
49 | + if (class_exists($filter)) { |
|
50 | + return $filter; |
|
51 | + } |
|
52 | + } |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param $name |
|
57 | - * |
|
58 | - * @return callable |
|
59 | - */ |
|
60 | - public function getValidFilter($name) |
|
61 | - { |
|
62 | - if ($filter = $this->getFilter($name)) { |
|
63 | - return $filter; |
|
64 | - } |
|
55 | + /** |
|
56 | + * @param $name |
|
57 | + * |
|
58 | + * @return callable |
|
59 | + */ |
|
60 | + public function getValidFilter($name) |
|
61 | + { |
|
62 | + if ($filter = $this->getFilter($name)) { |
|
63 | + return $filter; |
|
64 | + } |
|
65 | 65 | |
66 | - throw new \InvalidArgumentException($name . ': Filter doesn\'t exists', 17); |
|
67 | - } |
|
66 | + throw new \InvalidArgumentException($name . ': Filter doesn\'t exists', 17); |
|
67 | + } |
|
68 | 68 | } |
@@ -4,80 +4,80 @@ |
||
4 | 4 | |
5 | 5 | abstract class ValuesCompiler extends CompilerUtils |
6 | 6 | { |
7 | - /** |
|
8 | - * Value treatment if it must not be escaped. |
|
9 | - * |
|
10 | - * @param string input value |
|
11 | - * |
|
12 | - * @return string |
|
13 | - */ |
|
14 | - public static function getUnescapedValue($val) |
|
15 | - { |
|
16 | - if (is_null($val) || $val === false || $val === '') { |
|
17 | - return ''; |
|
18 | - } |
|
7 | + /** |
|
8 | + * Value treatment if it must not be escaped. |
|
9 | + * |
|
10 | + * @param string input value |
|
11 | + * |
|
12 | + * @return string |
|
13 | + */ |
|
14 | + public static function getUnescapedValue($val) |
|
15 | + { |
|
16 | + if (is_null($val) || $val === false || $val === '') { |
|
17 | + return ''; |
|
18 | + } |
|
19 | 19 | |
20 | - return is_array($val) || is_bool($val) || is_int($val) || is_float($val) ? json_encode($val) : strval($val); |
|
21 | - } |
|
20 | + return is_array($val) || is_bool($val) || is_int($val) || is_float($val) ? json_encode($val) : strval($val); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * Value treatment if it must be escaped. |
|
25 | - * |
|
26 | - * @param string input value |
|
27 | - * |
|
28 | - * @return string |
|
29 | - */ |
|
30 | - public static function getEscapedValue($val, $quote) |
|
31 | - { |
|
32 | - $val = htmlspecialchars(static::getUnescapedValue($val), ENT_NOQUOTES); |
|
23 | + /** |
|
24 | + * Value treatment if it must be escaped. |
|
25 | + * |
|
26 | + * @param string input value |
|
27 | + * |
|
28 | + * @return string |
|
29 | + */ |
|
30 | + public static function getEscapedValue($val, $quote) |
|
31 | + { |
|
32 | + $val = htmlspecialchars(static::getUnescapedValue($val), ENT_NOQUOTES); |
|
33 | 33 | |
34 | - return str_replace($quote, $quote === '"' ? '"' : ''', $val); |
|
35 | - } |
|
34 | + return str_replace($quote, $quote === '"' ? '"' : ''', $val); |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Convert style object to CSS string. |
|
39 | - * |
|
40 | - * @param mixed value to be computed into style. |
|
41 | - * |
|
42 | - * @return mixed |
|
43 | - */ |
|
44 | - public static function styleValue($val) |
|
45 | - { |
|
46 | - if (is_array($val) && !is_string(key($val))) { |
|
47 | - $val = implode(';', $val); |
|
48 | - } elseif (is_array($val) || is_object($val)) { |
|
49 | - $style = array(); |
|
50 | - foreach ($val as $key => $property) { |
|
51 | - $style[] = $key . ':' . $property; |
|
52 | - } |
|
37 | + /** |
|
38 | + * Convert style object to CSS string. |
|
39 | + * |
|
40 | + * @param mixed value to be computed into style. |
|
41 | + * |
|
42 | + * @return mixed |
|
43 | + */ |
|
44 | + public static function styleValue($val) |
|
45 | + { |
|
46 | + if (is_array($val) && !is_string(key($val))) { |
|
47 | + $val = implode(';', $val); |
|
48 | + } elseif (is_array($val) || is_object($val)) { |
|
49 | + $style = array(); |
|
50 | + foreach ($val as $key => $property) { |
|
51 | + $style[] = $key . ':' . $property; |
|
52 | + } |
|
53 | 53 | |
54 | - $val = implode(';', $style); |
|
55 | - } |
|
54 | + $val = implode(';', $style); |
|
55 | + } |
|
56 | 56 | |
57 | - return $val; |
|
58 | - } |
|
57 | + return $val; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Convert style object to CSS string and return PHP code to escape then display it. |
|
62 | - * |
|
63 | - * @param mixed value to be computed into style and escaped. |
|
64 | - * |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public static function getEscapedStyle($val, $quote) |
|
68 | - { |
|
69 | - return static::getEscapedValue(static::styleValue($val), $quote); |
|
70 | - } |
|
60 | + /** |
|
61 | + * Convert style object to CSS string and return PHP code to escape then display it. |
|
62 | + * |
|
63 | + * @param mixed value to be computed into style and escaped. |
|
64 | + * |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public static function getEscapedStyle($val, $quote) |
|
68 | + { |
|
69 | + return static::getEscapedValue(static::styleValue($val), $quote); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Convert style object to CSS string and return PHP code to display it. |
|
74 | - * |
|
75 | - * @param mixed value to be computed into style and stringified. |
|
76 | - * |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - public static function getUnescapedStyle($val) |
|
80 | - { |
|
81 | - return static::getUnescapedValue(static::styleValue($val)); |
|
82 | - } |
|
72 | + /** |
|
73 | + * Convert style object to CSS string and return PHP code to display it. |
|
74 | + * |
|
75 | + * @param mixed value to be computed into style and stringified. |
|
76 | + * |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + public static function getUnescapedStyle($val) |
|
80 | + { |
|
81 | + return static::getUnescapedValue(static::styleValue($val)); |
|
82 | + } |
|
83 | 83 | } |
@@ -7,168 +7,168 @@ |
||
7 | 7 | */ |
8 | 8 | class SubCodeHandler |
9 | 9 | { |
10 | - protected $codeHandler; |
|
11 | - protected $input; |
|
12 | - protected $name; |
|
13 | - |
|
14 | - public function __construct(CodeHandler $codeHandler, $input, $name) |
|
15 | - { |
|
16 | - $this->codeHandler = $codeHandler; |
|
17 | - $this->input = $input; |
|
18 | - $this->name = $name; |
|
19 | - } |
|
20 | - |
|
21 | - public function getMiddleString() |
|
22 | - { |
|
23 | - $input = $this->input; |
|
24 | - |
|
25 | - return function ($start, $end) use ($input) { |
|
26 | - $offset = $start[1] + strlen($start[0]); |
|
27 | - |
|
28 | - return substr($input, $offset, isset($end) ? $end[1] - $offset : strlen($input)); |
|
29 | - }; |
|
30 | - } |
|
31 | - |
|
32 | - public function handleRecursion(&$result) |
|
33 | - { |
|
34 | - $getMiddleString = $this->getMiddleString(); |
|
35 | - $codeHandler = $this->codeHandler; |
|
36 | - |
|
37 | - return function ($arg, $name = '') use (&$result, $codeHandler, $getMiddleString) { |
|
38 | - list($start, $end) = $arg; |
|
39 | - $str = trim($getMiddleString($start, $end)); |
|
40 | - |
|
41 | - if (!strlen($str)) { |
|
42 | - return ''; |
|
43 | - } |
|
44 | - |
|
45 | - $innerCode = $codeHandler->innerCode($str, $name); |
|
46 | - |
|
47 | - if (count($innerCode) > 1) { |
|
48 | - $result = array_merge($result, array_slice($innerCode, 0, -1)); |
|
49 | - |
|
50 | - return array_pop($innerCode); |
|
51 | - } |
|
52 | - |
|
53 | - return $innerCode[0]; |
|
54 | - }; |
|
55 | - } |
|
56 | - |
|
57 | - protected function handleNestedExpression(&$result) |
|
58 | - { |
|
59 | - $handleRecursion = $this->handleRecursion($result); |
|
60 | - $name = $this->name; |
|
61 | - |
|
62 | - return function (&$arguments, $start, $end) use ($name, $handleRecursion) { |
|
63 | - if ($end !== false && $start[1] !== $end[1]) { |
|
64 | - array_push( |
|
65 | - $arguments, |
|
66 | - $handleRecursion( |
|
67 | - array($start, $end), |
|
68 | - $name * 10 + count($arguments) |
|
69 | - ) |
|
70 | - ); |
|
71 | - } |
|
72 | - }; |
|
73 | - } |
|
74 | - |
|
75 | - protected function scanSeparators(&$separators, &$result) |
|
76 | - { |
|
77 | - $handleNested = $this->handleNestedExpression($result); |
|
78 | - |
|
79 | - return function (&$arguments, $open, $close) use (&$separators, $handleNested) { |
|
80 | - $count = 1; |
|
81 | - |
|
82 | - do { |
|
83 | - $start = current($separators); |
|
84 | - |
|
85 | - do { |
|
86 | - $curr = next($separators); |
|
87 | - |
|
88 | - if ($curr[0] === $open) { |
|
89 | - $count++; |
|
90 | - } |
|
91 | - if ($curr[0] === $close) { |
|
92 | - $count--; |
|
93 | - } |
|
94 | - } while ($curr[0] !== null && $count > 0 && $curr[0] !== ','); |
|
95 | - |
|
96 | - $handleNested($arguments, $start, current($separators)); |
|
97 | - } while ($curr !== false && $count > 0); |
|
98 | - |
|
99 | - return $count; |
|
100 | - }; |
|
101 | - } |
|
102 | - |
|
103 | - public function handleCodeInbetween(&$separators, &$result) |
|
104 | - { |
|
105 | - $scanSeparators = $this->scanSeparators($separators, $result); |
|
106 | - $input = $this->input; |
|
107 | - |
|
108 | - return function () use (&$separators, $input, $scanSeparators) { |
|
109 | - $arguments = array(); |
|
110 | - |
|
111 | - $start = current($separators); |
|
112 | - $endPair = array( |
|
113 | - '[' => ']', |
|
114 | - '{' => '}', |
|
115 | - '(' => ')', |
|
116 | - ',' => false, |
|
117 | - ); |
|
118 | - $open = $start[0]; |
|
119 | - $close = $endPair[$start[0]]; |
|
120 | - |
|
121 | - $count = $scanSeparators($arguments, $open, $close); |
|
122 | - |
|
123 | - if ($close && $count > 0) { |
|
124 | - throw new \ErrorException($input . "\nMissing closing: " . $close, 14); |
|
125 | - } |
|
126 | - |
|
127 | - if (($sep = current($separators)) !== false) { |
|
128 | - $end = next($separators); |
|
129 | - if ($end[0] === null && $sep[1] < $end[1]) { |
|
130 | - $key = count($arguments) - 1; |
|
131 | - $arguments[$key] .= substr($input, $sep[1] + 1, $end[1] - $sep[1] - 1); |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - return $arguments; |
|
136 | - }; |
|
137 | - } |
|
138 | - |
|
139 | - public function getNext($separators) |
|
140 | - { |
|
141 | - return function ($index) use ($separators) { |
|
142 | - if (isset($separators[$index + 1])) { |
|
143 | - return $separators[$index + 1]; |
|
144 | - } |
|
145 | - }; |
|
146 | - } |
|
147 | - |
|
148 | - public function addToOutput(&$output, &$key, &$value) |
|
149 | - { |
|
150 | - return function () use (&$output, &$key, &$value) { |
|
151 | - foreach (array('key', 'value') as $var) { |
|
152 | - ${$var} = trim(${$var}); |
|
153 | - if (empty(${$var})) { |
|
154 | - continue; |
|
155 | - } |
|
156 | - if (preg_match('/^\d*[a-zA-Z_]/', ${$var})) { |
|
157 | - ${$var} = var_export(${$var}, true); |
|
158 | - } |
|
159 | - } |
|
160 | - $output[] = empty($value) |
|
161 | - ? $key |
|
162 | - : $key . ' => ' . $value; |
|
163 | - $key = ''; |
|
164 | - $value = null; |
|
165 | - }; |
|
166 | - } |
|
167 | - |
|
168 | - public function consume() |
|
169 | - { |
|
170 | - return function (&$argument, $start) { |
|
171 | - $argument = substr($argument, strlen($start)); |
|
172 | - }; |
|
173 | - } |
|
10 | + protected $codeHandler; |
|
11 | + protected $input; |
|
12 | + protected $name; |
|
13 | + |
|
14 | + public function __construct(CodeHandler $codeHandler, $input, $name) |
|
15 | + { |
|
16 | + $this->codeHandler = $codeHandler; |
|
17 | + $this->input = $input; |
|
18 | + $this->name = $name; |
|
19 | + } |
|
20 | + |
|
21 | + public function getMiddleString() |
|
22 | + { |
|
23 | + $input = $this->input; |
|
24 | + |
|
25 | + return function ($start, $end) use ($input) { |
|
26 | + $offset = $start[1] + strlen($start[0]); |
|
27 | + |
|
28 | + return substr($input, $offset, isset($end) ? $end[1] - $offset : strlen($input)); |
|
29 | + }; |
|
30 | + } |
|
31 | + |
|
32 | + public function handleRecursion(&$result) |
|
33 | + { |
|
34 | + $getMiddleString = $this->getMiddleString(); |
|
35 | + $codeHandler = $this->codeHandler; |
|
36 | + |
|
37 | + return function ($arg, $name = '') use (&$result, $codeHandler, $getMiddleString) { |
|
38 | + list($start, $end) = $arg; |
|
39 | + $str = trim($getMiddleString($start, $end)); |
|
40 | + |
|
41 | + if (!strlen($str)) { |
|
42 | + return ''; |
|
43 | + } |
|
44 | + |
|
45 | + $innerCode = $codeHandler->innerCode($str, $name); |
|
46 | + |
|
47 | + if (count($innerCode) > 1) { |
|
48 | + $result = array_merge($result, array_slice($innerCode, 0, -1)); |
|
49 | + |
|
50 | + return array_pop($innerCode); |
|
51 | + } |
|
52 | + |
|
53 | + return $innerCode[0]; |
|
54 | + }; |
|
55 | + } |
|
56 | + |
|
57 | + protected function handleNestedExpression(&$result) |
|
58 | + { |
|
59 | + $handleRecursion = $this->handleRecursion($result); |
|
60 | + $name = $this->name; |
|
61 | + |
|
62 | + return function (&$arguments, $start, $end) use ($name, $handleRecursion) { |
|
63 | + if ($end !== false && $start[1] !== $end[1]) { |
|
64 | + array_push( |
|
65 | + $arguments, |
|
66 | + $handleRecursion( |
|
67 | + array($start, $end), |
|
68 | + $name * 10 + count($arguments) |
|
69 | + ) |
|
70 | + ); |
|
71 | + } |
|
72 | + }; |
|
73 | + } |
|
74 | + |
|
75 | + protected function scanSeparators(&$separators, &$result) |
|
76 | + { |
|
77 | + $handleNested = $this->handleNestedExpression($result); |
|
78 | + |
|
79 | + return function (&$arguments, $open, $close) use (&$separators, $handleNested) { |
|
80 | + $count = 1; |
|
81 | + |
|
82 | + do { |
|
83 | + $start = current($separators); |
|
84 | + |
|
85 | + do { |
|
86 | + $curr = next($separators); |
|
87 | + |
|
88 | + if ($curr[0] === $open) { |
|
89 | + $count++; |
|
90 | + } |
|
91 | + if ($curr[0] === $close) { |
|
92 | + $count--; |
|
93 | + } |
|
94 | + } while ($curr[0] !== null && $count > 0 && $curr[0] !== ','); |
|
95 | + |
|
96 | + $handleNested($arguments, $start, current($separators)); |
|
97 | + } while ($curr !== false && $count > 0); |
|
98 | + |
|
99 | + return $count; |
|
100 | + }; |
|
101 | + } |
|
102 | + |
|
103 | + public function handleCodeInbetween(&$separators, &$result) |
|
104 | + { |
|
105 | + $scanSeparators = $this->scanSeparators($separators, $result); |
|
106 | + $input = $this->input; |
|
107 | + |
|
108 | + return function () use (&$separators, $input, $scanSeparators) { |
|
109 | + $arguments = array(); |
|
110 | + |
|
111 | + $start = current($separators); |
|
112 | + $endPair = array( |
|
113 | + '[' => ']', |
|
114 | + '{' => '}', |
|
115 | + '(' => ')', |
|
116 | + ',' => false, |
|
117 | + ); |
|
118 | + $open = $start[0]; |
|
119 | + $close = $endPair[$start[0]]; |
|
120 | + |
|
121 | + $count = $scanSeparators($arguments, $open, $close); |
|
122 | + |
|
123 | + if ($close && $count > 0) { |
|
124 | + throw new \ErrorException($input . "\nMissing closing: " . $close, 14); |
|
125 | + } |
|
126 | + |
|
127 | + if (($sep = current($separators)) !== false) { |
|
128 | + $end = next($separators); |
|
129 | + if ($end[0] === null && $sep[1] < $end[1]) { |
|
130 | + $key = count($arguments) - 1; |
|
131 | + $arguments[$key] .= substr($input, $sep[1] + 1, $end[1] - $sep[1] - 1); |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + return $arguments; |
|
136 | + }; |
|
137 | + } |
|
138 | + |
|
139 | + public function getNext($separators) |
|
140 | + { |
|
141 | + return function ($index) use ($separators) { |
|
142 | + if (isset($separators[$index + 1])) { |
|
143 | + return $separators[$index + 1]; |
|
144 | + } |
|
145 | + }; |
|
146 | + } |
|
147 | + |
|
148 | + public function addToOutput(&$output, &$key, &$value) |
|
149 | + { |
|
150 | + return function () use (&$output, &$key, &$value) { |
|
151 | + foreach (array('key', 'value') as $var) { |
|
152 | + ${$var} = trim(${$var}); |
|
153 | + if (empty(${$var})) { |
|
154 | + continue; |
|
155 | + } |
|
156 | + if (preg_match('/^\d*[a-zA-Z_]/', ${$var})) { |
|
157 | + ${$var} = var_export(${$var}, true); |
|
158 | + } |
|
159 | + } |
|
160 | + $output[] = empty($value) |
|
161 | + ? $key |
|
162 | + : $key . ' => ' . $value; |
|
163 | + $key = ''; |
|
164 | + $value = null; |
|
165 | + }; |
|
166 | + } |
|
167 | + |
|
168 | + public function consume() |
|
169 | + { |
|
170 | + return function (&$argument, $start) { |
|
171 | + $argument = substr($argument, strlen($start)); |
|
172 | + }; |
|
173 | + } |
|
174 | 174 | } |
@@ -10,45 +10,45 @@ |
||
10 | 10 | */ |
11 | 11 | abstract class AbstractFilter implements FilterInterface |
12 | 12 | { |
13 | - /** |
|
14 | - * Returns the node string value, line by line. |
|
15 | - * If the compiler is present, that means we need |
|
16 | - * to interpolate line contents. |
|
17 | - * |
|
18 | - * @param Filter $node |
|
19 | - * @param Compiler $compiler |
|
20 | - * |
|
21 | - * @return mixed |
|
22 | - */ |
|
23 | - protected function getNodeString(Filter $node, Compiler $compiler = null) |
|
24 | - { |
|
25 | - return array_reduce($node->block->nodes, function ($result, $line) use ($compiler) { |
|
26 | - return $result . ($compiler |
|
27 | - ? $compiler->interpolate($line->value) |
|
28 | - : $line->value |
|
29 | - ) . "\n"; |
|
30 | - }); |
|
31 | - } |
|
13 | + /** |
|
14 | + * Returns the node string value, line by line. |
|
15 | + * If the compiler is present, that means we need |
|
16 | + * to interpolate line contents. |
|
17 | + * |
|
18 | + * @param Filter $node |
|
19 | + * @param Compiler $compiler |
|
20 | + * |
|
21 | + * @return mixed |
|
22 | + */ |
|
23 | + protected function getNodeString(Filter $node, Compiler $compiler = null) |
|
24 | + { |
|
25 | + return array_reduce($node->block->nodes, function ($result, $line) use ($compiler) { |
|
26 | + return $result . ($compiler |
|
27 | + ? $compiler->interpolate($line->value) |
|
28 | + : $line->value |
|
29 | + ) . "\n"; |
|
30 | + }); |
|
31 | + } |
|
32 | 32 | |
33 | - public function __invoke(Filter $node, Compiler $compiler) |
|
34 | - { |
|
35 | - $nodes = $node->block->nodes; |
|
36 | - $indent = strlen($nodes[0]->value) - strlen(ltrim($nodes[0]->value)); |
|
37 | - $code = ''; |
|
38 | - foreach ($nodes as $line) { |
|
39 | - $code .= substr($compiler->interpolate($line->value), $indent) . "\n"; |
|
40 | - } |
|
33 | + public function __invoke(Filter $node, Compiler $compiler) |
|
34 | + { |
|
35 | + $nodes = $node->block->nodes; |
|
36 | + $indent = strlen($nodes[0]->value) - strlen(ltrim($nodes[0]->value)); |
|
37 | + $code = ''; |
|
38 | + foreach ($nodes as $line) { |
|
39 | + $code .= substr($compiler->interpolate($line->value), $indent) . "\n"; |
|
40 | + } |
|
41 | 41 | |
42 | - if (method_exists($this, 'parse')) { |
|
43 | - $code = $this->parse($code); |
|
44 | - } |
|
42 | + if (method_exists($this, 'parse')) { |
|
43 | + $code = $this->parse($code); |
|
44 | + } |
|
45 | 45 | |
46 | - if (isset($this->tag)) { |
|
47 | - $code = '<' . $this->tag . (isset($this->textType) ? ' type="text/' . $this->textType . '"' : '') . '>' . |
|
48 | - $code . |
|
49 | - '</' . $this->tag . '>'; |
|
50 | - } |
|
46 | + if (isset($this->tag)) { |
|
47 | + $code = '<' . $this->tag . (isset($this->textType) ? ' type="text/' . $this->textType . '"' : '') . '>' . |
|
48 | + $code . |
|
49 | + '</' . $this->tag . '>'; |
|
50 | + } |
|
51 | 51 | |
52 | - return $code; |
|
53 | - } |
|
52 | + return $code; |
|
53 | + } |
|
54 | 54 | } |
@@ -10,14 +10,14 @@ |
||
10 | 10 | */ |
11 | 11 | class Javascript extends AbstractFilter |
12 | 12 | { |
13 | - /** |
|
14 | - * @param Filter $node |
|
15 | - * @param Compiler $compiler |
|
16 | - * |
|
17 | - * @return string |
|
18 | - */ |
|
19 | - public function __invoke(Filter $node, Compiler $compiler) |
|
20 | - { |
|
21 | - return '<script type="text/javascript">' . $this->getNodeString($node, $compiler) . '</script>'; |
|
22 | - } |
|
13 | + /** |
|
14 | + * @param Filter $node |
|
15 | + * @param Compiler $compiler |
|
16 | + * |
|
17 | + * @return string |
|
18 | + */ |
|
19 | + public function __invoke(Filter $node, Compiler $compiler) |
|
20 | + { |
|
21 | + return '<script type="text/javascript">' . $this->getNodeString($node, $compiler) . '</script>'; |
|
22 | + } |
|
23 | 23 | } |
@@ -7,8 +7,8 @@ |
||
7 | 7 | |
8 | 8 | class Css extends AbstractFilter |
9 | 9 | { |
10 | - public function __invoke(Filter $node, Compiler $compiler) |
|
11 | - { |
|
12 | - return '<style type="text/css">' . $this->getNodeString($node, $compiler) . '</style>'; |
|
13 | - } |
|
10 | + public function __invoke(Filter $node, Compiler $compiler) |
|
11 | + { |
|
12 | + return '<style type="text/css">' . $this->getNodeString($node, $compiler) . '</style>'; |
|
13 | + } |
|
14 | 14 | } |
@@ -10,5 +10,5 @@ |
||
10 | 10 | */ |
11 | 11 | interface FilterInterface |
12 | 12 | { |
13 | - public function __invoke(Filter $filter, Compiler $compiler); |
|
13 | + public function __invoke(Filter $filter, Compiler $compiler); |
|
14 | 14 | } |