@@ -341,6 +341,10 @@ |
||
341 | 341 | return $file; |
342 | 342 | } |
343 | 343 | |
344 | + /** |
|
345 | + * @param string $class |
|
346 | + * @param string $ext |
|
347 | + */ |
|
344 | 348 | private function findFileWithExtension($class, $ext) |
345 | 349 | { |
346 | 350 | // PSR-4 lookup |
@@ -42,364 +42,364 @@ discard block |
||
42 | 42 | */ |
43 | 43 | class ClassLoader |
44 | 44 | { |
45 | - // PSR-4 |
|
46 | - private $prefixLengthsPsr4 = array(); |
|
47 | - private $prefixDirsPsr4 = array(); |
|
48 | - private $fallbackDirsPsr4 = array(); |
|
49 | - |
|
50 | - // PSR-0 |
|
51 | - private $prefixesPsr0 = array(); |
|
52 | - private $fallbackDirsPsr0 = array(); |
|
53 | - |
|
54 | - private $useIncludePath = false; |
|
55 | - private $classMap = array(); |
|
56 | - |
|
57 | - private $classMapAuthoritative = false; |
|
58 | - |
|
59 | - public function getPrefixes() |
|
60 | - { |
|
61 | - if (!empty($this->prefixesPsr0)) { |
|
62 | - return call_user_func_array('array_merge', $this->prefixesPsr0); |
|
63 | - } |
|
64 | - |
|
65 | - return array(); |
|
66 | - } |
|
67 | - |
|
68 | - public function getPrefixesPsr4() |
|
69 | - { |
|
70 | - return $this->prefixDirsPsr4; |
|
71 | - } |
|
72 | - |
|
73 | - public function getFallbackDirs() |
|
74 | - { |
|
75 | - return $this->fallbackDirsPsr0; |
|
76 | - } |
|
77 | - |
|
78 | - public function getFallbackDirsPsr4() |
|
79 | - { |
|
80 | - return $this->fallbackDirsPsr4; |
|
81 | - } |
|
82 | - |
|
83 | - public function getClassMap() |
|
84 | - { |
|
85 | - return $this->classMap; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @param array $classMap Class to filename map |
|
90 | - */ |
|
91 | - public function addClassMap(array $classMap) |
|
92 | - { |
|
93 | - if ($this->classMap) { |
|
94 | - $this->classMap = array_merge($this->classMap, $classMap); |
|
95 | - } else { |
|
96 | - $this->classMap = $classMap; |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Registers a set of PSR-0 directories for a given prefix, either |
|
102 | - * appending or prepending to the ones previously set for this prefix. |
|
103 | - * |
|
104 | - * @param string $prefix The prefix |
|
105 | - * @param array|string $paths The PSR-0 root directories |
|
106 | - * @param bool $prepend Whether to prepend the directories |
|
107 | - */ |
|
108 | - public function add($prefix, $paths, $prepend = false) |
|
109 | - { |
|
110 | - if (!$prefix) { |
|
111 | - if ($prepend) { |
|
112 | - $this->fallbackDirsPsr0 = array_merge( |
|
113 | - (array) $paths, |
|
114 | - $this->fallbackDirsPsr0 |
|
115 | - ); |
|
116 | - } else { |
|
117 | - $this->fallbackDirsPsr0 = array_merge( |
|
118 | - $this->fallbackDirsPsr0, |
|
119 | - (array) $paths |
|
120 | - ); |
|
121 | - } |
|
122 | - |
|
123 | - return; |
|
124 | - } |
|
125 | - |
|
126 | - $first = $prefix[0]; |
|
127 | - if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
128 | - $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
|
129 | - |
|
130 | - return; |
|
131 | - } |
|
132 | - if ($prepend) { |
|
133 | - $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
134 | - (array) $paths, |
|
135 | - $this->prefixesPsr0[$first][$prefix] |
|
136 | - ); |
|
137 | - } else { |
|
138 | - $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
139 | - $this->prefixesPsr0[$first][$prefix], |
|
140 | - (array) $paths |
|
141 | - ); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Registers a set of PSR-4 directories for a given namespace, either |
|
147 | - * appending or prepending to the ones previously set for this namespace. |
|
148 | - * |
|
149 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
150 | - * @param array|string $paths The PSR-4 base directories |
|
151 | - * @param bool $prepend Whether to prepend the directories |
|
152 | - * |
|
153 | - * @throws \InvalidArgumentException |
|
154 | - */ |
|
155 | - public function addPsr4($prefix, $paths, $prepend = false) |
|
156 | - { |
|
157 | - if (!$prefix) { |
|
158 | - // Register directories for the root namespace. |
|
159 | - if ($prepend) { |
|
160 | - $this->fallbackDirsPsr4 = array_merge( |
|
161 | - (array) $paths, |
|
162 | - $this->fallbackDirsPsr4 |
|
163 | - ); |
|
164 | - } else { |
|
165 | - $this->fallbackDirsPsr4 = array_merge( |
|
166 | - $this->fallbackDirsPsr4, |
|
167 | - (array) $paths |
|
168 | - ); |
|
169 | - } |
|
170 | - } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
171 | - // Register directories for a new namespace. |
|
172 | - $length = strlen($prefix); |
|
173 | - if ('\\' !== $prefix[$length - 1]) { |
|
174 | - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
175 | - } |
|
176 | - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
177 | - $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
178 | - } elseif ($prepend) { |
|
179 | - // Prepend directories for an already registered namespace. |
|
180 | - $this->prefixDirsPsr4[$prefix] = array_merge( |
|
181 | - (array) $paths, |
|
182 | - $this->prefixDirsPsr4[$prefix] |
|
183 | - ); |
|
184 | - } else { |
|
185 | - // Append directories for an already registered namespace. |
|
186 | - $this->prefixDirsPsr4[$prefix] = array_merge( |
|
187 | - $this->prefixDirsPsr4[$prefix], |
|
188 | - (array) $paths |
|
189 | - ); |
|
190 | - } |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Registers a set of PSR-0 directories for a given prefix, |
|
195 | - * replacing any others previously set for this prefix. |
|
196 | - * |
|
197 | - * @param string $prefix The prefix |
|
198 | - * @param array|string $paths The PSR-0 base directories |
|
199 | - */ |
|
200 | - public function set($prefix, $paths) |
|
201 | - { |
|
202 | - if (!$prefix) { |
|
203 | - $this->fallbackDirsPsr0 = (array) $paths; |
|
204 | - } else { |
|
205 | - $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Registers a set of PSR-4 directories for a given namespace, |
|
211 | - * replacing any others previously set for this namespace. |
|
212 | - * |
|
213 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
214 | - * @param array|string $paths The PSR-4 base directories |
|
215 | - * |
|
216 | - * @throws \InvalidArgumentException |
|
217 | - */ |
|
218 | - public function setPsr4($prefix, $paths) |
|
219 | - { |
|
220 | - if (!$prefix) { |
|
221 | - $this->fallbackDirsPsr4 = (array) $paths; |
|
222 | - } else { |
|
223 | - $length = strlen($prefix); |
|
224 | - if ('\\' !== $prefix[$length - 1]) { |
|
225 | - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
226 | - } |
|
227 | - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
228 | - $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
229 | - } |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Turns on searching the include path for class files. |
|
234 | - * |
|
235 | - * @param bool $useIncludePath |
|
236 | - */ |
|
237 | - public function setUseIncludePath($useIncludePath) |
|
238 | - { |
|
239 | - $this->useIncludePath = $useIncludePath; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Can be used to check if the autoloader uses the include path to check |
|
244 | - * for classes. |
|
245 | - * |
|
246 | - * @return bool |
|
247 | - */ |
|
248 | - public function getUseIncludePath() |
|
249 | - { |
|
250 | - return $this->useIncludePath; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Turns off searching the prefix and fallback directories for classes |
|
255 | - * that have not been registered with the class map. |
|
256 | - * |
|
257 | - * @param bool $classMapAuthoritative |
|
258 | - */ |
|
259 | - public function setClassMapAuthoritative($classMapAuthoritative) |
|
260 | - { |
|
261 | - $this->classMapAuthoritative = $classMapAuthoritative; |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Should class lookup fail if not found in the current class map? |
|
266 | - * |
|
267 | - * @return bool |
|
268 | - */ |
|
269 | - public function isClassMapAuthoritative() |
|
270 | - { |
|
271 | - return $this->classMapAuthoritative; |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Registers this instance as an autoloader. |
|
276 | - * |
|
277 | - * @param bool $prepend Whether to prepend the autoloader or not |
|
278 | - */ |
|
279 | - public function register($prepend = false) |
|
280 | - { |
|
281 | - spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * Unregisters this instance as an autoloader. |
|
286 | - */ |
|
287 | - public function unregister() |
|
288 | - { |
|
289 | - spl_autoload_unregister(array($this, 'loadClass')); |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Loads the given class or interface. |
|
294 | - * |
|
295 | - * @param string $class The name of the class |
|
296 | - * @return bool|null True if loaded, null otherwise |
|
297 | - */ |
|
298 | - public function loadClass($class) |
|
299 | - { |
|
300 | - if ($file = $this->findFile($class)) { |
|
301 | - includeFile($file); |
|
302 | - |
|
303 | - return true; |
|
304 | - } |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Finds the path to the file where the class is defined. |
|
309 | - * |
|
310 | - * @param string $class The name of the class |
|
311 | - * |
|
312 | - * @return string|false The path if found, false otherwise |
|
313 | - */ |
|
314 | - public function findFile($class) |
|
315 | - { |
|
316 | - // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731 |
|
317 | - if ('\\' == $class[0]) { |
|
318 | - $class = substr($class, 1); |
|
319 | - } |
|
320 | - |
|
321 | - // class map lookup |
|
322 | - if (isset($this->classMap[$class])) { |
|
323 | - return $this->classMap[$class]; |
|
324 | - } |
|
325 | - if ($this->classMapAuthoritative) { |
|
326 | - return false; |
|
327 | - } |
|
328 | - |
|
329 | - $file = $this->findFileWithExtension($class, '.php'); |
|
330 | - |
|
331 | - // Search for Hack files if we are running on HHVM |
|
332 | - if ($file === null && defined('HHVM_VERSION')) { |
|
333 | - $file = $this->findFileWithExtension($class, '.hh'); |
|
334 | - } |
|
335 | - |
|
336 | - if ($file === null) { |
|
337 | - // Remember that this class does not exist. |
|
338 | - return $this->classMap[$class] = false; |
|
339 | - } |
|
340 | - |
|
341 | - return $file; |
|
342 | - } |
|
343 | - |
|
344 | - private function findFileWithExtension($class, $ext) |
|
345 | - { |
|
346 | - // PSR-4 lookup |
|
347 | - $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
348 | - |
|
349 | - $first = $class[0]; |
|
350 | - if (isset($this->prefixLengthsPsr4[$first])) { |
|
351 | - foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) { |
|
352 | - if (0 === strpos($class, $prefix)) { |
|
353 | - foreach ($this->prefixDirsPsr4[$prefix] as $dir) { |
|
354 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) { |
|
355 | - return $file; |
|
356 | - } |
|
357 | - } |
|
358 | - } |
|
359 | - } |
|
360 | - } |
|
361 | - |
|
362 | - // PSR-4 fallback dirs |
|
363 | - foreach ($this->fallbackDirsPsr4 as $dir) { |
|
364 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
365 | - return $file; |
|
366 | - } |
|
367 | - } |
|
368 | - |
|
369 | - // PSR-0 lookup |
|
370 | - if (false !== $pos = strrpos($class, '\\')) { |
|
371 | - // namespaced class name |
|
372 | - $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
|
373 | - . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
|
374 | - } else { |
|
375 | - // PEAR-like class name |
|
376 | - $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
377 | - } |
|
378 | - |
|
379 | - if (isset($this->prefixesPsr0[$first])) { |
|
380 | - foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
|
381 | - if (0 === strpos($class, $prefix)) { |
|
382 | - foreach ($dirs as $dir) { |
|
383 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
384 | - return $file; |
|
385 | - } |
|
386 | - } |
|
387 | - } |
|
388 | - } |
|
389 | - } |
|
390 | - |
|
391 | - // PSR-0 fallback dirs |
|
392 | - foreach ($this->fallbackDirsPsr0 as $dir) { |
|
393 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
394 | - return $file; |
|
395 | - } |
|
396 | - } |
|
397 | - |
|
398 | - // PSR-0 include paths. |
|
399 | - if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
|
400 | - return $file; |
|
401 | - } |
|
402 | - } |
|
45 | + // PSR-4 |
|
46 | + private $prefixLengthsPsr4 = array(); |
|
47 | + private $prefixDirsPsr4 = array(); |
|
48 | + private $fallbackDirsPsr4 = array(); |
|
49 | + |
|
50 | + // PSR-0 |
|
51 | + private $prefixesPsr0 = array(); |
|
52 | + private $fallbackDirsPsr0 = array(); |
|
53 | + |
|
54 | + private $useIncludePath = false; |
|
55 | + private $classMap = array(); |
|
56 | + |
|
57 | + private $classMapAuthoritative = false; |
|
58 | + |
|
59 | + public function getPrefixes() |
|
60 | + { |
|
61 | + if (!empty($this->prefixesPsr0)) { |
|
62 | + return call_user_func_array('array_merge', $this->prefixesPsr0); |
|
63 | + } |
|
64 | + |
|
65 | + return array(); |
|
66 | + } |
|
67 | + |
|
68 | + public function getPrefixesPsr4() |
|
69 | + { |
|
70 | + return $this->prefixDirsPsr4; |
|
71 | + } |
|
72 | + |
|
73 | + public function getFallbackDirs() |
|
74 | + { |
|
75 | + return $this->fallbackDirsPsr0; |
|
76 | + } |
|
77 | + |
|
78 | + public function getFallbackDirsPsr4() |
|
79 | + { |
|
80 | + return $this->fallbackDirsPsr4; |
|
81 | + } |
|
82 | + |
|
83 | + public function getClassMap() |
|
84 | + { |
|
85 | + return $this->classMap; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @param array $classMap Class to filename map |
|
90 | + */ |
|
91 | + public function addClassMap(array $classMap) |
|
92 | + { |
|
93 | + if ($this->classMap) { |
|
94 | + $this->classMap = array_merge($this->classMap, $classMap); |
|
95 | + } else { |
|
96 | + $this->classMap = $classMap; |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Registers a set of PSR-0 directories for a given prefix, either |
|
102 | + * appending or prepending to the ones previously set for this prefix. |
|
103 | + * |
|
104 | + * @param string $prefix The prefix |
|
105 | + * @param array|string $paths The PSR-0 root directories |
|
106 | + * @param bool $prepend Whether to prepend the directories |
|
107 | + */ |
|
108 | + public function add($prefix, $paths, $prepend = false) |
|
109 | + { |
|
110 | + if (!$prefix) { |
|
111 | + if ($prepend) { |
|
112 | + $this->fallbackDirsPsr0 = array_merge( |
|
113 | + (array) $paths, |
|
114 | + $this->fallbackDirsPsr0 |
|
115 | + ); |
|
116 | + } else { |
|
117 | + $this->fallbackDirsPsr0 = array_merge( |
|
118 | + $this->fallbackDirsPsr0, |
|
119 | + (array) $paths |
|
120 | + ); |
|
121 | + } |
|
122 | + |
|
123 | + return; |
|
124 | + } |
|
125 | + |
|
126 | + $first = $prefix[0]; |
|
127 | + if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
128 | + $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
|
129 | + |
|
130 | + return; |
|
131 | + } |
|
132 | + if ($prepend) { |
|
133 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
134 | + (array) $paths, |
|
135 | + $this->prefixesPsr0[$first][$prefix] |
|
136 | + ); |
|
137 | + } else { |
|
138 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
139 | + $this->prefixesPsr0[$first][$prefix], |
|
140 | + (array) $paths |
|
141 | + ); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Registers a set of PSR-4 directories for a given namespace, either |
|
147 | + * appending or prepending to the ones previously set for this namespace. |
|
148 | + * |
|
149 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
150 | + * @param array|string $paths The PSR-4 base directories |
|
151 | + * @param bool $prepend Whether to prepend the directories |
|
152 | + * |
|
153 | + * @throws \InvalidArgumentException |
|
154 | + */ |
|
155 | + public function addPsr4($prefix, $paths, $prepend = false) |
|
156 | + { |
|
157 | + if (!$prefix) { |
|
158 | + // Register directories for the root namespace. |
|
159 | + if ($prepend) { |
|
160 | + $this->fallbackDirsPsr4 = array_merge( |
|
161 | + (array) $paths, |
|
162 | + $this->fallbackDirsPsr4 |
|
163 | + ); |
|
164 | + } else { |
|
165 | + $this->fallbackDirsPsr4 = array_merge( |
|
166 | + $this->fallbackDirsPsr4, |
|
167 | + (array) $paths |
|
168 | + ); |
|
169 | + } |
|
170 | + } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
171 | + // Register directories for a new namespace. |
|
172 | + $length = strlen($prefix); |
|
173 | + if ('\\' !== $prefix[$length - 1]) { |
|
174 | + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
175 | + } |
|
176 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
177 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
178 | + } elseif ($prepend) { |
|
179 | + // Prepend directories for an already registered namespace. |
|
180 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
181 | + (array) $paths, |
|
182 | + $this->prefixDirsPsr4[$prefix] |
|
183 | + ); |
|
184 | + } else { |
|
185 | + // Append directories for an already registered namespace. |
|
186 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
187 | + $this->prefixDirsPsr4[$prefix], |
|
188 | + (array) $paths |
|
189 | + ); |
|
190 | + } |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Registers a set of PSR-0 directories for a given prefix, |
|
195 | + * replacing any others previously set for this prefix. |
|
196 | + * |
|
197 | + * @param string $prefix The prefix |
|
198 | + * @param array|string $paths The PSR-0 base directories |
|
199 | + */ |
|
200 | + public function set($prefix, $paths) |
|
201 | + { |
|
202 | + if (!$prefix) { |
|
203 | + $this->fallbackDirsPsr0 = (array) $paths; |
|
204 | + } else { |
|
205 | + $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Registers a set of PSR-4 directories for a given namespace, |
|
211 | + * replacing any others previously set for this namespace. |
|
212 | + * |
|
213 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
214 | + * @param array|string $paths The PSR-4 base directories |
|
215 | + * |
|
216 | + * @throws \InvalidArgumentException |
|
217 | + */ |
|
218 | + public function setPsr4($prefix, $paths) |
|
219 | + { |
|
220 | + if (!$prefix) { |
|
221 | + $this->fallbackDirsPsr4 = (array) $paths; |
|
222 | + } else { |
|
223 | + $length = strlen($prefix); |
|
224 | + if ('\\' !== $prefix[$length - 1]) { |
|
225 | + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
226 | + } |
|
227 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
228 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
229 | + } |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Turns on searching the include path for class files. |
|
234 | + * |
|
235 | + * @param bool $useIncludePath |
|
236 | + */ |
|
237 | + public function setUseIncludePath($useIncludePath) |
|
238 | + { |
|
239 | + $this->useIncludePath = $useIncludePath; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Can be used to check if the autoloader uses the include path to check |
|
244 | + * for classes. |
|
245 | + * |
|
246 | + * @return bool |
|
247 | + */ |
|
248 | + public function getUseIncludePath() |
|
249 | + { |
|
250 | + return $this->useIncludePath; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Turns off searching the prefix and fallback directories for classes |
|
255 | + * that have not been registered with the class map. |
|
256 | + * |
|
257 | + * @param bool $classMapAuthoritative |
|
258 | + */ |
|
259 | + public function setClassMapAuthoritative($classMapAuthoritative) |
|
260 | + { |
|
261 | + $this->classMapAuthoritative = $classMapAuthoritative; |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Should class lookup fail if not found in the current class map? |
|
266 | + * |
|
267 | + * @return bool |
|
268 | + */ |
|
269 | + public function isClassMapAuthoritative() |
|
270 | + { |
|
271 | + return $this->classMapAuthoritative; |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Registers this instance as an autoloader. |
|
276 | + * |
|
277 | + * @param bool $prepend Whether to prepend the autoloader or not |
|
278 | + */ |
|
279 | + public function register($prepend = false) |
|
280 | + { |
|
281 | + spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * Unregisters this instance as an autoloader. |
|
286 | + */ |
|
287 | + public function unregister() |
|
288 | + { |
|
289 | + spl_autoload_unregister(array($this, 'loadClass')); |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Loads the given class or interface. |
|
294 | + * |
|
295 | + * @param string $class The name of the class |
|
296 | + * @return bool|null True if loaded, null otherwise |
|
297 | + */ |
|
298 | + public function loadClass($class) |
|
299 | + { |
|
300 | + if ($file = $this->findFile($class)) { |
|
301 | + includeFile($file); |
|
302 | + |
|
303 | + return true; |
|
304 | + } |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Finds the path to the file where the class is defined. |
|
309 | + * |
|
310 | + * @param string $class The name of the class |
|
311 | + * |
|
312 | + * @return string|false The path if found, false otherwise |
|
313 | + */ |
|
314 | + public function findFile($class) |
|
315 | + { |
|
316 | + // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731 |
|
317 | + if ('\\' == $class[0]) { |
|
318 | + $class = substr($class, 1); |
|
319 | + } |
|
320 | + |
|
321 | + // class map lookup |
|
322 | + if (isset($this->classMap[$class])) { |
|
323 | + return $this->classMap[$class]; |
|
324 | + } |
|
325 | + if ($this->classMapAuthoritative) { |
|
326 | + return false; |
|
327 | + } |
|
328 | + |
|
329 | + $file = $this->findFileWithExtension($class, '.php'); |
|
330 | + |
|
331 | + // Search for Hack files if we are running on HHVM |
|
332 | + if ($file === null && defined('HHVM_VERSION')) { |
|
333 | + $file = $this->findFileWithExtension($class, '.hh'); |
|
334 | + } |
|
335 | + |
|
336 | + if ($file === null) { |
|
337 | + // Remember that this class does not exist. |
|
338 | + return $this->classMap[$class] = false; |
|
339 | + } |
|
340 | + |
|
341 | + return $file; |
|
342 | + } |
|
343 | + |
|
344 | + private function findFileWithExtension($class, $ext) |
|
345 | + { |
|
346 | + // PSR-4 lookup |
|
347 | + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
348 | + |
|
349 | + $first = $class[0]; |
|
350 | + if (isset($this->prefixLengthsPsr4[$first])) { |
|
351 | + foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) { |
|
352 | + if (0 === strpos($class, $prefix)) { |
|
353 | + foreach ($this->prefixDirsPsr4[$prefix] as $dir) { |
|
354 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) { |
|
355 | + return $file; |
|
356 | + } |
|
357 | + } |
|
358 | + } |
|
359 | + } |
|
360 | + } |
|
361 | + |
|
362 | + // PSR-4 fallback dirs |
|
363 | + foreach ($this->fallbackDirsPsr4 as $dir) { |
|
364 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
365 | + return $file; |
|
366 | + } |
|
367 | + } |
|
368 | + |
|
369 | + // PSR-0 lookup |
|
370 | + if (false !== $pos = strrpos($class, '\\')) { |
|
371 | + // namespaced class name |
|
372 | + $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
|
373 | + . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
|
374 | + } else { |
|
375 | + // PEAR-like class name |
|
376 | + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
377 | + } |
|
378 | + |
|
379 | + if (isset($this->prefixesPsr0[$first])) { |
|
380 | + foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
|
381 | + if (0 === strpos($class, $prefix)) { |
|
382 | + foreach ($dirs as $dir) { |
|
383 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
384 | + return $file; |
|
385 | + } |
|
386 | + } |
|
387 | + } |
|
388 | + } |
|
389 | + } |
|
390 | + |
|
391 | + // PSR-0 fallback dirs |
|
392 | + foreach ($this->fallbackDirsPsr0 as $dir) { |
|
393 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
394 | + return $file; |
|
395 | + } |
|
396 | + } |
|
397 | + |
|
398 | + // PSR-0 include paths. |
|
399 | + if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
|
400 | + return $file; |
|
401 | + } |
|
402 | + } |
|
403 | 403 | } |
404 | 404 | |
405 | 405 | /** |
@@ -409,5 +409,5 @@ discard block |
||
409 | 409 | */ |
410 | 410 | function includeFile($file) |
411 | 411 | { |
412 | - include $file; |
|
412 | + include $file; |
|
413 | 413 | } |
@@ -344,14 +344,14 @@ discard block |
||
344 | 344 | private function findFileWithExtension($class, $ext) |
345 | 345 | { |
346 | 346 | // PSR-4 lookup |
347 | - $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
347 | + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR).$ext; |
|
348 | 348 | |
349 | 349 | $first = $class[0]; |
350 | 350 | if (isset($this->prefixLengthsPsr4[$first])) { |
351 | 351 | foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) { |
352 | 352 | if (0 === strpos($class, $prefix)) { |
353 | 353 | foreach ($this->prefixDirsPsr4[$prefix] as $dir) { |
354 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) { |
|
354 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.substr($logicalPathPsr4, $length))) { |
|
355 | 355 | return $file; |
356 | 356 | } |
357 | 357 | } |
@@ -361,7 +361,7 @@ discard block |
||
361 | 361 | |
362 | 362 | // PSR-4 fallback dirs |
363 | 363 | foreach ($this->fallbackDirsPsr4 as $dir) { |
364 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
364 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr4)) { |
|
365 | 365 | return $file; |
366 | 366 | } |
367 | 367 | } |
@@ -373,14 +373,14 @@ discard block |
||
373 | 373 | . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
374 | 374 | } else { |
375 | 375 | // PEAR-like class name |
376 | - $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
376 | + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR).$ext; |
|
377 | 377 | } |
378 | 378 | |
379 | 379 | if (isset($this->prefixesPsr0[$first])) { |
380 | 380 | foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
381 | 381 | if (0 === strpos($class, $prefix)) { |
382 | 382 | foreach ($dirs as $dir) { |
383 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
383 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr0)) { |
|
384 | 384 | return $file; |
385 | 385 | } |
386 | 386 | } |
@@ -390,7 +390,7 @@ discard block |
||
390 | 390 | |
391 | 391 | // PSR-0 fallback dirs |
392 | 392 | foreach ($this->fallbackDirsPsr0 as $dir) { |
393 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
393 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr0)) { |
|
394 | 394 | return $file; |
395 | 395 | } |
396 | 396 | } |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | } |
201 | 201 | |
202 | 202 | /** |
203 | - * @param $method |
|
203 | + * @param string $method |
|
204 | 204 | * @param $arguments |
205 | 205 | * |
206 | 206 | * @throws \BadMethodCallException If the 'apply' rely on non existing method |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | } |
256 | 256 | |
257 | 257 | /** |
258 | - * @param $input |
|
258 | + * @param string $input |
|
259 | 259 | * |
260 | 260 | * @throws \ErrorException |
261 | 261 | * |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | /** |
313 | 313 | * @param string $text |
314 | 314 | * |
315 | - * @return mixed |
|
315 | + * @return string |
|
316 | 316 | */ |
317 | 317 | public function interpolate($text) |
318 | 318 | { |
@@ -379,6 +379,9 @@ discard block |
||
379 | 379 | return $statements; |
380 | 380 | } |
381 | 381 | |
382 | + /** |
|
383 | + * @param string $arg |
|
384 | + */ |
|
382 | 385 | protected function handleArgumentValue($arg) |
383 | 386 | { |
384 | 387 | if (preg_match('/^"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'/', $arg)) { |
@@ -11,443 +11,443 @@ |
||
11 | 11 | */ |
12 | 12 | class Compiler extends MixinVisitor |
13 | 13 | { |
14 | - /** |
|
15 | - * Constants and configuration in Compiler/CompilerConfig.php. |
|
16 | - */ |
|
17 | - |
|
18 | - /** |
|
19 | - * @var |
|
20 | - */ |
|
21 | - protected $xml; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var |
|
25 | - */ |
|
26 | - protected $parentIndents; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - protected $buffer = array(); |
|
32 | - /** |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - protected $options = array(); |
|
36 | - /** |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - protected $filters = array(); |
|
40 | - |
|
41 | - /** |
|
42 | - * @var bool |
|
43 | - */ |
|
44 | - protected $phpSingleLine = false; |
|
45 | - /** |
|
46 | - * @var bool |
|
47 | - */ |
|
48 | - protected $allowMixinOverride = false; |
|
49 | - /** |
|
50 | - * @var bool |
|
51 | - */ |
|
52 | - protected $keepNullAttributes = false; |
|
53 | - /** |
|
54 | - * @var bool |
|
55 | - */ |
|
56 | - protected $filterAutoLoad = true; |
|
57 | - /** |
|
58 | - * @var bool |
|
59 | - */ |
|
60 | - protected $terse = true; |
|
61 | - /** |
|
62 | - * @var bool |
|
63 | - */ |
|
64 | - protected $restrictedScope = false; |
|
65 | - /** |
|
66 | - * @var array |
|
67 | - */ |
|
68 | - protected $customKeywords = array(); |
|
69 | - /** |
|
70 | - * @var Jade |
|
71 | - */ |
|
72 | - protected $jade = null; |
|
73 | - |
|
74 | - /** |
|
75 | - * @var string |
|
76 | - */ |
|
77 | - protected $quote; |
|
78 | - |
|
79 | - /** |
|
80 | - * @var string |
|
81 | - */ |
|
82 | - protected $filename; |
|
83 | - |
|
84 | - /** |
|
85 | - * @param array/Jade $options |
|
86 | - * @param array $filters |
|
87 | - */ |
|
88 | - public function __construct($options = array(), array $filters = array(), $filename = null) |
|
89 | - { |
|
90 | - $this->options = $this->setOptions($options); |
|
91 | - $this->filters = $filters; |
|
92 | - $this->filename = $filename; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Get a jade engine reference or an options array and return needed options. |
|
97 | - * |
|
98 | - * @param array/Jade $options |
|
99 | - * |
|
100 | - * @return array |
|
101 | - */ |
|
102 | - protected function setOptions($options) |
|
103 | - { |
|
104 | - $optionTypes = array( |
|
105 | - 'prettyprint' => 'boolean', |
|
106 | - 'phpSingleLine' => 'boolean', |
|
107 | - 'allowMixinOverride' => 'boolean', |
|
108 | - 'keepNullAttributes' => 'boolean', |
|
109 | - 'filterAutoLoad' => 'boolean', |
|
110 | - 'restrictedScope' => 'boolean', |
|
111 | - 'indentSize' => 'integer', |
|
112 | - 'indentChar' => 'string', |
|
113 | - 'customKeywords' => 'array', |
|
114 | - ); |
|
115 | - |
|
116 | - if ($options instanceof Jade) { |
|
117 | - $this->jade = $options; |
|
118 | - $options = array(); |
|
119 | - |
|
120 | - foreach ($optionTypes as $option => $type) { |
|
121 | - $this->$option = $this->jade->getOption($option); |
|
122 | - $options[$option] = $this->$option; |
|
123 | - settype($this->$option, $type); |
|
124 | - } |
|
125 | - |
|
126 | - $this->quote = $this->jade->getOption('singleQuote') ? '\'' : '"'; |
|
127 | - |
|
128 | - return $options; |
|
129 | - } |
|
130 | - |
|
131 | - foreach (array_intersect_key($optionTypes, $options) as $option => $type) { |
|
132 | - $this->$option = $options[$option]; |
|
133 | - settype($this->$option, $type); |
|
134 | - } |
|
135 | - |
|
136 | - $this->quote = isset($options['singleQuote']) && $options['singleQuote'] ? '\'' : '"'; |
|
137 | - |
|
138 | - return $options; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Get an option from the jade engine if set or from the options array else. |
|
143 | - * |
|
144 | - * @param string $option |
|
145 | - * |
|
146 | - * @throws \InvalidArgumentException |
|
147 | - * |
|
148 | - * @return mixed |
|
149 | - */ |
|
150 | - public function getOption($option) |
|
151 | - { |
|
152 | - if (is_null($this->jade)) { |
|
153 | - if (!isset($this->options[$option])) { |
|
154 | - throw new \InvalidArgumentException("$option is not a valid option name.", 28); |
|
155 | - } |
|
156 | - |
|
157 | - return $this->options[$option]; |
|
158 | - } |
|
159 | - |
|
160 | - return $this->jade->getOption($option); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Get a compiler with the same settings. |
|
165 | - * |
|
166 | - * @return Compiler |
|
167 | - */ |
|
168 | - public function subCompiler() |
|
169 | - { |
|
170 | - return new static($this->options, $this->filters); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * php closing tag depanding on the pretty print setting. |
|
175 | - * |
|
176 | - * @return string |
|
177 | - */ |
|
178 | - protected function closingTag() |
|
179 | - { |
|
180 | - return '?>' . ($this->prettyprint ? ' ' : ''); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * @param $node |
|
185 | - * |
|
186 | - * @return string |
|
187 | - */ |
|
188 | - public function compile($node) |
|
189 | - { |
|
190 | - $this->visit($node); |
|
191 | - |
|
192 | - $code = ltrim(implode('', $this->buffer)); |
|
193 | - |
|
194 | - // Separate in several lines to get a useable line number in case of an error occurs |
|
195 | - if ($this->phpSingleLine) { |
|
196 | - $code = str_replace(array('<?php', '?>'), array("<?php\n", "\n" . $this->closingTag()), $code); |
|
197 | - } |
|
198 | - // Remove the $ wich are not needed |
|
199 | - return $code; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * @param $method |
|
204 | - * @param $arguments |
|
205 | - * |
|
206 | - * @throws \BadMethodCallException If the 'apply' rely on non existing method |
|
207 | - * |
|
208 | - * @return mixed |
|
209 | - */ |
|
210 | - protected function apply($method, $arguments) |
|
211 | - { |
|
212 | - if (!method_exists($this, $method)) { |
|
213 | - throw new \BadMethodCallException(sprintf('Method %s do not exists', $method), 7); |
|
214 | - } |
|
215 | - |
|
216 | - return call_user_func_array(array($this, $method), $arguments); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * @param $line |
|
221 | - * @param null $indent |
|
222 | - */ |
|
223 | - protected function buffer($line, $indent = null) |
|
224 | - { |
|
225 | - if ($indent === true || ($indent === null && $this->prettyprint)) { |
|
226 | - $line = $this->indent() . $line . $this->newline(); |
|
227 | - } |
|
228 | - |
|
229 | - $this->buffer[] = $line; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * @param string $str |
|
234 | - * |
|
235 | - * @return bool|int |
|
236 | - */ |
|
237 | - protected function isConstant($str) |
|
238 | - { |
|
239 | - return preg_match('/^' . static::CONSTANT_VALUE . '$/', trim($str)); |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * @param $input |
|
244 | - * @param string $name |
|
245 | - * |
|
246 | - * @throws \ErrorException |
|
247 | - * |
|
248 | - * @return array |
|
249 | - */ |
|
250 | - public function handleCode($input, $name = '') |
|
251 | - { |
|
252 | - $handler = new CodeHandler($input, $name); |
|
253 | - |
|
254 | - return $handler->parse(); |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * @param $input |
|
259 | - * |
|
260 | - * @throws \ErrorException |
|
261 | - * |
|
262 | - * @return array |
|
263 | - */ |
|
264 | - public function handleString($input) |
|
265 | - { |
|
266 | - $result = array(); |
|
267 | - $resultsString = array(); |
|
268 | - |
|
269 | - $separators = preg_split( |
|
270 | - '/[+](?!\\()/', // concatenation operator - only js |
|
271 | - $input, |
|
272 | - -1, |
|
273 | - PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_DELIM_CAPTURE |
|
274 | - ); |
|
275 | - |
|
276 | - foreach ($separators as $part) { |
|
277 | - // $sep[0] - the separator string due to PREG_SPLIT_OFFSET_CAPTURE flag |
|
278 | - // $sep[1] - the offset due to PREG_SPLIT_OFFSET_CAPTURE |
|
279 | - // @todo: = find original usage of this |
|
280 | - //$sep = substr( |
|
281 | - // $input, |
|
282 | - // strlen($part[0]) + $part[1] + 1, |
|
283 | - // isset($separators[$i+1]) ? $separators[$i+1][1] : strlen($input) |
|
284 | - //); |
|
285 | - |
|
286 | - // @todo: handleCode() in concat |
|
287 | - $part[0] = trim($part[0]); |
|
288 | - |
|
289 | - if (preg_match('/^("(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\')(.*)$/', $part[0], $match)) { |
|
290 | - $quote = substr($match[1], 0, 1); |
|
291 | - |
|
292 | - if (strlen(trim($match[2]))) { |
|
293 | - throw new \ErrorException('Unexpected value: ' . $match[2], 8); |
|
294 | - } |
|
295 | - |
|
296 | - array_push($resultsString, $match[1]); |
|
297 | - |
|
298 | - continue; |
|
299 | - } |
|
300 | - |
|
301 | - $code = $this->handleCode($part[0]); |
|
302 | - |
|
303 | - $result = array_merge($result, array_slice($code, 0, -1)); |
|
304 | - array_push($resultsString, array_pop($code)); |
|
305 | - } |
|
306 | - |
|
307 | - array_push($result, implode(' . ', $resultsString)); |
|
308 | - |
|
309 | - return $result; |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * @param string $text |
|
314 | - * |
|
315 | - * @return mixed |
|
316 | - */ |
|
317 | - public function interpolate($text) |
|
318 | - { |
|
319 | - return preg_replace_callback('/(\\\\)?([#!]){(.*?)}/', array($this, 'interpolateFromCapture'), $text); |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * @param array $match |
|
324 | - * |
|
325 | - * @return string |
|
326 | - */ |
|
327 | - protected function interpolateFromCapture($match) |
|
328 | - { |
|
329 | - if ($match[1] === '') { |
|
330 | - return $this->escapeIfNeeded($match[2] === '!', $match[3]); |
|
331 | - } |
|
332 | - |
|
333 | - return substr($match[0], 1); |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * @throws \InvalidArgumentException |
|
338 | - * |
|
339 | - * @return array |
|
340 | - */ |
|
341 | - protected function createStatements() |
|
342 | - { |
|
343 | - if (func_num_args() === 0) { |
|
344 | - throw new \InvalidArgumentException('No Arguments provided', 9); |
|
345 | - } |
|
346 | - |
|
347 | - $arguments = func_get_args(); |
|
348 | - $statements = array(); |
|
349 | - $variables = array(); |
|
350 | - |
|
351 | - foreach ($arguments as $arg) { |
|
352 | - $arg = static::convertVarPath($arg); |
|
353 | - |
|
354 | - // add dollar if missing |
|
355 | - if (preg_match('/^' . static::VARNAME . '(\s*,.+)?$/', $arg)) { |
|
356 | - $arg = static::addDollarIfNeeded($arg); |
|
357 | - } |
|
358 | - |
|
359 | - // shortcut for constants |
|
360 | - if ($this->isConstant($arg)) { |
|
361 | - array_push($variables, $arg); |
|
362 | - continue; |
|
363 | - } |
|
364 | - |
|
365 | - // if we have a php variable assume that the string is good php |
|
366 | - if (strpos('{[', substr($arg, 0, 1)) === false && preg_match('/&?\${1,2}' . static::VARNAME . '|[A-Za-z0-9_\\\\]+::/', $arg)) { |
|
367 | - array_push($variables, $arg); |
|
368 | - continue; |
|
369 | - } |
|
370 | - |
|
371 | - $code = $this->handleArgumentValue($arg); |
|
372 | - |
|
373 | - $statements = array_merge($statements, array_slice($code, 0, -1)); |
|
374 | - array_push($variables, array_pop($code)); |
|
375 | - } |
|
376 | - |
|
377 | - array_push($statements, $variables); |
|
378 | - |
|
379 | - return $statements; |
|
380 | - } |
|
381 | - |
|
382 | - protected function handleArgumentValue($arg) |
|
383 | - { |
|
384 | - if (preg_match('/^"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'/', $arg)) { |
|
385 | - return $this->handleString(trim($arg)); |
|
386 | - } |
|
387 | - |
|
388 | - try { |
|
389 | - return $this->handleCode($arg); |
|
390 | - } catch (\Exception $e) { |
|
391 | - // if a bug occur, try to remove comments |
|
392 | - try { |
|
393 | - return $this->handleCode(preg_replace('#/\*(.*)\*/#', '', $arg)); |
|
394 | - } catch (\Exception $e) { |
|
395 | - throw new ParserException('Pug.php did not understand ' . $arg, 10, $e); |
|
396 | - } |
|
397 | - } |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * @param $code |
|
402 | - * @param null $statements |
|
403 | - * |
|
404 | - * @return string |
|
405 | - */ |
|
406 | - protected function createPhpBlock($code, $statements = null) |
|
407 | - { |
|
408 | - if ($statements === null) { |
|
409 | - return '<?php ' . $code . ' ' . $this->closingTag(); |
|
410 | - } |
|
411 | - |
|
412 | - $codeFormat = array_pop($statements); |
|
413 | - array_unshift($codeFormat, $code); |
|
414 | - |
|
415 | - if (count($statements) === 0) { |
|
416 | - $phpString = call_user_func_array('sprintf', $codeFormat); |
|
417 | - |
|
418 | - return '<?php ' . $phpString . ' ' . $this->closingTag(); |
|
419 | - } |
|
420 | - |
|
421 | - $stmtString = ''; |
|
422 | - foreach ($statements as $stmt) { |
|
423 | - $stmtString .= $this->newline() . $this->indent() . $stmt . ';'; |
|
424 | - } |
|
425 | - |
|
426 | - $stmtString .= $this->newline() . $this->indent(); |
|
427 | - $stmtString .= call_user_func_array('sprintf', $codeFormat); |
|
428 | - |
|
429 | - $phpString = '<?php '; |
|
430 | - $phpString .= $stmtString; |
|
431 | - $phpString .= $this->newline() . $this->indent() . ' ' . $this->closingTag(); |
|
432 | - |
|
433 | - return $phpString; |
|
434 | - } |
|
435 | - |
|
436 | - /** |
|
437 | - * @param $code |
|
438 | - * |
|
439 | - * @return string |
|
440 | - */ |
|
441 | - protected function createCode($code) |
|
442 | - { |
|
443 | - if (func_num_args() > 1) { |
|
444 | - $arguments = func_get_args(); |
|
445 | - array_shift($arguments); // remove $code |
|
446 | - $statements = $this->apply('createStatements', $arguments); |
|
447 | - |
|
448 | - return $this->createPhpBlock($code, $statements); |
|
449 | - } |
|
450 | - |
|
451 | - return $this->createPhpBlock($code); |
|
452 | - } |
|
14 | + /** |
|
15 | + * Constants and configuration in Compiler/CompilerConfig.php. |
|
16 | + */ |
|
17 | + |
|
18 | + /** |
|
19 | + * @var |
|
20 | + */ |
|
21 | + protected $xml; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var |
|
25 | + */ |
|
26 | + protected $parentIndents; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + protected $buffer = array(); |
|
32 | + /** |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + protected $options = array(); |
|
36 | + /** |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + protected $filters = array(); |
|
40 | + |
|
41 | + /** |
|
42 | + * @var bool |
|
43 | + */ |
|
44 | + protected $phpSingleLine = false; |
|
45 | + /** |
|
46 | + * @var bool |
|
47 | + */ |
|
48 | + protected $allowMixinOverride = false; |
|
49 | + /** |
|
50 | + * @var bool |
|
51 | + */ |
|
52 | + protected $keepNullAttributes = false; |
|
53 | + /** |
|
54 | + * @var bool |
|
55 | + */ |
|
56 | + protected $filterAutoLoad = true; |
|
57 | + /** |
|
58 | + * @var bool |
|
59 | + */ |
|
60 | + protected $terse = true; |
|
61 | + /** |
|
62 | + * @var bool |
|
63 | + */ |
|
64 | + protected $restrictedScope = false; |
|
65 | + /** |
|
66 | + * @var array |
|
67 | + */ |
|
68 | + protected $customKeywords = array(); |
|
69 | + /** |
|
70 | + * @var Jade |
|
71 | + */ |
|
72 | + protected $jade = null; |
|
73 | + |
|
74 | + /** |
|
75 | + * @var string |
|
76 | + */ |
|
77 | + protected $quote; |
|
78 | + |
|
79 | + /** |
|
80 | + * @var string |
|
81 | + */ |
|
82 | + protected $filename; |
|
83 | + |
|
84 | + /** |
|
85 | + * @param array/Jade $options |
|
86 | + * @param array $filters |
|
87 | + */ |
|
88 | + public function __construct($options = array(), array $filters = array(), $filename = null) |
|
89 | + { |
|
90 | + $this->options = $this->setOptions($options); |
|
91 | + $this->filters = $filters; |
|
92 | + $this->filename = $filename; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Get a jade engine reference or an options array and return needed options. |
|
97 | + * |
|
98 | + * @param array/Jade $options |
|
99 | + * |
|
100 | + * @return array |
|
101 | + */ |
|
102 | + protected function setOptions($options) |
|
103 | + { |
|
104 | + $optionTypes = array( |
|
105 | + 'prettyprint' => 'boolean', |
|
106 | + 'phpSingleLine' => 'boolean', |
|
107 | + 'allowMixinOverride' => 'boolean', |
|
108 | + 'keepNullAttributes' => 'boolean', |
|
109 | + 'filterAutoLoad' => 'boolean', |
|
110 | + 'restrictedScope' => 'boolean', |
|
111 | + 'indentSize' => 'integer', |
|
112 | + 'indentChar' => 'string', |
|
113 | + 'customKeywords' => 'array', |
|
114 | + ); |
|
115 | + |
|
116 | + if ($options instanceof Jade) { |
|
117 | + $this->jade = $options; |
|
118 | + $options = array(); |
|
119 | + |
|
120 | + foreach ($optionTypes as $option => $type) { |
|
121 | + $this->$option = $this->jade->getOption($option); |
|
122 | + $options[$option] = $this->$option; |
|
123 | + settype($this->$option, $type); |
|
124 | + } |
|
125 | + |
|
126 | + $this->quote = $this->jade->getOption('singleQuote') ? '\'' : '"'; |
|
127 | + |
|
128 | + return $options; |
|
129 | + } |
|
130 | + |
|
131 | + foreach (array_intersect_key($optionTypes, $options) as $option => $type) { |
|
132 | + $this->$option = $options[$option]; |
|
133 | + settype($this->$option, $type); |
|
134 | + } |
|
135 | + |
|
136 | + $this->quote = isset($options['singleQuote']) && $options['singleQuote'] ? '\'' : '"'; |
|
137 | + |
|
138 | + return $options; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Get an option from the jade engine if set or from the options array else. |
|
143 | + * |
|
144 | + * @param string $option |
|
145 | + * |
|
146 | + * @throws \InvalidArgumentException |
|
147 | + * |
|
148 | + * @return mixed |
|
149 | + */ |
|
150 | + public function getOption($option) |
|
151 | + { |
|
152 | + if (is_null($this->jade)) { |
|
153 | + if (!isset($this->options[$option])) { |
|
154 | + throw new \InvalidArgumentException("$option is not a valid option name.", 28); |
|
155 | + } |
|
156 | + |
|
157 | + return $this->options[$option]; |
|
158 | + } |
|
159 | + |
|
160 | + return $this->jade->getOption($option); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Get a compiler with the same settings. |
|
165 | + * |
|
166 | + * @return Compiler |
|
167 | + */ |
|
168 | + public function subCompiler() |
|
169 | + { |
|
170 | + return new static($this->options, $this->filters); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * php closing tag depanding on the pretty print setting. |
|
175 | + * |
|
176 | + * @return string |
|
177 | + */ |
|
178 | + protected function closingTag() |
|
179 | + { |
|
180 | + return '?>' . ($this->prettyprint ? ' ' : ''); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * @param $node |
|
185 | + * |
|
186 | + * @return string |
|
187 | + */ |
|
188 | + public function compile($node) |
|
189 | + { |
|
190 | + $this->visit($node); |
|
191 | + |
|
192 | + $code = ltrim(implode('', $this->buffer)); |
|
193 | + |
|
194 | + // Separate in several lines to get a useable line number in case of an error occurs |
|
195 | + if ($this->phpSingleLine) { |
|
196 | + $code = str_replace(array('<?php', '?>'), array("<?php\n", "\n" . $this->closingTag()), $code); |
|
197 | + } |
|
198 | + // Remove the $ wich are not needed |
|
199 | + return $code; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * @param $method |
|
204 | + * @param $arguments |
|
205 | + * |
|
206 | + * @throws \BadMethodCallException If the 'apply' rely on non existing method |
|
207 | + * |
|
208 | + * @return mixed |
|
209 | + */ |
|
210 | + protected function apply($method, $arguments) |
|
211 | + { |
|
212 | + if (!method_exists($this, $method)) { |
|
213 | + throw new \BadMethodCallException(sprintf('Method %s do not exists', $method), 7); |
|
214 | + } |
|
215 | + |
|
216 | + return call_user_func_array(array($this, $method), $arguments); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * @param $line |
|
221 | + * @param null $indent |
|
222 | + */ |
|
223 | + protected function buffer($line, $indent = null) |
|
224 | + { |
|
225 | + if ($indent === true || ($indent === null && $this->prettyprint)) { |
|
226 | + $line = $this->indent() . $line . $this->newline(); |
|
227 | + } |
|
228 | + |
|
229 | + $this->buffer[] = $line; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * @param string $str |
|
234 | + * |
|
235 | + * @return bool|int |
|
236 | + */ |
|
237 | + protected function isConstant($str) |
|
238 | + { |
|
239 | + return preg_match('/^' . static::CONSTANT_VALUE . '$/', trim($str)); |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * @param $input |
|
244 | + * @param string $name |
|
245 | + * |
|
246 | + * @throws \ErrorException |
|
247 | + * |
|
248 | + * @return array |
|
249 | + */ |
|
250 | + public function handleCode($input, $name = '') |
|
251 | + { |
|
252 | + $handler = new CodeHandler($input, $name); |
|
253 | + |
|
254 | + return $handler->parse(); |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * @param $input |
|
259 | + * |
|
260 | + * @throws \ErrorException |
|
261 | + * |
|
262 | + * @return array |
|
263 | + */ |
|
264 | + public function handleString($input) |
|
265 | + { |
|
266 | + $result = array(); |
|
267 | + $resultsString = array(); |
|
268 | + |
|
269 | + $separators = preg_split( |
|
270 | + '/[+](?!\\()/', // concatenation operator - only js |
|
271 | + $input, |
|
272 | + -1, |
|
273 | + PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_DELIM_CAPTURE |
|
274 | + ); |
|
275 | + |
|
276 | + foreach ($separators as $part) { |
|
277 | + // $sep[0] - the separator string due to PREG_SPLIT_OFFSET_CAPTURE flag |
|
278 | + // $sep[1] - the offset due to PREG_SPLIT_OFFSET_CAPTURE |
|
279 | + // @todo: = find original usage of this |
|
280 | + //$sep = substr( |
|
281 | + // $input, |
|
282 | + // strlen($part[0]) + $part[1] + 1, |
|
283 | + // isset($separators[$i+1]) ? $separators[$i+1][1] : strlen($input) |
|
284 | + //); |
|
285 | + |
|
286 | + // @todo: handleCode() in concat |
|
287 | + $part[0] = trim($part[0]); |
|
288 | + |
|
289 | + if (preg_match('/^("(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\')(.*)$/', $part[0], $match)) { |
|
290 | + $quote = substr($match[1], 0, 1); |
|
291 | + |
|
292 | + if (strlen(trim($match[2]))) { |
|
293 | + throw new \ErrorException('Unexpected value: ' . $match[2], 8); |
|
294 | + } |
|
295 | + |
|
296 | + array_push($resultsString, $match[1]); |
|
297 | + |
|
298 | + continue; |
|
299 | + } |
|
300 | + |
|
301 | + $code = $this->handleCode($part[0]); |
|
302 | + |
|
303 | + $result = array_merge($result, array_slice($code, 0, -1)); |
|
304 | + array_push($resultsString, array_pop($code)); |
|
305 | + } |
|
306 | + |
|
307 | + array_push($result, implode(' . ', $resultsString)); |
|
308 | + |
|
309 | + return $result; |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * @param string $text |
|
314 | + * |
|
315 | + * @return mixed |
|
316 | + */ |
|
317 | + public function interpolate($text) |
|
318 | + { |
|
319 | + return preg_replace_callback('/(\\\\)?([#!]){(.*?)}/', array($this, 'interpolateFromCapture'), $text); |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * @param array $match |
|
324 | + * |
|
325 | + * @return string |
|
326 | + */ |
|
327 | + protected function interpolateFromCapture($match) |
|
328 | + { |
|
329 | + if ($match[1] === '') { |
|
330 | + return $this->escapeIfNeeded($match[2] === '!', $match[3]); |
|
331 | + } |
|
332 | + |
|
333 | + return substr($match[0], 1); |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * @throws \InvalidArgumentException |
|
338 | + * |
|
339 | + * @return array |
|
340 | + */ |
|
341 | + protected function createStatements() |
|
342 | + { |
|
343 | + if (func_num_args() === 0) { |
|
344 | + throw new \InvalidArgumentException('No Arguments provided', 9); |
|
345 | + } |
|
346 | + |
|
347 | + $arguments = func_get_args(); |
|
348 | + $statements = array(); |
|
349 | + $variables = array(); |
|
350 | + |
|
351 | + foreach ($arguments as $arg) { |
|
352 | + $arg = static::convertVarPath($arg); |
|
353 | + |
|
354 | + // add dollar if missing |
|
355 | + if (preg_match('/^' . static::VARNAME . '(\s*,.+)?$/', $arg)) { |
|
356 | + $arg = static::addDollarIfNeeded($arg); |
|
357 | + } |
|
358 | + |
|
359 | + // shortcut for constants |
|
360 | + if ($this->isConstant($arg)) { |
|
361 | + array_push($variables, $arg); |
|
362 | + continue; |
|
363 | + } |
|
364 | + |
|
365 | + // if we have a php variable assume that the string is good php |
|
366 | + if (strpos('{[', substr($arg, 0, 1)) === false && preg_match('/&?\${1,2}' . static::VARNAME . '|[A-Za-z0-9_\\\\]+::/', $arg)) { |
|
367 | + array_push($variables, $arg); |
|
368 | + continue; |
|
369 | + } |
|
370 | + |
|
371 | + $code = $this->handleArgumentValue($arg); |
|
372 | + |
|
373 | + $statements = array_merge($statements, array_slice($code, 0, -1)); |
|
374 | + array_push($variables, array_pop($code)); |
|
375 | + } |
|
376 | + |
|
377 | + array_push($statements, $variables); |
|
378 | + |
|
379 | + return $statements; |
|
380 | + } |
|
381 | + |
|
382 | + protected function handleArgumentValue($arg) |
|
383 | + { |
|
384 | + if (preg_match('/^"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'/', $arg)) { |
|
385 | + return $this->handleString(trim($arg)); |
|
386 | + } |
|
387 | + |
|
388 | + try { |
|
389 | + return $this->handleCode($arg); |
|
390 | + } catch (\Exception $e) { |
|
391 | + // if a bug occur, try to remove comments |
|
392 | + try { |
|
393 | + return $this->handleCode(preg_replace('#/\*(.*)\*/#', '', $arg)); |
|
394 | + } catch (\Exception $e) { |
|
395 | + throw new ParserException('Pug.php did not understand ' . $arg, 10, $e); |
|
396 | + } |
|
397 | + } |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * @param $code |
|
402 | + * @param null $statements |
|
403 | + * |
|
404 | + * @return string |
|
405 | + */ |
|
406 | + protected function createPhpBlock($code, $statements = null) |
|
407 | + { |
|
408 | + if ($statements === null) { |
|
409 | + return '<?php ' . $code . ' ' . $this->closingTag(); |
|
410 | + } |
|
411 | + |
|
412 | + $codeFormat = array_pop($statements); |
|
413 | + array_unshift($codeFormat, $code); |
|
414 | + |
|
415 | + if (count($statements) === 0) { |
|
416 | + $phpString = call_user_func_array('sprintf', $codeFormat); |
|
417 | + |
|
418 | + return '<?php ' . $phpString . ' ' . $this->closingTag(); |
|
419 | + } |
|
420 | + |
|
421 | + $stmtString = ''; |
|
422 | + foreach ($statements as $stmt) { |
|
423 | + $stmtString .= $this->newline() . $this->indent() . $stmt . ';'; |
|
424 | + } |
|
425 | + |
|
426 | + $stmtString .= $this->newline() . $this->indent(); |
|
427 | + $stmtString .= call_user_func_array('sprintf', $codeFormat); |
|
428 | + |
|
429 | + $phpString = '<?php '; |
|
430 | + $phpString .= $stmtString; |
|
431 | + $phpString .= $this->newline() . $this->indent() . ' ' . $this->closingTag(); |
|
432 | + |
|
433 | + return $phpString; |
|
434 | + } |
|
435 | + |
|
436 | + /** |
|
437 | + * @param $code |
|
438 | + * |
|
439 | + * @return string |
|
440 | + */ |
|
441 | + protected function createCode($code) |
|
442 | + { |
|
443 | + if (func_num_args() > 1) { |
|
444 | + $arguments = func_get_args(); |
|
445 | + array_shift($arguments); // remove $code |
|
446 | + $statements = $this->apply('createStatements', $arguments); |
|
447 | + |
|
448 | + return $this->createPhpBlock($code, $statements); |
|
449 | + } |
|
450 | + |
|
451 | + return $this->createPhpBlock($code); |
|
452 | + } |
|
453 | 453 | } |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | */ |
178 | 178 | protected function closingTag() |
179 | 179 | { |
180 | - return '?>' . ($this->prettyprint ? ' ' : ''); |
|
180 | + return '?>'.($this->prettyprint ? ' ' : ''); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | /** |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | |
194 | 194 | // Separate in several lines to get a useable line number in case of an error occurs |
195 | 195 | if ($this->phpSingleLine) { |
196 | - $code = str_replace(array('<?php', '?>'), array("<?php\n", "\n" . $this->closingTag()), $code); |
|
196 | + $code = str_replace(array('<?php', '?>'), array("<?php\n", "\n".$this->closingTag()), $code); |
|
197 | 197 | } |
198 | 198 | // Remove the $ wich are not needed |
199 | 199 | return $code; |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | protected function buffer($line, $indent = null) |
224 | 224 | { |
225 | 225 | if ($indent === true || ($indent === null && $this->prettyprint)) { |
226 | - $line = $this->indent() . $line . $this->newline(); |
|
226 | + $line = $this->indent().$line.$this->newline(); |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | $this->buffer[] = $line; |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | */ |
237 | 237 | protected function isConstant($str) |
238 | 238 | { |
239 | - return preg_match('/^' . static::CONSTANT_VALUE . '$/', trim($str)); |
|
239 | + return preg_match('/^'.static::CONSTANT_VALUE.'$/', trim($str)); |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | /** |
@@ -290,7 +290,7 @@ discard block |
||
290 | 290 | $quote = substr($match[1], 0, 1); |
291 | 291 | |
292 | 292 | if (strlen(trim($match[2]))) { |
293 | - throw new \ErrorException('Unexpected value: ' . $match[2], 8); |
|
293 | + throw new \ErrorException('Unexpected value: '.$match[2], 8); |
|
294 | 294 | } |
295 | 295 | |
296 | 296 | array_push($resultsString, $match[1]); |
@@ -352,7 +352,7 @@ discard block |
||
352 | 352 | $arg = static::convertVarPath($arg); |
353 | 353 | |
354 | 354 | // add dollar if missing |
355 | - if (preg_match('/^' . static::VARNAME . '(\s*,.+)?$/', $arg)) { |
|
355 | + if (preg_match('/^'.static::VARNAME.'(\s*,.+)?$/', $arg)) { |
|
356 | 356 | $arg = static::addDollarIfNeeded($arg); |
357 | 357 | } |
358 | 358 | |
@@ -363,7 +363,7 @@ discard block |
||
363 | 363 | } |
364 | 364 | |
365 | 365 | // if we have a php variable assume that the string is good php |
366 | - if (strpos('{[', substr($arg, 0, 1)) === false && preg_match('/&?\${1,2}' . static::VARNAME . '|[A-Za-z0-9_\\\\]+::/', $arg)) { |
|
366 | + if (strpos('{[', substr($arg, 0, 1)) === false && preg_match('/&?\${1,2}'.static::VARNAME.'|[A-Za-z0-9_\\\\]+::/', $arg)) { |
|
367 | 367 | array_push($variables, $arg); |
368 | 368 | continue; |
369 | 369 | } |
@@ -392,7 +392,7 @@ discard block |
||
392 | 392 | try { |
393 | 393 | return $this->handleCode(preg_replace('#/\*(.*)\*/#', '', $arg)); |
394 | 394 | } catch (\Exception $e) { |
395 | - throw new ParserException('Pug.php did not understand ' . $arg, 10, $e); |
|
395 | + throw new ParserException('Pug.php did not understand '.$arg, 10, $e); |
|
396 | 396 | } |
397 | 397 | } |
398 | 398 | } |
@@ -406,7 +406,7 @@ discard block |
||
406 | 406 | protected function createPhpBlock($code, $statements = null) |
407 | 407 | { |
408 | 408 | if ($statements === null) { |
409 | - return '<?php ' . $code . ' ' . $this->closingTag(); |
|
409 | + return '<?php '.$code.' '.$this->closingTag(); |
|
410 | 410 | } |
411 | 411 | |
412 | 412 | $codeFormat = array_pop($statements); |
@@ -415,20 +415,20 @@ discard block |
||
415 | 415 | if (count($statements) === 0) { |
416 | 416 | $phpString = call_user_func_array('sprintf', $codeFormat); |
417 | 417 | |
418 | - return '<?php ' . $phpString . ' ' . $this->closingTag(); |
|
418 | + return '<?php '.$phpString.' '.$this->closingTag(); |
|
419 | 419 | } |
420 | 420 | |
421 | 421 | $stmtString = ''; |
422 | 422 | foreach ($statements as $stmt) { |
423 | - $stmtString .= $this->newline() . $this->indent() . $stmt . ';'; |
|
423 | + $stmtString .= $this->newline().$this->indent().$stmt.';'; |
|
424 | 424 | } |
425 | 425 | |
426 | - $stmtString .= $this->newline() . $this->indent(); |
|
426 | + $stmtString .= $this->newline().$this->indent(); |
|
427 | 427 | $stmtString .= call_user_func_array('sprintf', $codeFormat); |
428 | 428 | |
429 | 429 | $phpString = '<?php '; |
430 | 430 | $phpString .= $stmtString; |
431 | - $phpString .= $this->newline() . $this->indent() . ' ' . $this->closingTag(); |
|
431 | + $phpString .= $this->newline().$this->indent().' '.$this->closingTag(); |
|
432 | 432 | |
433 | 433 | return $phpString; |
434 | 434 | } |
@@ -87,6 +87,10 @@ discard block |
||
87 | 87 | return $this->escapeIfNeeded($escaped, '$__value'); |
88 | 88 | } |
89 | 89 | |
90 | + /** |
|
91 | + * @param string $key |
|
92 | + * @param string $value |
|
93 | + */ |
|
90 | 94 | protected function getAttributeValue($escaped, $key, $value, &$classesCheck, &$valueCheck) |
91 | 95 | { |
92 | 96 | if ($this->isConstant($value)) { |
@@ -113,6 +117,10 @@ discard block |
||
113 | 117 | : $value; |
114 | 118 | } |
115 | 119 | |
120 | + /** |
|
121 | + * @param string $key |
|
122 | + * @param callable $value |
|
123 | + */ |
|
116 | 124 | protected function compileAttributeValue($key, $value, $attr, $valueCheck) |
117 | 125 | { |
118 | 126 | return $value === true || $attr['value'] === true |
@@ -4,193 +4,193 @@ |
||
4 | 4 | |
5 | 5 | abstract class AttributesCompiler extends CompilerFacade |
6 | 6 | { |
7 | - protected function getAttributeDisplayCode($key, $value, $valueCheck) |
|
8 | - { |
|
9 | - if ($key === 'style') { |
|
10 | - $value = preg_replace('/::get(Escaped|Unescaped)Value/', '::get$1Style', $value, 1); |
|
11 | - } |
|
12 | - |
|
13 | - return is_null($valueCheck) |
|
14 | - ? ' ' . $key . '=' . $this->quote . $value . $this->quote |
|
15 | - : $this->createCode('if (true === ($__value = %1$s)) { ', $valueCheck) |
|
16 | - . $this->getBooleanAttributeDisplayCode($key) |
|
17 | - . $this->createCode('} else if (\\Jade\\Compiler::isDisplayable($__value)) { ') |
|
18 | - . ' ' . $key . '=' . $this->quote . $value . $this->quote |
|
19 | - . $this->createCode('}'); |
|
20 | - } |
|
21 | - |
|
22 | - protected function getBooleanAttributeDisplayCode($key) |
|
23 | - { |
|
24 | - return ' ' . $key . ($this->terse |
|
25 | - ? '' |
|
26 | - : '=' . $this->quote . $key . $this->quote |
|
27 | - ); |
|
28 | - } |
|
29 | - |
|
30 | - protected function getValueStatement($statements) |
|
31 | - { |
|
32 | - return is_string($statements[0]) |
|
33 | - ? $statements[0] |
|
34 | - : $statements[0][0]; |
|
35 | - } |
|
36 | - |
|
37 | - protected function getAndAttributeCode($attr, &$classes, &$classesCheck) |
|
38 | - { |
|
39 | - $addClasses = '""'; |
|
40 | - if (count($classes) || count($classesCheck)) { |
|
41 | - foreach ($classes as &$value) { |
|
42 | - $value = var_export($value, true); |
|
43 | - } |
|
44 | - foreach ($classesCheck as $value) { |
|
45 | - $statements = $this->createStatements($value); |
|
46 | - $classes[] = $statements[0][0]; |
|
47 | - } |
|
48 | - $addClasses = '" " . implode(" ", array(' . implode(', ', $classes) . '))'; |
|
49 | - $classes = array(); |
|
50 | - $classesCheck = array(); |
|
51 | - } |
|
52 | - $value = empty($attr['value']) ? 'attributes' : $attr['value']; |
|
53 | - $statements = $this->createStatements($value); |
|
54 | - |
|
55 | - return $this->createCode( |
|
56 | - '$__attributes = ' . $this->getValueStatement($statements) . ';' . |
|
57 | - 'if (is_array($__attributes)) { ' . |
|
58 | - '$__attributes["class"] = trim(' . |
|
59 | - '$__classes = (empty($__classes) ? "" : $__classes . " ") . ' . |
|
60 | - '(isset($__attributes["class"]) ? (is_array($__attributes["class"]) ? implode(" ", $__attributes["class"]) : $__attributes["class"]) : "") . ' . |
|
61 | - $addClasses . |
|
62 | - '); ' . |
|
63 | - 'if (empty($__attributes["class"])) { ' . |
|
64 | - 'unset($__attributes["class"]); ' . |
|
65 | - '} ' . |
|
66 | - '} ' . |
|
67 | - '\\Jade\\Compiler::displayAttributes($__attributes, ' . var_export($this->quote, true) . ', ' . var_export($this->terse, true) . ');'); |
|
68 | - } |
|
69 | - |
|
70 | - protected function getClassAttribute($value, &$classesCheck) |
|
71 | - { |
|
72 | - $statements = $this->createStatements($value); |
|
73 | - $value = is_array($statements[0]) ? $statements[0][0] : $statements[0]; |
|
74 | - $classesCheck[] = '(is_array($_a = ' . $value . ') ? implode(" ", $_a) : $_a)'; |
|
75 | - |
|
76 | - return $this->keepNullAttributes ? '' : 'null'; |
|
77 | - } |
|
78 | - |
|
79 | - protected function getValueCode($escaped, $value, &$valueCheck) |
|
80 | - { |
|
81 | - if ($this->keepNullAttributes) { |
|
82 | - return $this->escapeIfNeeded($escaped, $value); |
|
83 | - } |
|
84 | - |
|
85 | - $valueCheck = $value; |
|
86 | - |
|
87 | - return $this->escapeIfNeeded($escaped, '$__value'); |
|
88 | - } |
|
89 | - |
|
90 | - protected function getAttributeValue($escaped, $key, $value, &$classesCheck, &$valueCheck) |
|
91 | - { |
|
92 | - if ($this->isConstant($value)) { |
|
93 | - $value = trim($value, ' \'"'); |
|
94 | - |
|
95 | - return $value === 'undefined' ? 'null' : $value; |
|
96 | - } |
|
97 | - |
|
98 | - $json = static::parseValue($value); |
|
99 | - |
|
100 | - if ($key === 'class') { |
|
101 | - return $json !== null && is_array($json) |
|
102 | - ? implode(' ', $json) |
|
103 | - : $this->getClassAttribute($value, $classesCheck); |
|
104 | - } |
|
105 | - |
|
106 | - return $this->getValueCode($escaped, $value, $valueCheck); |
|
107 | - } |
|
108 | - |
|
109 | - protected function escapeValueIfNeeded($value, $escaped, $valueCheck) |
|
110 | - { |
|
111 | - return is_null($valueCheck) && $escaped && !$this->keepNullAttributes |
|
112 | - ? $this->escapeValue($value) |
|
113 | - : $value; |
|
114 | - } |
|
115 | - |
|
116 | - protected function compileAttributeValue($key, $value, $attr, $valueCheck) |
|
117 | - { |
|
118 | - return $value === true || $attr['value'] === true |
|
119 | - ? $this->getBooleanAttributeDisplayCode($key) |
|
120 | - : ($value !== false && $attr['value'] !== false && $value !== 'null' && $value !== 'undefined' |
|
121 | - ? $this->getAttributeDisplayCode( |
|
122 | - $key, |
|
123 | - $this->escapeValueIfNeeded($value, $attr['escaped'], $valueCheck), |
|
124 | - $valueCheck |
|
125 | - ) |
|
126 | - : '' |
|
127 | - ); |
|
128 | - } |
|
129 | - |
|
130 | - protected function getAttributeCode($attr, &$classes, &$classesCheck) |
|
131 | - { |
|
132 | - $key = trim($attr['name']); |
|
133 | - |
|
134 | - if ($key === '&attributes') { |
|
135 | - return $this->getAndAttributeCode($attr, $classes, $classesCheck); |
|
136 | - } |
|
137 | - |
|
138 | - $valueCheck = null; |
|
139 | - $value = trim($attr['value']); |
|
140 | - |
|
141 | - $value = $this->getAttributeValue($attr['escaped'], $key, $value, $classesCheck, $valueCheck); |
|
142 | - |
|
143 | - if ($key === 'class') { |
|
144 | - if ($value !== 'false' && $value !== 'null' && $value !== 'undefined') { |
|
145 | - array_push($classes, $value); |
|
146 | - } |
|
147 | - |
|
148 | - return ''; |
|
149 | - } |
|
150 | - |
|
151 | - return $this->compileAttributeValue($key, $value, $attr, $valueCheck); |
|
152 | - } |
|
153 | - |
|
154 | - protected function getClassesCode(&$classes, &$classesCheck) |
|
155 | - { |
|
156 | - return trim($this->createCode( |
|
157 | - '$__classes = implode(" ", ' . |
|
158 | - 'array_unique(explode(" ", (empty($__classes) ? "" : $__classes) . ' . |
|
159 | - var_export(implode(' ', $classes), true) . ' . ' . |
|
160 | - 'implode(" ", array(' . implode(', ', $classesCheck) . ')) ' . |
|
161 | - ')) ' . |
|
162 | - ');' |
|
163 | - )); |
|
164 | - } |
|
165 | - |
|
166 | - protected function getClassesDisplayCode() |
|
167 | - { |
|
168 | - return trim($this->createCode( |
|
169 | - 'if (!empty($__classes)) { ' . |
|
170 | - '?> ' . (isset($this->options['classAttribute']) |
|
171 | - ? $this->options['classAttribute'] |
|
172 | - : 'class' |
|
173 | - ) . '=' . $this->quote . '<?php echo $__classes; ?>' . $this->quote . '<?php ' . |
|
174 | - '} ' . |
|
175 | - 'unset($__classes); ' |
|
176 | - )); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * @param array $attributes |
|
181 | - */ |
|
182 | - protected function compileAttributes($attributes) |
|
183 | - { |
|
184 | - $items = ''; |
|
185 | - $classes = array(); |
|
186 | - $classesCheck = array(); |
|
187 | - |
|
188 | - foreach ($attributes as $attr) { |
|
189 | - $items .= $this->getAttributeCode($attr, $classes, $classesCheck); |
|
190 | - } |
|
191 | - |
|
192 | - $items .= $this->getClassesCode($classes, $classesCheck); |
|
193 | - |
|
194 | - $this->buffer($items, false); |
|
195 | - } |
|
7 | + protected function getAttributeDisplayCode($key, $value, $valueCheck) |
|
8 | + { |
|
9 | + if ($key === 'style') { |
|
10 | + $value = preg_replace('/::get(Escaped|Unescaped)Value/', '::get$1Style', $value, 1); |
|
11 | + } |
|
12 | + |
|
13 | + return is_null($valueCheck) |
|
14 | + ? ' ' . $key . '=' . $this->quote . $value . $this->quote |
|
15 | + : $this->createCode('if (true === ($__value = %1$s)) { ', $valueCheck) |
|
16 | + . $this->getBooleanAttributeDisplayCode($key) |
|
17 | + . $this->createCode('} else if (\\Jade\\Compiler::isDisplayable($__value)) { ') |
|
18 | + . ' ' . $key . '=' . $this->quote . $value . $this->quote |
|
19 | + . $this->createCode('}'); |
|
20 | + } |
|
21 | + |
|
22 | + protected function getBooleanAttributeDisplayCode($key) |
|
23 | + { |
|
24 | + return ' ' . $key . ($this->terse |
|
25 | + ? '' |
|
26 | + : '=' . $this->quote . $key . $this->quote |
|
27 | + ); |
|
28 | + } |
|
29 | + |
|
30 | + protected function getValueStatement($statements) |
|
31 | + { |
|
32 | + return is_string($statements[0]) |
|
33 | + ? $statements[0] |
|
34 | + : $statements[0][0]; |
|
35 | + } |
|
36 | + |
|
37 | + protected function getAndAttributeCode($attr, &$classes, &$classesCheck) |
|
38 | + { |
|
39 | + $addClasses = '""'; |
|
40 | + if (count($classes) || count($classesCheck)) { |
|
41 | + foreach ($classes as &$value) { |
|
42 | + $value = var_export($value, true); |
|
43 | + } |
|
44 | + foreach ($classesCheck as $value) { |
|
45 | + $statements = $this->createStatements($value); |
|
46 | + $classes[] = $statements[0][0]; |
|
47 | + } |
|
48 | + $addClasses = '" " . implode(" ", array(' . implode(', ', $classes) . '))'; |
|
49 | + $classes = array(); |
|
50 | + $classesCheck = array(); |
|
51 | + } |
|
52 | + $value = empty($attr['value']) ? 'attributes' : $attr['value']; |
|
53 | + $statements = $this->createStatements($value); |
|
54 | + |
|
55 | + return $this->createCode( |
|
56 | + '$__attributes = ' . $this->getValueStatement($statements) . ';' . |
|
57 | + 'if (is_array($__attributes)) { ' . |
|
58 | + '$__attributes["class"] = trim(' . |
|
59 | + '$__classes = (empty($__classes) ? "" : $__classes . " ") . ' . |
|
60 | + '(isset($__attributes["class"]) ? (is_array($__attributes["class"]) ? implode(" ", $__attributes["class"]) : $__attributes["class"]) : "") . ' . |
|
61 | + $addClasses . |
|
62 | + '); ' . |
|
63 | + 'if (empty($__attributes["class"])) { ' . |
|
64 | + 'unset($__attributes["class"]); ' . |
|
65 | + '} ' . |
|
66 | + '} ' . |
|
67 | + '\\Jade\\Compiler::displayAttributes($__attributes, ' . var_export($this->quote, true) . ', ' . var_export($this->terse, true) . ');'); |
|
68 | + } |
|
69 | + |
|
70 | + protected function getClassAttribute($value, &$classesCheck) |
|
71 | + { |
|
72 | + $statements = $this->createStatements($value); |
|
73 | + $value = is_array($statements[0]) ? $statements[0][0] : $statements[0]; |
|
74 | + $classesCheck[] = '(is_array($_a = ' . $value . ') ? implode(" ", $_a) : $_a)'; |
|
75 | + |
|
76 | + return $this->keepNullAttributes ? '' : 'null'; |
|
77 | + } |
|
78 | + |
|
79 | + protected function getValueCode($escaped, $value, &$valueCheck) |
|
80 | + { |
|
81 | + if ($this->keepNullAttributes) { |
|
82 | + return $this->escapeIfNeeded($escaped, $value); |
|
83 | + } |
|
84 | + |
|
85 | + $valueCheck = $value; |
|
86 | + |
|
87 | + return $this->escapeIfNeeded($escaped, '$__value'); |
|
88 | + } |
|
89 | + |
|
90 | + protected function getAttributeValue($escaped, $key, $value, &$classesCheck, &$valueCheck) |
|
91 | + { |
|
92 | + if ($this->isConstant($value)) { |
|
93 | + $value = trim($value, ' \'"'); |
|
94 | + |
|
95 | + return $value === 'undefined' ? 'null' : $value; |
|
96 | + } |
|
97 | + |
|
98 | + $json = static::parseValue($value); |
|
99 | + |
|
100 | + if ($key === 'class') { |
|
101 | + return $json !== null && is_array($json) |
|
102 | + ? implode(' ', $json) |
|
103 | + : $this->getClassAttribute($value, $classesCheck); |
|
104 | + } |
|
105 | + |
|
106 | + return $this->getValueCode($escaped, $value, $valueCheck); |
|
107 | + } |
|
108 | + |
|
109 | + protected function escapeValueIfNeeded($value, $escaped, $valueCheck) |
|
110 | + { |
|
111 | + return is_null($valueCheck) && $escaped && !$this->keepNullAttributes |
|
112 | + ? $this->escapeValue($value) |
|
113 | + : $value; |
|
114 | + } |
|
115 | + |
|
116 | + protected function compileAttributeValue($key, $value, $attr, $valueCheck) |
|
117 | + { |
|
118 | + return $value === true || $attr['value'] === true |
|
119 | + ? $this->getBooleanAttributeDisplayCode($key) |
|
120 | + : ($value !== false && $attr['value'] !== false && $value !== 'null' && $value !== 'undefined' |
|
121 | + ? $this->getAttributeDisplayCode( |
|
122 | + $key, |
|
123 | + $this->escapeValueIfNeeded($value, $attr['escaped'], $valueCheck), |
|
124 | + $valueCheck |
|
125 | + ) |
|
126 | + : '' |
|
127 | + ); |
|
128 | + } |
|
129 | + |
|
130 | + protected function getAttributeCode($attr, &$classes, &$classesCheck) |
|
131 | + { |
|
132 | + $key = trim($attr['name']); |
|
133 | + |
|
134 | + if ($key === '&attributes') { |
|
135 | + return $this->getAndAttributeCode($attr, $classes, $classesCheck); |
|
136 | + } |
|
137 | + |
|
138 | + $valueCheck = null; |
|
139 | + $value = trim($attr['value']); |
|
140 | + |
|
141 | + $value = $this->getAttributeValue($attr['escaped'], $key, $value, $classesCheck, $valueCheck); |
|
142 | + |
|
143 | + if ($key === 'class') { |
|
144 | + if ($value !== 'false' && $value !== 'null' && $value !== 'undefined') { |
|
145 | + array_push($classes, $value); |
|
146 | + } |
|
147 | + |
|
148 | + return ''; |
|
149 | + } |
|
150 | + |
|
151 | + return $this->compileAttributeValue($key, $value, $attr, $valueCheck); |
|
152 | + } |
|
153 | + |
|
154 | + protected function getClassesCode(&$classes, &$classesCheck) |
|
155 | + { |
|
156 | + return trim($this->createCode( |
|
157 | + '$__classes = implode(" ", ' . |
|
158 | + 'array_unique(explode(" ", (empty($__classes) ? "" : $__classes) . ' . |
|
159 | + var_export(implode(' ', $classes), true) . ' . ' . |
|
160 | + 'implode(" ", array(' . implode(', ', $classesCheck) . ')) ' . |
|
161 | + ')) ' . |
|
162 | + ');' |
|
163 | + )); |
|
164 | + } |
|
165 | + |
|
166 | + protected function getClassesDisplayCode() |
|
167 | + { |
|
168 | + return trim($this->createCode( |
|
169 | + 'if (!empty($__classes)) { ' . |
|
170 | + '?> ' . (isset($this->options['classAttribute']) |
|
171 | + ? $this->options['classAttribute'] |
|
172 | + : 'class' |
|
173 | + ) . '=' . $this->quote . '<?php echo $__classes; ?>' . $this->quote . '<?php ' . |
|
174 | + '} ' . |
|
175 | + 'unset($__classes); ' |
|
176 | + )); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * @param array $attributes |
|
181 | + */ |
|
182 | + protected function compileAttributes($attributes) |
|
183 | + { |
|
184 | + $items = ''; |
|
185 | + $classes = array(); |
|
186 | + $classesCheck = array(); |
|
187 | + |
|
188 | + foreach ($attributes as $attr) { |
|
189 | + $items .= $this->getAttributeCode($attr, $classes, $classesCheck); |
|
190 | + } |
|
191 | + |
|
192 | + $items .= $this->getClassesCode($classes, $classesCheck); |
|
193 | + |
|
194 | + $this->buffer($items, false); |
|
195 | + } |
|
196 | 196 | } |
@@ -11,19 +11,19 @@ discard block |
||
11 | 11 | } |
12 | 12 | |
13 | 13 | return is_null($valueCheck) |
14 | - ? ' ' . $key . '=' . $this->quote . $value . $this->quote |
|
14 | + ? ' '.$key.'='.$this->quote.$value.$this->quote |
|
15 | 15 | : $this->createCode('if (true === ($__value = %1$s)) { ', $valueCheck) |
16 | 16 | . $this->getBooleanAttributeDisplayCode($key) |
17 | 17 | . $this->createCode('} else if (\\Jade\\Compiler::isDisplayable($__value)) { ') |
18 | - . ' ' . $key . '=' . $this->quote . $value . $this->quote |
|
18 | + . ' '.$key.'='.$this->quote.$value.$this->quote |
|
19 | 19 | . $this->createCode('}'); |
20 | 20 | } |
21 | 21 | |
22 | 22 | protected function getBooleanAttributeDisplayCode($key) |
23 | 23 | { |
24 | - return ' ' . $key . ($this->terse |
|
24 | + return ' '.$key.($this->terse |
|
25 | 25 | ? '' |
26 | - : '=' . $this->quote . $key . $this->quote |
|
26 | + : '='.$this->quote.$key.$this->quote |
|
27 | 27 | ); |
28 | 28 | } |
29 | 29 | |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | $statements = $this->createStatements($value); |
46 | 46 | $classes[] = $statements[0][0]; |
47 | 47 | } |
48 | - $addClasses = '" " . implode(" ", array(' . implode(', ', $classes) . '))'; |
|
48 | + $addClasses = '" " . implode(" ", array('.implode(', ', $classes).'))'; |
|
49 | 49 | $classes = array(); |
50 | 50 | $classesCheck = array(); |
51 | 51 | } |
@@ -53,25 +53,25 @@ discard block |
||
53 | 53 | $statements = $this->createStatements($value); |
54 | 54 | |
55 | 55 | return $this->createCode( |
56 | - '$__attributes = ' . $this->getValueStatement($statements) . ';' . |
|
57 | - 'if (is_array($__attributes)) { ' . |
|
58 | - '$__attributes["class"] = trim(' . |
|
59 | - '$__classes = (empty($__classes) ? "" : $__classes . " ") . ' . |
|
60 | - '(isset($__attributes["class"]) ? (is_array($__attributes["class"]) ? implode(" ", $__attributes["class"]) : $__attributes["class"]) : "") . ' . |
|
61 | - $addClasses . |
|
62 | - '); ' . |
|
63 | - 'if (empty($__attributes["class"])) { ' . |
|
64 | - 'unset($__attributes["class"]); ' . |
|
65 | - '} ' . |
|
66 | - '} ' . |
|
67 | - '\\Jade\\Compiler::displayAttributes($__attributes, ' . var_export($this->quote, true) . ', ' . var_export($this->terse, true) . ');'); |
|
56 | + '$__attributes = '.$this->getValueStatement($statements).';'. |
|
57 | + 'if (is_array($__attributes)) { '. |
|
58 | + '$__attributes["class"] = trim('. |
|
59 | + '$__classes = (empty($__classes) ? "" : $__classes . " ") . '. |
|
60 | + '(isset($__attributes["class"]) ? (is_array($__attributes["class"]) ? implode(" ", $__attributes["class"]) : $__attributes["class"]) : "") . '. |
|
61 | + $addClasses. |
|
62 | + '); '. |
|
63 | + 'if (empty($__attributes["class"])) { '. |
|
64 | + 'unset($__attributes["class"]); '. |
|
65 | + '} '. |
|
66 | + '} '. |
|
67 | + '\\Jade\\Compiler::displayAttributes($__attributes, '.var_export($this->quote, true).', '.var_export($this->terse, true).');'); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | protected function getClassAttribute($value, &$classesCheck) |
71 | 71 | { |
72 | 72 | $statements = $this->createStatements($value); |
73 | 73 | $value = is_array($statements[0]) ? $statements[0][0] : $statements[0]; |
74 | - $classesCheck[] = '(is_array($_a = ' . $value . ') ? implode(" ", $_a) : $_a)'; |
|
74 | + $classesCheck[] = '(is_array($_a = '.$value.') ? implode(" ", $_a) : $_a)'; |
|
75 | 75 | |
76 | 76 | return $this->keepNullAttributes ? '' : 'null'; |
77 | 77 | } |
@@ -154,11 +154,11 @@ discard block |
||
154 | 154 | protected function getClassesCode(&$classes, &$classesCheck) |
155 | 155 | { |
156 | 156 | return trim($this->createCode( |
157 | - '$__classes = implode(" ", ' . |
|
158 | - 'array_unique(explode(" ", (empty($__classes) ? "" : $__classes) . ' . |
|
159 | - var_export(implode(' ', $classes), true) . ' . ' . |
|
160 | - 'implode(" ", array(' . implode(', ', $classesCheck) . ')) ' . |
|
161 | - ')) ' . |
|
157 | + '$__classes = implode(" ", '. |
|
158 | + 'array_unique(explode(" ", (empty($__classes) ? "" : $__classes) . '. |
|
159 | + var_export(implode(' ', $classes), true).' . '. |
|
160 | + 'implode(" ", array('.implode(', ', $classesCheck).')) '. |
|
161 | + ')) '. |
|
162 | 162 | ');' |
163 | 163 | )); |
164 | 164 | } |
@@ -166,12 +166,12 @@ discard block |
||
166 | 166 | protected function getClassesDisplayCode() |
167 | 167 | { |
168 | 168 | return trim($this->createCode( |
169 | - 'if (!empty($__classes)) { ' . |
|
170 | - '?> ' . (isset($this->options['classAttribute']) |
|
169 | + 'if (!empty($__classes)) { '. |
|
170 | + '?> '.(isset($this->options['classAttribute']) |
|
171 | 171 | ? $this->options['classAttribute'] |
172 | 172 | : 'class' |
173 | - ) . '=' . $this->quote . '<?php echo $__classes; ?>' . $this->quote . '<?php ' . |
|
174 | - '} ' . |
|
173 | + ).'='.$this->quote.'<?php echo $__classes; ?>'.$this->quote.'<?php '. |
|
174 | + '} '. |
|
175 | 175 | 'unset($__classes); ' |
176 | 176 | )); |
177 | 177 | } |
@@ -133,7 +133,7 @@ |
||
133 | 133 | * |
134 | 134 | * @param string $directory the directory to search in pug templates |
135 | 135 | * |
136 | - * @return array count of cached files and error count |
|
136 | + * @return integer[] count of cached files and error count |
|
137 | 137 | */ |
138 | 138 | public function cacheDirectory($directory) |
139 | 139 | { |
@@ -7,163 +7,163 @@ |
||
7 | 7 | |
8 | 8 | class CacheHelper |
9 | 9 | { |
10 | - protected $pug; |
|
11 | - |
|
12 | - public function __construct(Jade $pug) |
|
13 | - { |
|
14 | - $this->pug = $pug; |
|
15 | - } |
|
16 | - |
|
17 | - /** |
|
18 | - * Return a file path in the cache for a given name. |
|
19 | - * |
|
20 | - * @param string $name |
|
21 | - * |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - protected function getCachePath($name) |
|
25 | - { |
|
26 | - return str_replace('//', '/', $this->pug->getOption('cache') . '/' . $name) . '.php'; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Return a hashed print from input file or content. |
|
31 | - * |
|
32 | - * @param string $input |
|
33 | - * |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - protected function hashPrint($input) |
|
37 | - { |
|
38 | - // Get the stronger hashing algorithm available to minimize collision risks |
|
39 | - $algos = hash_algos(); |
|
40 | - $algo = $algos[0]; |
|
41 | - $number = 0; |
|
42 | - foreach ($algos as $hashAlgorithm) { |
|
43 | - if (strpos($hashAlgorithm, 'md') === 0) { |
|
44 | - $hashNumber = substr($hashAlgorithm, 2); |
|
45 | - if ($hashNumber > $number) { |
|
46 | - $number = $hashNumber; |
|
47 | - $algo = $hashAlgorithm; |
|
48 | - } |
|
49 | - continue; |
|
50 | - } |
|
51 | - if (strpos($hashAlgorithm, 'sha') === 0) { |
|
52 | - $hashNumber = substr($hashAlgorithm, 3); |
|
53 | - if ($hashNumber > $number) { |
|
54 | - $number = $hashNumber; |
|
55 | - $algo = $hashAlgorithm; |
|
56 | - } |
|
57 | - continue; |
|
58 | - } |
|
59 | - } |
|
60 | - |
|
61 | - return rtrim(strtr(base64_encode(hash($algo, $input, true)), '+/', '-_'), '='); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Return true if the file or content is up to date in the cache folder, |
|
66 | - * false else. |
|
67 | - * |
|
68 | - * @param string $input file or pug code |
|
69 | - * @param &string $path to be filled |
|
70 | - * |
|
71 | - * @return bool |
|
72 | - */ |
|
73 | - protected function isCacheUpToDate($input, &$path) |
|
74 | - { |
|
75 | - if (is_file($input)) { |
|
76 | - $path = $this->getCachePath( |
|
77 | - ($this->pug->getOption('keepBaseName') ? basename($input) : '') . |
|
78 | - $this->hashPrint(realpath($input)) |
|
79 | - ); |
|
80 | - |
|
81 | - // Do not re-parse file if original is older |
|
82 | - return (!$this->pug->getOption('upToDateCheck')) || (file_exists($path) && filemtime($input) < filemtime($path)); |
|
83 | - } |
|
84 | - |
|
85 | - $path = $this->getCachePath($this->hashPrint($input)); |
|
86 | - |
|
87 | - // Do not re-parse file if the same hash exists |
|
88 | - return file_exists($path); |
|
89 | - } |
|
90 | - |
|
91 | - protected function getCacheDirectory() |
|
92 | - { |
|
93 | - $cacheFolder = $this->pug->getOption('cache'); |
|
94 | - |
|
95 | - if (!is_dir($cacheFolder)) { |
|
96 | - throw new \ErrorException($cacheFolder . ': Cache directory seem\'s to not exists', 5); |
|
97 | - } |
|
98 | - |
|
99 | - return $cacheFolder; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Get cached input/file a matching cache file exists. |
|
104 | - * Else, render the input, cache it in a file and return it. |
|
105 | - * |
|
106 | - * @param string input |
|
107 | - * |
|
108 | - * @throws \InvalidArgumentException |
|
109 | - * @throws \Exception |
|
110 | - * |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - public function cache($input) |
|
114 | - { |
|
115 | - $cacheFolder = $this->getCacheDirectory(); |
|
116 | - |
|
117 | - if ($this->isCacheUpToDate($input, $path)) { |
|
118 | - return $path; |
|
119 | - } |
|
120 | - |
|
121 | - if (!is_writable($cacheFolder)) { |
|
122 | - throw new \ErrorException(sprintf('Cache directory must be writable. "%s" is not.', $cacheFolder), 6); |
|
123 | - } |
|
124 | - |
|
125 | - $rendered = $this->pug->compile($input); |
|
126 | - file_put_contents($path, $rendered); |
|
127 | - |
|
128 | - return $this->pug->stream($rendered); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Scan a directory recursively, compile them and save them into the cache directory. |
|
133 | - * |
|
134 | - * @param string $directory the directory to search in pug templates |
|
135 | - * |
|
136 | - * @return array count of cached files and error count |
|
137 | - */ |
|
138 | - public function cacheDirectory($directory) |
|
139 | - { |
|
140 | - $success = 0; |
|
141 | - $errors = 0; |
|
142 | - |
|
143 | - $extensions = new ExtensionsHelper($this->pug->getOption('extension')); |
|
144 | - |
|
145 | - foreach (scandir($directory) as $object) { |
|
146 | - if ($object === '.' || $object === '..') { |
|
147 | - continue; |
|
148 | - } |
|
149 | - $input = $directory . DIRECTORY_SEPARATOR . $object; |
|
150 | - if (is_dir($input)) { |
|
151 | - list($subSuccess, $subErrors) = $this->cacheDirectory($input); |
|
152 | - $success += $subSuccess; |
|
153 | - $errors += $subErrors; |
|
154 | - continue; |
|
155 | - } |
|
156 | - if ($extensions->hasValidTemplateExtension($object)) { |
|
157 | - $this->isCacheUpToDate($input, $path); |
|
158 | - try { |
|
159 | - file_put_contents($path, $this->pug->compile($input)); |
|
160 | - $success++; |
|
161 | - } catch (\Exception $e) { |
|
162 | - $errors++; |
|
163 | - } |
|
164 | - } |
|
165 | - } |
|
166 | - |
|
167 | - return array($success, $errors); |
|
168 | - } |
|
10 | + protected $pug; |
|
11 | + |
|
12 | + public function __construct(Jade $pug) |
|
13 | + { |
|
14 | + $this->pug = $pug; |
|
15 | + } |
|
16 | + |
|
17 | + /** |
|
18 | + * Return a file path in the cache for a given name. |
|
19 | + * |
|
20 | + * @param string $name |
|
21 | + * |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + protected function getCachePath($name) |
|
25 | + { |
|
26 | + return str_replace('//', '/', $this->pug->getOption('cache') . '/' . $name) . '.php'; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Return a hashed print from input file or content. |
|
31 | + * |
|
32 | + * @param string $input |
|
33 | + * |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + protected function hashPrint($input) |
|
37 | + { |
|
38 | + // Get the stronger hashing algorithm available to minimize collision risks |
|
39 | + $algos = hash_algos(); |
|
40 | + $algo = $algos[0]; |
|
41 | + $number = 0; |
|
42 | + foreach ($algos as $hashAlgorithm) { |
|
43 | + if (strpos($hashAlgorithm, 'md') === 0) { |
|
44 | + $hashNumber = substr($hashAlgorithm, 2); |
|
45 | + if ($hashNumber > $number) { |
|
46 | + $number = $hashNumber; |
|
47 | + $algo = $hashAlgorithm; |
|
48 | + } |
|
49 | + continue; |
|
50 | + } |
|
51 | + if (strpos($hashAlgorithm, 'sha') === 0) { |
|
52 | + $hashNumber = substr($hashAlgorithm, 3); |
|
53 | + if ($hashNumber > $number) { |
|
54 | + $number = $hashNumber; |
|
55 | + $algo = $hashAlgorithm; |
|
56 | + } |
|
57 | + continue; |
|
58 | + } |
|
59 | + } |
|
60 | + |
|
61 | + return rtrim(strtr(base64_encode(hash($algo, $input, true)), '+/', '-_'), '='); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Return true if the file or content is up to date in the cache folder, |
|
66 | + * false else. |
|
67 | + * |
|
68 | + * @param string $input file or pug code |
|
69 | + * @param &string $path to be filled |
|
70 | + * |
|
71 | + * @return bool |
|
72 | + */ |
|
73 | + protected function isCacheUpToDate($input, &$path) |
|
74 | + { |
|
75 | + if (is_file($input)) { |
|
76 | + $path = $this->getCachePath( |
|
77 | + ($this->pug->getOption('keepBaseName') ? basename($input) : '') . |
|
78 | + $this->hashPrint(realpath($input)) |
|
79 | + ); |
|
80 | + |
|
81 | + // Do not re-parse file if original is older |
|
82 | + return (!$this->pug->getOption('upToDateCheck')) || (file_exists($path) && filemtime($input) < filemtime($path)); |
|
83 | + } |
|
84 | + |
|
85 | + $path = $this->getCachePath($this->hashPrint($input)); |
|
86 | + |
|
87 | + // Do not re-parse file if the same hash exists |
|
88 | + return file_exists($path); |
|
89 | + } |
|
90 | + |
|
91 | + protected function getCacheDirectory() |
|
92 | + { |
|
93 | + $cacheFolder = $this->pug->getOption('cache'); |
|
94 | + |
|
95 | + if (!is_dir($cacheFolder)) { |
|
96 | + throw new \ErrorException($cacheFolder . ': Cache directory seem\'s to not exists', 5); |
|
97 | + } |
|
98 | + |
|
99 | + return $cacheFolder; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Get cached input/file a matching cache file exists. |
|
104 | + * Else, render the input, cache it in a file and return it. |
|
105 | + * |
|
106 | + * @param string input |
|
107 | + * |
|
108 | + * @throws \InvalidArgumentException |
|
109 | + * @throws \Exception |
|
110 | + * |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + public function cache($input) |
|
114 | + { |
|
115 | + $cacheFolder = $this->getCacheDirectory(); |
|
116 | + |
|
117 | + if ($this->isCacheUpToDate($input, $path)) { |
|
118 | + return $path; |
|
119 | + } |
|
120 | + |
|
121 | + if (!is_writable($cacheFolder)) { |
|
122 | + throw new \ErrorException(sprintf('Cache directory must be writable. "%s" is not.', $cacheFolder), 6); |
|
123 | + } |
|
124 | + |
|
125 | + $rendered = $this->pug->compile($input); |
|
126 | + file_put_contents($path, $rendered); |
|
127 | + |
|
128 | + return $this->pug->stream($rendered); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Scan a directory recursively, compile them and save them into the cache directory. |
|
133 | + * |
|
134 | + * @param string $directory the directory to search in pug templates |
|
135 | + * |
|
136 | + * @return array count of cached files and error count |
|
137 | + */ |
|
138 | + public function cacheDirectory($directory) |
|
139 | + { |
|
140 | + $success = 0; |
|
141 | + $errors = 0; |
|
142 | + |
|
143 | + $extensions = new ExtensionsHelper($this->pug->getOption('extension')); |
|
144 | + |
|
145 | + foreach (scandir($directory) as $object) { |
|
146 | + if ($object === '.' || $object === '..') { |
|
147 | + continue; |
|
148 | + } |
|
149 | + $input = $directory . DIRECTORY_SEPARATOR . $object; |
|
150 | + if (is_dir($input)) { |
|
151 | + list($subSuccess, $subErrors) = $this->cacheDirectory($input); |
|
152 | + $success += $subSuccess; |
|
153 | + $errors += $subErrors; |
|
154 | + continue; |
|
155 | + } |
|
156 | + if ($extensions->hasValidTemplateExtension($object)) { |
|
157 | + $this->isCacheUpToDate($input, $path); |
|
158 | + try { |
|
159 | + file_put_contents($path, $this->pug->compile($input)); |
|
160 | + $success++; |
|
161 | + } catch (\Exception $e) { |
|
162 | + $errors++; |
|
163 | + } |
|
164 | + } |
|
165 | + } |
|
166 | + |
|
167 | + return array($success, $errors); |
|
168 | + } |
|
169 | 169 | } |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | */ |
24 | 24 | protected function getCachePath($name) |
25 | 25 | { |
26 | - return str_replace('//', '/', $this->pug->getOption('cache') . '/' . $name) . '.php'; |
|
26 | + return str_replace('//', '/', $this->pug->getOption('cache').'/'.$name).'.php'; |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | /** |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | { |
75 | 75 | if (is_file($input)) { |
76 | 76 | $path = $this->getCachePath( |
77 | - ($this->pug->getOption('keepBaseName') ? basename($input) : '') . |
|
77 | + ($this->pug->getOption('keepBaseName') ? basename($input) : ''). |
|
78 | 78 | $this->hashPrint(realpath($input)) |
79 | 79 | ); |
80 | 80 | |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | $cacheFolder = $this->pug->getOption('cache'); |
94 | 94 | |
95 | 95 | if (!is_dir($cacheFolder)) { |
96 | - throw new \ErrorException($cacheFolder . ': Cache directory seem\'s to not exists', 5); |
|
96 | + throw new \ErrorException($cacheFolder.': Cache directory seem\'s to not exists', 5); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | return $cacheFolder; |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | if ($object === '.' || $object === '..') { |
147 | 147 | continue; |
148 | 148 | } |
149 | - $input = $directory . DIRECTORY_SEPARATOR . $object; |
|
149 | + $input = $directory.DIRECTORY_SEPARATOR.$object; |
|
150 | 150 | if (is_dir($input)) { |
151 | 151 | list($subSuccess, $subErrors) = $this->cacheDirectory($input); |
152 | 152 | $success += $subSuccess; |
@@ -26,6 +26,10 @@ discard block |
||
26 | 26 | $this->separators = array(); |
27 | 27 | } |
28 | 28 | |
29 | + /** |
|
30 | + * @param string $input |
|
31 | + * @param string $name |
|
32 | + */ |
|
29 | 33 | public function innerCode($input, $name) |
30 | 34 | { |
31 | 35 | $handler = new static($input, $name); |
@@ -119,6 +123,9 @@ discard block |
||
119 | 123 | $consume($argument, $match[0]); |
120 | 124 | } |
121 | 125 | |
126 | + /** |
|
127 | + * @param string[] $match |
|
128 | + */ |
|
122 | 129 | protected function parseArrayElement(&$argument, $match, $consume, &$quote, &$key, &$value) |
123 | 130 | { |
124 | 131 | switch ($match[2]) { |
@@ -171,6 +178,9 @@ discard block |
||
171 | 178 | return $handleRecursion(array($sep, end($separators))); |
172 | 179 | } |
173 | 180 | |
181 | + /** |
|
182 | + * @param SubCodeHandler $subCodeHandler |
|
183 | + */ |
|
174 | 184 | protected function parseSeparator($sep, &$separators, &$result, &$varname, $subCodeHandler, $innerName) |
175 | 185 | { |
176 | 186 | $handleCodeInbetween = $subCodeHandler->handleCodeInbetween($separators, $result); |
@@ -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 | } |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | public function __construct($input, $name) |
15 | 15 | { |
16 | 16 | if (!is_string($input)) { |
17 | - throw new \InvalidArgumentException('Expecting a string of PHP, got: ' . gettype($input), 11); |
|
17 | + throw new \InvalidArgumentException('Expecting a string of PHP, got: '.gettype($input), 11); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | if (strlen($input) === 0) { |
@@ -40,15 +40,15 @@ discard block |
||
40 | 40 | } |
41 | 41 | |
42 | 42 | if (strpos('=,;?', substr($this->input, 0, 1)) !== false) { |
43 | - throw new \ErrorException('Expecting a variable name or an expression, got: ' . $this->input, 13); |
|
43 | + throw new \ErrorException('Expecting a variable name or an expression, got: '.$this->input, 13); |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | preg_match_all( |
47 | 47 | '/(?<![<>=!])=(?!>|=)|[\[\]\{\}\(\),;\.]|(?!:):|->/', // punctuation |
48 | - preg_replace_callback('/[a-zA-Z0-9\\\\_\\x7f-\\xff]*\((?:[0-9\/%\.\s*+-]++|(?R))*+\)/', function ($match) { |
|
48 | + preg_replace_callback('/[a-zA-Z0-9\\\\_\\x7f-\\xff]*\((?:[0-9\/%\.\s*+-]++|(?R))*+\)/', function($match) { |
|
49 | 49 | // no need to keep separators in simple PHP expressions (functions calls, parentheses, calculs) |
50 | 50 | return str_repeat(' ', strlen($match[0])); |
51 | - }, preg_replace_callback('/([\'"]).*?(?<!\\\\)(?:\\\\{2})*\\1/', function ($match) { |
|
51 | + }, preg_replace_callback('/([\'"]).*?(?<!\\\\)(?:\\\\{2})*\\1/', function($match) { |
|
52 | 52 | // do not take separators in strings |
53 | 53 | return str_repeat(' ', strlen($match[0])); |
54 | 54 | }, $this->input)), |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | } |
112 | 112 | |
113 | 113 | if (!is_null($value)) { |
114 | - throw new \ErrorException('Parse error on ' . substr($argument, strlen($match[1])), 15); |
|
114 | + throw new \ErrorException('Parse error on '.substr($argument, strlen($match[1])), 15); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | $key .= $match[1]; |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | $addToOutput(); |
155 | 155 | } |
156 | 156 | |
157 | - return 'array(' . implode(', ', $output) . ')'; |
|
157 | + return 'array('.implode(', ', $output).')'; |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | protected function parseEqual($sep, &$separators, &$result, $innerName, $subCodeHandler) |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | protected function parseSeparator($sep, &$separators, &$result, &$varname, $subCodeHandler, $innerName) |
175 | 175 | { |
176 | 176 | $handleCodeInbetween = $subCodeHandler->handleCodeInbetween($separators, $result); |
177 | - $var = '$__' . $this->name; |
|
177 | + $var = '$__'.$this->name; |
|
178 | 178 | |
179 | 179 | switch ($sep[0]) { |
180 | 180 | // translate the javascript's obj.attr into php's obj->attr or obj['attr'] |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | // funcall |
191 | 191 | case '(': |
192 | 192 | $arguments = $handleCodeInbetween(); |
193 | - $call = $varname . '(' . implode(', ', $arguments) . ')'; |
|
193 | + $call = $varname.'('.implode(', ', $arguments).')'; |
|
194 | 194 | $call = static::addDollarIfNeeded($call); |
195 | 195 | $varname = $var; |
196 | 196 | array_push($result, "{$var}={$call}"); |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | |
199 | 199 | case '[': |
200 | 200 | if (preg_match('/[a-zA-Z0-9\\\\_\\x7f-\\xff]$/', $varname)) { |
201 | - $varname .= $sep[0] . $innerName; |
|
201 | + $varname .= $sep[0].$innerName; |
|
202 | 202 | break; |
203 | 203 | } |
204 | 204 | case '{': |
@@ -206,12 +206,12 @@ discard block |
||
206 | 206 | break; |
207 | 207 | |
208 | 208 | case '=': |
209 | - $varname .= '=' . $this->parseEqual($sep, $separators, $result, $innerName, $subCodeHandler); |
|
209 | + $varname .= '='.$this->parseEqual($sep, $separators, $result, $innerName, $subCodeHandler); |
|
210 | 210 | break; |
211 | 211 | |
212 | 212 | default: |
213 | 213 | if (($innerName !== false && $innerName !== '') || $sep[0] !== ')') { |
214 | - $varname .= $sep[0] . $innerName; |
|
214 | + $varname .= $sep[0].$innerName; |
|
215 | 215 | } |
216 | 216 | break; |
217 | 217 | } |
@@ -7,7 +7,6 @@ discard block |
||
7 | 7 | abstract class CodeVisitor extends TagVisitor |
8 | 8 | { |
9 | 9 | /** |
10 | - * @param Nodes\Code $node |
|
11 | 10 | */ |
12 | 11 | protected function visitCodeConditional(array $matches) |
13 | 12 | { |
@@ -44,7 +43,7 @@ discard block |
||
44 | 43 | } |
45 | 44 | |
46 | 45 | /** |
47 | - * @param Nodes\Code $node |
|
46 | + * @param Code $node |
|
48 | 47 | */ |
49 | 48 | protected function visitCodeOpening(Code $node) |
50 | 49 | { |
@@ -68,7 +67,7 @@ discard block |
||
68 | 67 | } |
69 | 68 | |
70 | 69 | /** |
71 | - * @param Nodes\Code $node |
|
70 | + * @param Code $node |
|
72 | 71 | */ |
73 | 72 | protected function visitCode(Code $node) |
74 | 73 | { |
@@ -6,82 +6,82 @@ |
||
6 | 6 | |
7 | 7 | abstract class CodeVisitor extends TagVisitor |
8 | 8 | { |
9 | - /** |
|
10 | - * @param Nodes\Code $node |
|
11 | - */ |
|
12 | - protected function visitCodeConditional(array $matches) |
|
13 | - { |
|
14 | - $code = trim($matches[2], '; '); |
|
15 | - while (($len = strlen($code)) > 1 && ($code[0] === '(' || $code[0] === '{') && ord($code[0]) === ord(substr($code, -1)) - 1) { |
|
16 | - $code = trim(substr($code, 1, $len - 2)); |
|
17 | - } |
|
18 | - |
|
19 | - $index = count($this->buffer) - 1; |
|
20 | - $conditional = ''; |
|
21 | - |
|
22 | - if (isset($this->buffer[$index]) && false !== strpos($this->buffer[$index], $this->createCode('}'))) { |
|
23 | - // the "else" statement needs to be in the php block that closes the if |
|
24 | - $this->buffer[$index] = null; |
|
25 | - $conditional .= '} '; |
|
26 | - } |
|
27 | - |
|
28 | - $conditional .= '%s'; |
|
29 | - |
|
30 | - if (strlen($code) > 0) { |
|
31 | - $conditional .= '(%s) {'; |
|
32 | - $conditional = $matches[1] === 'unless' |
|
33 | - ? sprintf($conditional, 'if', '!(%s)') |
|
34 | - : sprintf($conditional, $matches[1], '%s'); |
|
35 | - $this->buffer($this->createCode($conditional, $code)); |
|
36 | - |
|
37 | - return; |
|
38 | - } |
|
39 | - |
|
40 | - $conditional .= ' {'; |
|
41 | - $conditional = sprintf($conditional, $matches[1]); |
|
42 | - |
|
43 | - $this->buffer($this->createCode($conditional)); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @param Nodes\Code $node |
|
48 | - */ |
|
49 | - protected function visitCodeOpening(Code $node) |
|
50 | - { |
|
51 | - $code = trim($node->value); |
|
52 | - |
|
53 | - if ($node->buffer) { |
|
54 | - $this->buffer($this->escapeIfNeeded($node->escape, $code)); |
|
55 | - |
|
56 | - return; |
|
57 | - } |
|
58 | - |
|
59 | - $phpOpen = implode('|', $this->phpOpenBlock); |
|
60 | - |
|
61 | - if (preg_match("/^[[:space:]]*({$phpOpen})(.*)/", $code, $matches)) { |
|
62 | - $this->visitCodeConditional($matches); |
|
63 | - |
|
64 | - return; |
|
65 | - } |
|
66 | - |
|
67 | - $this->buffer($this->createCode('%s', $code)); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param Nodes\Code $node |
|
72 | - */ |
|
73 | - protected function visitCode(Code $node) |
|
74 | - { |
|
75 | - $this->visitCodeOpening($node); |
|
76 | - |
|
77 | - if (isset($node->block)) { |
|
78 | - $this->indents++; |
|
79 | - $this->visit($node->block); |
|
80 | - $this->indents--; |
|
81 | - |
|
82 | - if (!$node->buffer) { |
|
83 | - $this->buffer($this->createCode('}')); |
|
84 | - } |
|
85 | - } |
|
86 | - } |
|
9 | + /** |
|
10 | + * @param Nodes\Code $node |
|
11 | + */ |
|
12 | + protected function visitCodeConditional(array $matches) |
|
13 | + { |
|
14 | + $code = trim($matches[2], '; '); |
|
15 | + while (($len = strlen($code)) > 1 && ($code[0] === '(' || $code[0] === '{') && ord($code[0]) === ord(substr($code, -1)) - 1) { |
|
16 | + $code = trim(substr($code, 1, $len - 2)); |
|
17 | + } |
|
18 | + |
|
19 | + $index = count($this->buffer) - 1; |
|
20 | + $conditional = ''; |
|
21 | + |
|
22 | + if (isset($this->buffer[$index]) && false !== strpos($this->buffer[$index], $this->createCode('}'))) { |
|
23 | + // the "else" statement needs to be in the php block that closes the if |
|
24 | + $this->buffer[$index] = null; |
|
25 | + $conditional .= '} '; |
|
26 | + } |
|
27 | + |
|
28 | + $conditional .= '%s'; |
|
29 | + |
|
30 | + if (strlen($code) > 0) { |
|
31 | + $conditional .= '(%s) {'; |
|
32 | + $conditional = $matches[1] === 'unless' |
|
33 | + ? sprintf($conditional, 'if', '!(%s)') |
|
34 | + : sprintf($conditional, $matches[1], '%s'); |
|
35 | + $this->buffer($this->createCode($conditional, $code)); |
|
36 | + |
|
37 | + return; |
|
38 | + } |
|
39 | + |
|
40 | + $conditional .= ' {'; |
|
41 | + $conditional = sprintf($conditional, $matches[1]); |
|
42 | + |
|
43 | + $this->buffer($this->createCode($conditional)); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @param Nodes\Code $node |
|
48 | + */ |
|
49 | + protected function visitCodeOpening(Code $node) |
|
50 | + { |
|
51 | + $code = trim($node->value); |
|
52 | + |
|
53 | + if ($node->buffer) { |
|
54 | + $this->buffer($this->escapeIfNeeded($node->escape, $code)); |
|
55 | + |
|
56 | + return; |
|
57 | + } |
|
58 | + |
|
59 | + $phpOpen = implode('|', $this->phpOpenBlock); |
|
60 | + |
|
61 | + if (preg_match("/^[[:space:]]*({$phpOpen})(.*)/", $code, $matches)) { |
|
62 | + $this->visitCodeConditional($matches); |
|
63 | + |
|
64 | + return; |
|
65 | + } |
|
66 | + |
|
67 | + $this->buffer($this->createCode('%s', $code)); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param Nodes\Code $node |
|
72 | + */ |
|
73 | + protected function visitCode(Code $node) |
|
74 | + { |
|
75 | + $this->visitCodeOpening($node); |
|
76 | + |
|
77 | + if (isset($node->block)) { |
|
78 | + $this->indents++; |
|
79 | + $this->visit($node->block); |
|
80 | + $this->indents--; |
|
81 | + |
|
82 | + if (!$node->buffer) { |
|
83 | + $this->buffer($this->createCode('}')); |
|
84 | + } |
|
85 | + } |
|
86 | + } |
|
87 | 87 | } |
@@ -56,8 +56,6 @@ |
||
56 | 56 | * Return true if the ending quote of the string is escaped. |
57 | 57 | * |
58 | 58 | * @param object|array $anything object or array (PHP >= 7) that contains a callable |
59 | - * @param string|int $key|$method key or method name |
|
60 | - * @param bool $isMethod true if the second argument is a method |
|
61 | 59 | * |
62 | 60 | * @return string |
63 | 61 | */ |
@@ -8,64 +8,64 @@ |
||
8 | 8 | */ |
9 | 9 | class CommonUtils |
10 | 10 | { |
11 | - /** |
|
12 | - * @param string $call |
|
13 | - * |
|
14 | - * @throws \InvalidArgumentException |
|
15 | - * |
|
16 | - * @return string |
|
17 | - */ |
|
18 | - public static function addDollarIfNeeded($call) |
|
19 | - { |
|
20 | - if ($call === 'Inf') { |
|
21 | - throw new \InvalidArgumentException($call . ' cannot be read from PHP', 16); |
|
22 | - } |
|
23 | - if ($call === 'undefined') { |
|
24 | - return 'null'; |
|
25 | - } |
|
26 | - $firstChar = substr($call, 0, 1); |
|
27 | - if ( |
|
28 | - !in_array($firstChar, array('$', '\\')) && |
|
29 | - !preg_match('#^(?:' . CompilerConfig::VARNAME . '\\s*\\(|(?:null|false|true)(?![a-z]))#i', $call) && |
|
30 | - ( |
|
31 | - preg_match('#^' . CompilerConfig::VARNAME . '#', $call) || |
|
32 | - $firstChar === '_' |
|
33 | - ) |
|
34 | - ) { |
|
35 | - $call = '$' . $call; |
|
36 | - } |
|
11 | + /** |
|
12 | + * @param string $call |
|
13 | + * |
|
14 | + * @throws \InvalidArgumentException |
|
15 | + * |
|
16 | + * @return string |
|
17 | + */ |
|
18 | + public static function addDollarIfNeeded($call) |
|
19 | + { |
|
20 | + if ($call === 'Inf') { |
|
21 | + throw new \InvalidArgumentException($call . ' cannot be read from PHP', 16); |
|
22 | + } |
|
23 | + if ($call === 'undefined') { |
|
24 | + return 'null'; |
|
25 | + } |
|
26 | + $firstChar = substr($call, 0, 1); |
|
27 | + if ( |
|
28 | + !in_array($firstChar, array('$', '\\')) && |
|
29 | + !preg_match('#^(?:' . CompilerConfig::VARNAME . '\\s*\\(|(?:null|false|true)(?![a-z]))#i', $call) && |
|
30 | + ( |
|
31 | + preg_match('#^' . CompilerConfig::VARNAME . '#', $call) || |
|
32 | + $firstChar === '_' |
|
33 | + ) |
|
34 | + ) { |
|
35 | + $call = '$' . $call; |
|
36 | + } |
|
37 | 37 | |
38 | - return $call; |
|
39 | - } |
|
38 | + return $call; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Return true if the ending quote of the string is escaped. |
|
43 | - * |
|
44 | - * @param string $quotedString |
|
45 | - * |
|
46 | - * @return bool |
|
47 | - */ |
|
48 | - public static function escapedEnd($quotedString) |
|
49 | - { |
|
50 | - $end = substr($quotedString, strlen(rtrim($quotedString, '\\'))); |
|
41 | + /** |
|
42 | + * Return true if the ending quote of the string is escaped. |
|
43 | + * |
|
44 | + * @param string $quotedString |
|
45 | + * |
|
46 | + * @return bool |
|
47 | + */ |
|
48 | + public static function escapedEnd($quotedString) |
|
49 | + { |
|
50 | + $end = substr($quotedString, strlen(rtrim($quotedString, '\\'))); |
|
51 | 51 | |
52 | - return substr($end, 0, 1) === '\\' && strlen($end) & 1; |
|
53 | - } |
|
52 | + return substr($end, 0, 1) === '\\' && strlen($end) & 1; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Return true if the ending quote of the string is escaped. |
|
57 | - * |
|
58 | - * @param object|array $anything object or array (PHP >= 7) that contains a callable |
|
59 | - * @param string|int $key|$method key or method name |
|
60 | - * @param bool $isMethod true if the second argument is a method |
|
61 | - * |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - public static function getGetter($anything, $key) |
|
65 | - { |
|
66 | - return '\\Jade\\Compiler::getPropertyFromAnything(' . |
|
67 | - static::addDollarIfNeeded($anything) . ', ' . |
|
68 | - var_export($key, true) . |
|
69 | - ')'; |
|
70 | - } |
|
55 | + /** |
|
56 | + * Return true if the ending quote of the string is escaped. |
|
57 | + * |
|
58 | + * @param object|array $anything object or array (PHP >= 7) that contains a callable |
|
59 | + * @param string|int $key|$method key or method name |
|
60 | + * @param bool $isMethod true if the second argument is a method |
|
61 | + * |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + public static function getGetter($anything, $key) |
|
65 | + { |
|
66 | + return '\\Jade\\Compiler::getPropertyFromAnything(' . |
|
67 | + static::addDollarIfNeeded($anything) . ', ' . |
|
68 | + var_export($key, true) . |
|
69 | + ')'; |
|
70 | + } |
|
71 | 71 | } |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | public static function addDollarIfNeeded($call) |
19 | 19 | { |
20 | 20 | if ($call === 'Inf') { |
21 | - throw new \InvalidArgumentException($call . ' cannot be read from PHP', 16); |
|
21 | + throw new \InvalidArgumentException($call.' cannot be read from PHP', 16); |
|
22 | 22 | } |
23 | 23 | if ($call === 'undefined') { |
24 | 24 | return 'null'; |
@@ -26,13 +26,13 @@ discard block |
||
26 | 26 | $firstChar = substr($call, 0, 1); |
27 | 27 | if ( |
28 | 28 | !in_array($firstChar, array('$', '\\')) && |
29 | - !preg_match('#^(?:' . CompilerConfig::VARNAME . '\\s*\\(|(?:null|false|true)(?![a-z]))#i', $call) && |
|
29 | + !preg_match('#^(?:'.CompilerConfig::VARNAME.'\\s*\\(|(?:null|false|true)(?![a-z]))#i', $call) && |
|
30 | 30 | ( |
31 | - preg_match('#^' . CompilerConfig::VARNAME . '#', $call) || |
|
31 | + preg_match('#^'.CompilerConfig::VARNAME.'#', $call) || |
|
32 | 32 | $firstChar === '_' |
33 | 33 | ) |
34 | 34 | ) { |
35 | - $call = '$' . $call; |
|
35 | + $call = '$'.$call; |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | return $call; |
@@ -63,9 +63,9 @@ discard block |
||
63 | 63 | */ |
64 | 64 | public static function getGetter($anything, $key) |
65 | 65 | { |
66 | - return '\\Jade\\Compiler::getPropertyFromAnything(' . |
|
67 | - static::addDollarIfNeeded($anything) . ', ' . |
|
68 | - var_export($key, true) . |
|
66 | + return '\\Jade\\Compiler::getPropertyFromAnything('. |
|
67 | + static::addDollarIfNeeded($anything).', '. |
|
68 | + var_export($key, true). |
|
69 | 69 | ')'; |
70 | 70 | } |
71 | 71 | } |
@@ -73,7 +73,6 @@ |
||
73 | 73 | /** |
74 | 74 | * Get property from object. |
75 | 75 | * |
76 | - * @param object $object source object |
|
77 | 76 | * @param mixed $key key to retrive from the object or the array |
78 | 77 | * |
79 | 78 | * @return mixed |
@@ -8,165 +8,165 @@ |
||
8 | 8 | */ |
9 | 9 | abstract class CompilerFacade extends ValuesCompiler |
10 | 10 | { |
11 | - protected static $mixinBlocks = array(); |
|
11 | + protected static $mixinBlocks = array(); |
|
12 | 12 | |
13 | - /** |
|
14 | - * Record a closure as a mixin block during execution jade template time. |
|
15 | - * |
|
16 | - * @param string mixin name |
|
17 | - * @param string mixin block treatment |
|
18 | - */ |
|
19 | - public static function recordMixinBlock($name, $func = null) |
|
20 | - { |
|
21 | - if (!isset(static::$mixinBlocks[$name])) { |
|
22 | - static::$mixinBlocks[$name] = array(); |
|
23 | - } |
|
24 | - array_push(static::$mixinBlocks[$name], $func); |
|
25 | - } |
|
13 | + /** |
|
14 | + * Record a closure as a mixin block during execution jade template time. |
|
15 | + * |
|
16 | + * @param string mixin name |
|
17 | + * @param string mixin block treatment |
|
18 | + */ |
|
19 | + public static function recordMixinBlock($name, $func = null) |
|
20 | + { |
|
21 | + if (!isset(static::$mixinBlocks[$name])) { |
|
22 | + static::$mixinBlocks[$name] = array(); |
|
23 | + } |
|
24 | + array_push(static::$mixinBlocks[$name], $func); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Record a closure as a mixin block during execution jade template time. |
|
29 | - * |
|
30 | - * @param string mixin name |
|
31 | - * @param string mixin block treatment |
|
32 | - */ |
|
33 | - public static function callMixinBlock($name, $attributes = array()) |
|
34 | - { |
|
35 | - if (isset(static::$mixinBlocks[$name]) && is_array($mixinBlocks = static::$mixinBlocks[$name])) { |
|
36 | - $func = end($mixinBlocks); |
|
37 | - if (is_callable($func)) { |
|
38 | - $func($attributes); |
|
39 | - } |
|
40 | - } |
|
41 | - } |
|
27 | + /** |
|
28 | + * Record a closure as a mixin block during execution jade template time. |
|
29 | + * |
|
30 | + * @param string mixin name |
|
31 | + * @param string mixin block treatment |
|
32 | + */ |
|
33 | + public static function callMixinBlock($name, $attributes = array()) |
|
34 | + { |
|
35 | + if (isset(static::$mixinBlocks[$name]) && is_array($mixinBlocks = static::$mixinBlocks[$name])) { |
|
36 | + $func = end($mixinBlocks); |
|
37 | + if (is_callable($func)) { |
|
38 | + $func($attributes); |
|
39 | + } |
|
40 | + } |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Record a closure as a mixin block during execution jade template time |
|
45 | - * and propagate variables. |
|
46 | - * |
|
47 | - * @param string mixin name |
|
48 | - * @param &array variables handler propagated from parent scope |
|
49 | - * @param string mixin block treatment |
|
50 | - */ |
|
51 | - public static function callMixinBlockWithVars($name, &$varHandler, $attributes = array()) |
|
52 | - { |
|
53 | - if (isset(static::$mixinBlocks[$name]) && is_array($mixinBlocks = static::$mixinBlocks[$name])) { |
|
54 | - $func = end($mixinBlocks); |
|
55 | - if (is_callable($func)) { |
|
56 | - $func($varHandler, $attributes); |
|
57 | - } |
|
58 | - } |
|
59 | - } |
|
43 | + /** |
|
44 | + * Record a closure as a mixin block during execution jade template time |
|
45 | + * and propagate variables. |
|
46 | + * |
|
47 | + * @param string mixin name |
|
48 | + * @param &array variables handler propagated from parent scope |
|
49 | + * @param string mixin block treatment |
|
50 | + */ |
|
51 | + public static function callMixinBlockWithVars($name, &$varHandler, $attributes = array()) |
|
52 | + { |
|
53 | + if (isset(static::$mixinBlocks[$name]) && is_array($mixinBlocks = static::$mixinBlocks[$name])) { |
|
54 | + $func = end($mixinBlocks); |
|
55 | + if (is_callable($func)) { |
|
56 | + $func($varHandler, $attributes); |
|
57 | + } |
|
58 | + } |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * End of the record of the mixin block. |
|
63 | - * |
|
64 | - * @param string mixin name |
|
65 | - */ |
|
66 | - public static function terminateMixinBlock($name) |
|
67 | - { |
|
68 | - if (isset(static::$mixinBlocks[$name])) { |
|
69 | - array_pop(static::$mixinBlocks); |
|
70 | - } |
|
71 | - } |
|
61 | + /** |
|
62 | + * End of the record of the mixin block. |
|
63 | + * |
|
64 | + * @param string mixin name |
|
65 | + */ |
|
66 | + public static function terminateMixinBlock($name) |
|
67 | + { |
|
68 | + if (isset(static::$mixinBlocks[$name])) { |
|
69 | + array_pop(static::$mixinBlocks); |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Get property from object. |
|
75 | - * |
|
76 | - * @param object $object source object |
|
77 | - * @param mixed $key key to retrive from the object or the array |
|
78 | - * |
|
79 | - * @return mixed |
|
80 | - */ |
|
81 | - public static function getPropertyFromObject($anything, $key) |
|
82 | - { |
|
83 | - return isset($anything->$key) |
|
84 | - ? $anything->$key |
|
85 | - : (method_exists($anything, $method = 'get' . ucfirst($key)) |
|
86 | - ? $anything->$method() |
|
87 | - : (method_exists($anything, $key) |
|
88 | - ? array($anything, $key) |
|
89 | - : null |
|
90 | - ) |
|
91 | - ); |
|
92 | - } |
|
73 | + /** |
|
74 | + * Get property from object. |
|
75 | + * |
|
76 | + * @param object $object source object |
|
77 | + * @param mixed $key key to retrive from the object or the array |
|
78 | + * |
|
79 | + * @return mixed |
|
80 | + */ |
|
81 | + public static function getPropertyFromObject($anything, $key) |
|
82 | + { |
|
83 | + return isset($anything->$key) |
|
84 | + ? $anything->$key |
|
85 | + : (method_exists($anything, $method = 'get' . ucfirst($key)) |
|
86 | + ? $anything->$method() |
|
87 | + : (method_exists($anything, $key) |
|
88 | + ? array($anything, $key) |
|
89 | + : null |
|
90 | + ) |
|
91 | + ); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Get property from object or entry from array. |
|
96 | - * |
|
97 | - * @param object|array $anything source |
|
98 | - * @param mixed $key key to retrive from the object or the array |
|
99 | - * |
|
100 | - * @return mixed |
|
101 | - */ |
|
102 | - public static function getPropertyFromAnything($anything, $key) |
|
103 | - { |
|
104 | - return is_array($anything) |
|
105 | - ? (isset($anything[$key]) |
|
106 | - ? $anything[$key] |
|
107 | - : null |
|
108 | - ) : (is_object($anything) |
|
109 | - ? static::getPropertyFromObject($anything, $key) |
|
110 | - : null |
|
111 | - ); |
|
112 | - } |
|
94 | + /** |
|
95 | + * Get property from object or entry from array. |
|
96 | + * |
|
97 | + * @param object|array $anything source |
|
98 | + * @param mixed $key key to retrive from the object or the array |
|
99 | + * |
|
100 | + * @return mixed |
|
101 | + */ |
|
102 | + public static function getPropertyFromAnything($anything, $key) |
|
103 | + { |
|
104 | + return is_array($anything) |
|
105 | + ? (isset($anything[$key]) |
|
106 | + ? $anything[$key] |
|
107 | + : null |
|
108 | + ) : (is_object($anything) |
|
109 | + ? static::getPropertyFromObject($anything, $key) |
|
110 | + : null |
|
111 | + ); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Merge given attributes such as tag attributes with mixin attributes. |
|
116 | - * |
|
117 | - * @param array $attributes |
|
118 | - * @param array $mixinAttributes |
|
119 | - * |
|
120 | - * @return array |
|
121 | - */ |
|
122 | - public static function withMixinAttributes($attributes, $mixinAttributes) |
|
123 | - { |
|
124 | - foreach ($mixinAttributes as $attribute) { |
|
125 | - if ($attribute['name'] === 'class') { |
|
126 | - $value = static::joinAny($attribute['value']); |
|
127 | - $attributes['class'] = empty($attributes['class']) |
|
128 | - ? $value |
|
129 | - : static::joinAny($attributes['class']) . ' ' . $value; |
|
130 | - } |
|
131 | - } |
|
132 | - if (isset($attributes['class'])) { |
|
133 | - $attributes['class'] = implode(' ', array_unique(explode(' ', $attributes['class']))); |
|
134 | - } |
|
114 | + /** |
|
115 | + * Merge given attributes such as tag attributes with mixin attributes. |
|
116 | + * |
|
117 | + * @param array $attributes |
|
118 | + * @param array $mixinAttributes |
|
119 | + * |
|
120 | + * @return array |
|
121 | + */ |
|
122 | + public static function withMixinAttributes($attributes, $mixinAttributes) |
|
123 | + { |
|
124 | + foreach ($mixinAttributes as $attribute) { |
|
125 | + if ($attribute['name'] === 'class') { |
|
126 | + $value = static::joinAny($attribute['value']); |
|
127 | + $attributes['class'] = empty($attributes['class']) |
|
128 | + ? $value |
|
129 | + : static::joinAny($attributes['class']) . ' ' . $value; |
|
130 | + } |
|
131 | + } |
|
132 | + if (isset($attributes['class'])) { |
|
133 | + $attributes['class'] = implode(' ', array_unique(explode(' ', $attributes['class']))); |
|
134 | + } |
|
135 | 135 | |
136 | - return $attributes; |
|
137 | - } |
|
136 | + return $attributes; |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * Display a list of attributes with the given quote character in HTML. |
|
141 | - * |
|
142 | - * @param array $attributes |
|
143 | - * @param string $quote |
|
144 | - */ |
|
145 | - public static function displayAttributes($attributes, $quote, $terse) |
|
146 | - { |
|
147 | - if (is_array($attributes) || $attributes instanceof Traversable) { |
|
148 | - foreach ($attributes as $key => $value) { |
|
149 | - if ($key !== 'class' && $value !== false && $value !== 'null') { |
|
150 | - if ($value === true) { |
|
151 | - echo ' ' . $key . ($terse ? '' : '=' . $quote . $key . $quote); |
|
152 | - continue; |
|
153 | - } |
|
154 | - echo ' ' . $key . '=' . $quote . htmlspecialchars($value) . $quote; |
|
155 | - } |
|
156 | - } |
|
157 | - } |
|
158 | - } |
|
139 | + /** |
|
140 | + * Display a list of attributes with the given quote character in HTML. |
|
141 | + * |
|
142 | + * @param array $attributes |
|
143 | + * @param string $quote |
|
144 | + */ |
|
145 | + public static function displayAttributes($attributes, $quote, $terse) |
|
146 | + { |
|
147 | + if (is_array($attributes) || $attributes instanceof Traversable) { |
|
148 | + foreach ($attributes as $key => $value) { |
|
149 | + if ($key !== 'class' && $value !== false && $value !== 'null') { |
|
150 | + if ($value === true) { |
|
151 | + echo ' ' . $key . ($terse ? '' : '=' . $quote . $key . $quote); |
|
152 | + continue; |
|
153 | + } |
|
154 | + echo ' ' . $key . '=' . $quote . htmlspecialchars($value) . $quote; |
|
155 | + } |
|
156 | + } |
|
157 | + } |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * Return true if the given value can be display |
|
162 | - * (null or false should not be displayed in the output HTML). |
|
163 | - * |
|
164 | - * @param $value |
|
165 | - * |
|
166 | - * @return bool |
|
167 | - */ |
|
168 | - public static function isDisplayable($value) |
|
169 | - { |
|
170 | - return !is_null($value) && $value !== false; |
|
171 | - } |
|
160 | + /** |
|
161 | + * Return true if the given value can be display |
|
162 | + * (null or false should not be displayed in the output HTML). |
|
163 | + * |
|
164 | + * @param $value |
|
165 | + * |
|
166 | + * @return bool |
|
167 | + */ |
|
168 | + public static function isDisplayable($value) |
|
169 | + { |
|
170 | + return !is_null($value) && $value !== false; |
|
171 | + } |
|
172 | 172 | } |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | { |
83 | 83 | return isset($anything->$key) |
84 | 84 | ? $anything->$key |
85 | - : (method_exists($anything, $method = 'get' . ucfirst($key)) |
|
85 | + : (method_exists($anything, $method = 'get'.ucfirst($key)) |
|
86 | 86 | ? $anything->$method() |
87 | 87 | : (method_exists($anything, $key) |
88 | 88 | ? array($anything, $key) |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | $value = static::joinAny($attribute['value']); |
127 | 127 | $attributes['class'] = empty($attributes['class']) |
128 | 128 | ? $value |
129 | - : static::joinAny($attributes['class']) . ' ' . $value; |
|
129 | + : static::joinAny($attributes['class']).' '.$value; |
|
130 | 130 | } |
131 | 131 | } |
132 | 132 | if (isset($attributes['class'])) { |
@@ -148,10 +148,10 @@ discard block |
||
148 | 148 | foreach ($attributes as $key => $value) { |
149 | 149 | if ($key !== 'class' && $value !== false && $value !== 'null') { |
150 | 150 | if ($value === true) { |
151 | - echo ' ' . $key . ($terse ? '' : '=' . $quote . $key . $quote); |
|
151 | + echo ' '.$key.($terse ? '' : '='.$quote.$key.$quote); |
|
152 | 152 | continue; |
153 | 153 | } |
154 | - echo ' ' . $key . '=' . $quote . htmlspecialchars($value) . $quote; |
|
154 | + echo ' '.$key.'='.$quote.htmlspecialchars($value).$quote; |
|
155 | 155 | } |
156 | 156 | } |
157 | 157 | } |
@@ -179,7 +179,7 @@ |
||
179 | 179 | * |
180 | 180 | * @param array $value |
181 | 181 | * |
182 | - * @return string|mixed |
|
182 | + * @return string |
|
183 | 183 | */ |
184 | 184 | protected static function joinAny($value) |
185 | 185 | { |
@@ -8,183 +8,183 @@ |
||
8 | 8 | */ |
9 | 9 | abstract class CompilerUtils extends Indenter |
10 | 10 | { |
11 | - /** |
|
12 | - * Prepend "$" to the given input if it's a varname. |
|
13 | - * |
|
14 | - * @param string $call |
|
15 | - * |
|
16 | - * @throws \InvalidArgumentException |
|
17 | - * |
|
18 | - * @return string |
|
19 | - */ |
|
20 | - protected static function addDollarIfNeeded($call) |
|
21 | - { |
|
22 | - return CommonUtils::addDollarIfNeeded($call); |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * Escape value depanding on the current quote. |
|
27 | - * |
|
28 | - * @param string $val input value |
|
29 | - * |
|
30 | - * @return string |
|
31 | - */ |
|
32 | - protected function escapeValue($val) |
|
33 | - { |
|
34 | - return static::getEscapedValue($val, $this->quote); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Return PHP code to translate dot to object/array getter. |
|
39 | - * |
|
40 | - * @example foo.bar return $foo->bar (if foo is an object), or $foo["bar"] if it's an array. |
|
41 | - * |
|
42 | - * @param array $match regex match |
|
43 | - * |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - protected static function convertVarPathCallback($match) |
|
47 | - { |
|
48 | - if (empty($match[1])) { |
|
49 | - return $match[0]; |
|
50 | - } |
|
51 | - |
|
52 | - $var = ($match[0] === ',' ? ',' : '') . $match[1]; |
|
53 | - foreach (explode('.', substr($match[2], 1)) as $name) { |
|
54 | - if (!empty($name)) { |
|
55 | - $var = CommonUtils::getGetter($var, $name, false); |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - return $var; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Replace var paths in a string. |
|
64 | - * |
|
65 | - * @param string $arg |
|
66 | - * @param string $regexp |
|
67 | - * |
|
68 | - * @return string |
|
69 | - */ |
|
70 | - protected static function convertVarPath($arg, $regexp = '/^%s|,%s/') |
|
71 | - { |
|
72 | - $pattern = '\s*(\\${0,2}' . static::VARNAME . ')((\.' . static::VARNAME . ')*)'; |
|
73 | - |
|
74 | - return preg_replace_callback( |
|
75 | - str_replace('%s', $pattern, $regexp), |
|
76 | - array(get_class(), 'convertVarPathCallback'), |
|
77 | - $arg |
|
78 | - ); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Concat " = null" to initializations to simulate the JS "var foo;". |
|
83 | - * |
|
84 | - * @param &string $arg reference of an argument containing an expression |
|
85 | - * |
|
86 | - * @throws \InvalidArgumentException |
|
87 | - */ |
|
88 | - protected static function initArgToNull(&$arg) |
|
89 | - { |
|
90 | - $arg = static::addDollarIfNeeded(trim($arg)); |
|
91 | - if (strpos($arg, '=') === false) { |
|
92 | - $arg .= ' = null'; |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Parse a value from its quoted string (or JSON) representation. |
|
98 | - * |
|
99 | - * @param string $value |
|
100 | - * |
|
101 | - * @return mixed |
|
102 | - */ |
|
103 | - protected static function parseValue($value) |
|
104 | - { |
|
105 | - return json_decode(preg_replace("/'([^']*?)'/", '"$1"', $value)); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Decode a value (parse it except if it's null). |
|
110 | - * |
|
111 | - * @param string $value |
|
112 | - * |
|
113 | - * @return mixed |
|
114 | - */ |
|
115 | - protected static function decodeValue($value) |
|
116 | - { |
|
117 | - $parsedValue = static::parseValue($value); |
|
118 | - |
|
119 | - return is_null($parsedValue) ? $value : $parsedValue; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Decode each attribute in the given list. |
|
124 | - * |
|
125 | - * @param array $attributes |
|
126 | - * |
|
127 | - * @return array |
|
128 | - */ |
|
129 | - protected static function decodeAttributes($attributes) |
|
130 | - { |
|
131 | - foreach ($attributes as &$attribute) { |
|
132 | - if (is_array($attribute)) { |
|
133 | - $attribute['value'] = is_bool($attribute['value']) ? $attribute['value'] : static::decodeValue($attribute['value']); |
|
134 | - continue; |
|
135 | - } |
|
136 | - |
|
137 | - $attribute = static::decodeValue($attribute); |
|
138 | - } |
|
139 | - |
|
140 | - return $attributes; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Get filter by name. |
|
145 | - * |
|
146 | - * @param string $name |
|
147 | - * |
|
148 | - * @return callable |
|
149 | - */ |
|
150 | - protected function getFilter($name) |
|
151 | - { |
|
152 | - $helper = new FilterHelper($this->filters, $this->filterAutoLoad); |
|
153 | - |
|
154 | - return $helper->getValidFilter($name); |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Return PHP code wich wrap the given value and escape it if $escaped is true. |
|
159 | - * |
|
160 | - * @param bool $escaped need to be escaped |
|
161 | - * @param mixed $value to be escaped if $escaped is true |
|
162 | - * |
|
163 | - * @return callable |
|
164 | - */ |
|
165 | - protected function escapeIfNeeded($escaped, $value) |
|
166 | - { |
|
167 | - $value = rtrim($value, ';'); |
|
168 | - |
|
169 | - if ($escaped) { |
|
170 | - return $this->createCode(static::ESCAPED, $value, var_export($this->quote, true)); |
|
171 | - } |
|
172 | - |
|
173 | - return $this->createCode(static::UNESCAPED, $value); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Join with space if the value is an array, else return the input value |
|
178 | - * with no changes. |
|
179 | - * |
|
180 | - * @param array $value |
|
181 | - * |
|
182 | - * @return string|mixed |
|
183 | - */ |
|
184 | - protected static function joinAny($value) |
|
185 | - { |
|
186 | - return is_array($value) |
|
187 | - ? implode(' ', $value) |
|
188 | - : $value; |
|
189 | - } |
|
11 | + /** |
|
12 | + * Prepend "$" to the given input if it's a varname. |
|
13 | + * |
|
14 | + * @param string $call |
|
15 | + * |
|
16 | + * @throws \InvalidArgumentException |
|
17 | + * |
|
18 | + * @return string |
|
19 | + */ |
|
20 | + protected static function addDollarIfNeeded($call) |
|
21 | + { |
|
22 | + return CommonUtils::addDollarIfNeeded($call); |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * Escape value depanding on the current quote. |
|
27 | + * |
|
28 | + * @param string $val input value |
|
29 | + * |
|
30 | + * @return string |
|
31 | + */ |
|
32 | + protected function escapeValue($val) |
|
33 | + { |
|
34 | + return static::getEscapedValue($val, $this->quote); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Return PHP code to translate dot to object/array getter. |
|
39 | + * |
|
40 | + * @example foo.bar return $foo->bar (if foo is an object), or $foo["bar"] if it's an array. |
|
41 | + * |
|
42 | + * @param array $match regex match |
|
43 | + * |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + protected static function convertVarPathCallback($match) |
|
47 | + { |
|
48 | + if (empty($match[1])) { |
|
49 | + return $match[0]; |
|
50 | + } |
|
51 | + |
|
52 | + $var = ($match[0] === ',' ? ',' : '') . $match[1]; |
|
53 | + foreach (explode('.', substr($match[2], 1)) as $name) { |
|
54 | + if (!empty($name)) { |
|
55 | + $var = CommonUtils::getGetter($var, $name, false); |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + return $var; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Replace var paths in a string. |
|
64 | + * |
|
65 | + * @param string $arg |
|
66 | + * @param string $regexp |
|
67 | + * |
|
68 | + * @return string |
|
69 | + */ |
|
70 | + protected static function convertVarPath($arg, $regexp = '/^%s|,%s/') |
|
71 | + { |
|
72 | + $pattern = '\s*(\\${0,2}' . static::VARNAME . ')((\.' . static::VARNAME . ')*)'; |
|
73 | + |
|
74 | + return preg_replace_callback( |
|
75 | + str_replace('%s', $pattern, $regexp), |
|
76 | + array(get_class(), 'convertVarPathCallback'), |
|
77 | + $arg |
|
78 | + ); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Concat " = null" to initializations to simulate the JS "var foo;". |
|
83 | + * |
|
84 | + * @param &string $arg reference of an argument containing an expression |
|
85 | + * |
|
86 | + * @throws \InvalidArgumentException |
|
87 | + */ |
|
88 | + protected static function initArgToNull(&$arg) |
|
89 | + { |
|
90 | + $arg = static::addDollarIfNeeded(trim($arg)); |
|
91 | + if (strpos($arg, '=') === false) { |
|
92 | + $arg .= ' = null'; |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Parse a value from its quoted string (or JSON) representation. |
|
98 | + * |
|
99 | + * @param string $value |
|
100 | + * |
|
101 | + * @return mixed |
|
102 | + */ |
|
103 | + protected static function parseValue($value) |
|
104 | + { |
|
105 | + return json_decode(preg_replace("/'([^']*?)'/", '"$1"', $value)); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Decode a value (parse it except if it's null). |
|
110 | + * |
|
111 | + * @param string $value |
|
112 | + * |
|
113 | + * @return mixed |
|
114 | + */ |
|
115 | + protected static function decodeValue($value) |
|
116 | + { |
|
117 | + $parsedValue = static::parseValue($value); |
|
118 | + |
|
119 | + return is_null($parsedValue) ? $value : $parsedValue; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Decode each attribute in the given list. |
|
124 | + * |
|
125 | + * @param array $attributes |
|
126 | + * |
|
127 | + * @return array |
|
128 | + */ |
|
129 | + protected static function decodeAttributes($attributes) |
|
130 | + { |
|
131 | + foreach ($attributes as &$attribute) { |
|
132 | + if (is_array($attribute)) { |
|
133 | + $attribute['value'] = is_bool($attribute['value']) ? $attribute['value'] : static::decodeValue($attribute['value']); |
|
134 | + continue; |
|
135 | + } |
|
136 | + |
|
137 | + $attribute = static::decodeValue($attribute); |
|
138 | + } |
|
139 | + |
|
140 | + return $attributes; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Get filter by name. |
|
145 | + * |
|
146 | + * @param string $name |
|
147 | + * |
|
148 | + * @return callable |
|
149 | + */ |
|
150 | + protected function getFilter($name) |
|
151 | + { |
|
152 | + $helper = new FilterHelper($this->filters, $this->filterAutoLoad); |
|
153 | + |
|
154 | + return $helper->getValidFilter($name); |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Return PHP code wich wrap the given value and escape it if $escaped is true. |
|
159 | + * |
|
160 | + * @param bool $escaped need to be escaped |
|
161 | + * @param mixed $value to be escaped if $escaped is true |
|
162 | + * |
|
163 | + * @return callable |
|
164 | + */ |
|
165 | + protected function escapeIfNeeded($escaped, $value) |
|
166 | + { |
|
167 | + $value = rtrim($value, ';'); |
|
168 | + |
|
169 | + if ($escaped) { |
|
170 | + return $this->createCode(static::ESCAPED, $value, var_export($this->quote, true)); |
|
171 | + } |
|
172 | + |
|
173 | + return $this->createCode(static::UNESCAPED, $value); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Join with space if the value is an array, else return the input value |
|
178 | + * with no changes. |
|
179 | + * |
|
180 | + * @param array $value |
|
181 | + * |
|
182 | + * @return string|mixed |
|
183 | + */ |
|
184 | + protected static function joinAny($value) |
|
185 | + { |
|
186 | + return is_array($value) |
|
187 | + ? implode(' ', $value) |
|
188 | + : $value; |
|
189 | + } |
|
190 | 190 | } |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | return $match[0]; |
50 | 50 | } |
51 | 51 | |
52 | - $var = ($match[0] === ',' ? ',' : '') . $match[1]; |
|
52 | + $var = ($match[0] === ',' ? ',' : '').$match[1]; |
|
53 | 53 | foreach (explode('.', substr($match[2], 1)) as $name) { |
54 | 54 | if (!empty($name)) { |
55 | 55 | $var = CommonUtils::getGetter($var, $name, false); |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | */ |
70 | 70 | protected static function convertVarPath($arg, $regexp = '/^%s|,%s/') |
71 | 71 | { |
72 | - $pattern = '\s*(\\${0,2}' . static::VARNAME . ')((\.' . static::VARNAME . ')*)'; |
|
72 | + $pattern = '\s*(\\${0,2}'.static::VARNAME.')((\.'.static::VARNAME.')*)'; |
|
73 | 73 | |
74 | 74 | return preg_replace_callback( |
75 | 75 | str_replace('%s', $pattern, $regexp), |