@@ -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), |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | abstract class KeywordsCompiler extends AttributesCompiler |
12 | 12 | { |
13 | 13 | /** |
14 | - * @param Nodes\CaseNode $node |
|
14 | + * @param CaseNode $node |
|
15 | 15 | */ |
16 | 16 | protected function visitCasenode(CaseNode $node) |
17 | 17 | { |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | - * @param Nodes\When $node |
|
48 | + * @param When $node |
|
49 | 49 | */ |
50 | 50 | protected function visitWhen(When $node) |
51 | 51 | { |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
78 | - * @param Nodes\Filter $node |
|
78 | + * @param Filter $node |
|
79 | 79 | * |
80 | 80 | * @throws \InvalidArgumentException |
81 | 81 | */ |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
97 | - * @param Nodes\Each $node |
|
97 | + * @param Each $node |
|
98 | 98 | */ |
99 | 99 | protected function visitEach(Each $node) |
100 | 100 | { |
@@ -138,6 +138,9 @@ discard block |
||
138 | 138 | } |
139 | 139 | } |
140 | 140 | |
141 | + /** |
|
142 | + * @param \Jade\Nodes\Block $block |
|
143 | + */ |
|
141 | 144 | protected function bufferCustomKeyword($data, $block) |
142 | 145 | { |
143 | 146 | if (isset($data['begin'])) { |
@@ -156,7 +159,7 @@ discard block |
||
156 | 159 | } |
157 | 160 | |
158 | 161 | /** |
159 | - * @param Nodes\CustomKeyword $node |
|
162 | + * @param CustomKeyword $node |
|
160 | 163 | */ |
161 | 164 | protected function visitCustomKeyword(CustomKeyword $node) |
162 | 165 | { |
@@ -10,180 +10,180 @@ |
||
10 | 10 | |
11 | 11 | abstract class KeywordsCompiler extends AttributesCompiler |
12 | 12 | { |
13 | - /** |
|
14 | - * @param Nodes\CaseNode $node |
|
15 | - */ |
|
16 | - protected function visitCasenode(CaseNode $node) |
|
17 | - { |
|
18 | - $this->switchNode = $node; |
|
19 | - $this->visit($node->block); |
|
20 | - |
|
21 | - if (!isset($this->switchNode)) { |
|
22 | - unset($this->switchNode); |
|
23 | - $this->indents--; |
|
24 | - |
|
25 | - $code = $this->createCode('}'); |
|
26 | - $this->buffer($code); |
|
27 | - } |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * @param string $expression |
|
32 | - * @param &array $arguments |
|
33 | - * |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - protected function visitCase($expression, &$arguments) |
|
37 | - { |
|
38 | - if ('default' === $expression) { |
|
39 | - return 'default:'; |
|
40 | - } |
|
41 | - |
|
42 | - $arguments[] = $expression; |
|
43 | - |
|
44 | - return 'case %s:'; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * @param Nodes\When $node |
|
49 | - */ |
|
50 | - protected function visitWhen(When $node) |
|
51 | - { |
|
52 | - $code = ''; |
|
53 | - $arguments = array(); |
|
54 | - |
|
55 | - if (isset($this->switchNode)) { |
|
56 | - $code .= 'switch (%s) {'; |
|
57 | - $arguments[] = $this->switchNode->expr; |
|
58 | - unset($this->switchNode); |
|
59 | - |
|
60 | - $this->indents++; |
|
61 | - } |
|
62 | - |
|
63 | - $code .= $this->visitCase($node->expr, $arguments); |
|
64 | - |
|
65 | - array_unshift($arguments, $code); |
|
66 | - |
|
67 | - $code = call_user_func_array(array($this, 'createCode'), $arguments); |
|
68 | - |
|
69 | - $this->buffer($code); |
|
70 | - |
|
71 | - $this->visit($node->block); |
|
72 | - |
|
73 | - $code = $this->createCode('break;'); |
|
74 | - $this->buffer($code . $this->newline()); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @param Nodes\Filter $node |
|
79 | - * |
|
80 | - * @throws \InvalidArgumentException |
|
81 | - */ |
|
82 | - protected function visitFilter(Filter $node) |
|
83 | - { |
|
84 | - $filter = $this->getFilter($node->name); |
|
85 | - |
|
86 | - // Filters can be either a iFilter implementation, nor a callable |
|
87 | - if (is_string($filter) && class_exists($filter)) { |
|
88 | - $filter = new $filter(); |
|
89 | - } |
|
90 | - if (!is_callable($filter)) { |
|
91 | - throw new \InvalidArgumentException($node->name . ': Filter must be callable', 18); |
|
92 | - } |
|
93 | - $this->buffer($filter($node, $this)); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param Nodes\Each $node |
|
98 | - */ |
|
99 | - protected function visitEach(Each $node) |
|
100 | - { |
|
101 | - //if (is_numeric($node->obj)) { |
|
102 | - //if (is_string($node->obj)) { |
|
103 | - //$serialized = serialize($node->obj); |
|
104 | - if (isset($node->alternative)) { |
|
105 | - $this->buffer($this->createCode( |
|
106 | - 'if (isset(%s) && %s) {', |
|
107 | - $node->obj, $node->obj |
|
108 | - )); |
|
109 | - $this->indents++; |
|
110 | - } |
|
111 | - |
|
112 | - $this->buffer(isset($node->key) && strlen($node->key) > 0 |
|
113 | - ? $this->createCode( |
|
114 | - 'foreach (%s as %s => %s) {', |
|
115 | - $node->obj, $node->key, $node->value |
|
116 | - ) |
|
117 | - : $this->createCode( |
|
118 | - 'foreach (%s as %s) {', |
|
119 | - $node->obj, $node->value |
|
120 | - ) |
|
121 | - ); |
|
122 | - |
|
123 | - $this->indents++; |
|
124 | - $this->visit($node->block); |
|
125 | - $this->indents--; |
|
126 | - |
|
127 | - $this->buffer($this->createCode('}')); |
|
128 | - |
|
129 | - if (isset($node->alternative)) { |
|
130 | - $this->indents--; |
|
131 | - $this->buffer($this->createCode('} else {')); |
|
132 | - $this->indents++; |
|
133 | - |
|
134 | - $this->visit($node->alternative); |
|
135 | - $this->indents--; |
|
136 | - |
|
137 | - $this->buffer($this->createCode('}')); |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - protected function bufferCustomKeyword($data, $block) |
|
142 | - { |
|
143 | - if (isset($data['begin'])) { |
|
144 | - $this->buffer($data['begin']); |
|
145 | - } |
|
146 | - |
|
147 | - if ($block) { |
|
148 | - $this->indents++; |
|
149 | - $this->visit($block); |
|
150 | - $this->indents--; |
|
151 | - } |
|
152 | - |
|
153 | - if (isset($data['end'])) { |
|
154 | - $this->buffer($data['end']); |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @param Nodes\CustomKeyword $node |
|
160 | - */ |
|
161 | - protected function visitCustomKeyword(CustomKeyword $node) |
|
162 | - { |
|
163 | - $action = $this->options['customKeywords'][$node->keyWord]; |
|
164 | - |
|
165 | - $data = $action($node->args, $node->block, $node->keyWord); |
|
166 | - |
|
167 | - if (is_string($data)) { |
|
168 | - $data = array( |
|
169 | - 'begin' => $data, |
|
170 | - ); |
|
171 | - } |
|
172 | - |
|
173 | - if (!is_array($data) && !($data instanceof \ArrayAccess)) { |
|
174 | - throw new \ErrorException("The keyword {$node->keyWord} returned an invalid value type, string or array was expected.", 33); |
|
175 | - } |
|
176 | - |
|
177 | - foreach (array('begin', 'end') as $key) { |
|
178 | - $data[$key] = (isset($data[$key . 'Php']) |
|
179 | - ? $this->createCode($data[$key . 'Php']) |
|
180 | - : '' |
|
181 | - ) . (isset($data[$key]) |
|
182 | - ? $data[$key] |
|
183 | - : '' |
|
184 | - ); |
|
185 | - } |
|
186 | - |
|
187 | - $this->bufferCustomKeyword($data, $node->block); |
|
188 | - } |
|
13 | + /** |
|
14 | + * @param Nodes\CaseNode $node |
|
15 | + */ |
|
16 | + protected function visitCasenode(CaseNode $node) |
|
17 | + { |
|
18 | + $this->switchNode = $node; |
|
19 | + $this->visit($node->block); |
|
20 | + |
|
21 | + if (!isset($this->switchNode)) { |
|
22 | + unset($this->switchNode); |
|
23 | + $this->indents--; |
|
24 | + |
|
25 | + $code = $this->createCode('}'); |
|
26 | + $this->buffer($code); |
|
27 | + } |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * @param string $expression |
|
32 | + * @param &array $arguments |
|
33 | + * |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + protected function visitCase($expression, &$arguments) |
|
37 | + { |
|
38 | + if ('default' === $expression) { |
|
39 | + return 'default:'; |
|
40 | + } |
|
41 | + |
|
42 | + $arguments[] = $expression; |
|
43 | + |
|
44 | + return 'case %s:'; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * @param Nodes\When $node |
|
49 | + */ |
|
50 | + protected function visitWhen(When $node) |
|
51 | + { |
|
52 | + $code = ''; |
|
53 | + $arguments = array(); |
|
54 | + |
|
55 | + if (isset($this->switchNode)) { |
|
56 | + $code .= 'switch (%s) {'; |
|
57 | + $arguments[] = $this->switchNode->expr; |
|
58 | + unset($this->switchNode); |
|
59 | + |
|
60 | + $this->indents++; |
|
61 | + } |
|
62 | + |
|
63 | + $code .= $this->visitCase($node->expr, $arguments); |
|
64 | + |
|
65 | + array_unshift($arguments, $code); |
|
66 | + |
|
67 | + $code = call_user_func_array(array($this, 'createCode'), $arguments); |
|
68 | + |
|
69 | + $this->buffer($code); |
|
70 | + |
|
71 | + $this->visit($node->block); |
|
72 | + |
|
73 | + $code = $this->createCode('break;'); |
|
74 | + $this->buffer($code . $this->newline()); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @param Nodes\Filter $node |
|
79 | + * |
|
80 | + * @throws \InvalidArgumentException |
|
81 | + */ |
|
82 | + protected function visitFilter(Filter $node) |
|
83 | + { |
|
84 | + $filter = $this->getFilter($node->name); |
|
85 | + |
|
86 | + // Filters can be either a iFilter implementation, nor a callable |
|
87 | + if (is_string($filter) && class_exists($filter)) { |
|
88 | + $filter = new $filter(); |
|
89 | + } |
|
90 | + if (!is_callable($filter)) { |
|
91 | + throw new \InvalidArgumentException($node->name . ': Filter must be callable', 18); |
|
92 | + } |
|
93 | + $this->buffer($filter($node, $this)); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param Nodes\Each $node |
|
98 | + */ |
|
99 | + protected function visitEach(Each $node) |
|
100 | + { |
|
101 | + //if (is_numeric($node->obj)) { |
|
102 | + //if (is_string($node->obj)) { |
|
103 | + //$serialized = serialize($node->obj); |
|
104 | + if (isset($node->alternative)) { |
|
105 | + $this->buffer($this->createCode( |
|
106 | + 'if (isset(%s) && %s) {', |
|
107 | + $node->obj, $node->obj |
|
108 | + )); |
|
109 | + $this->indents++; |
|
110 | + } |
|
111 | + |
|
112 | + $this->buffer(isset($node->key) && strlen($node->key) > 0 |
|
113 | + ? $this->createCode( |
|
114 | + 'foreach (%s as %s => %s) {', |
|
115 | + $node->obj, $node->key, $node->value |
|
116 | + ) |
|
117 | + : $this->createCode( |
|
118 | + 'foreach (%s as %s) {', |
|
119 | + $node->obj, $node->value |
|
120 | + ) |
|
121 | + ); |
|
122 | + |
|
123 | + $this->indents++; |
|
124 | + $this->visit($node->block); |
|
125 | + $this->indents--; |
|
126 | + |
|
127 | + $this->buffer($this->createCode('}')); |
|
128 | + |
|
129 | + if (isset($node->alternative)) { |
|
130 | + $this->indents--; |
|
131 | + $this->buffer($this->createCode('} else {')); |
|
132 | + $this->indents++; |
|
133 | + |
|
134 | + $this->visit($node->alternative); |
|
135 | + $this->indents--; |
|
136 | + |
|
137 | + $this->buffer($this->createCode('}')); |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + protected function bufferCustomKeyword($data, $block) |
|
142 | + { |
|
143 | + if (isset($data['begin'])) { |
|
144 | + $this->buffer($data['begin']); |
|
145 | + } |
|
146 | + |
|
147 | + if ($block) { |
|
148 | + $this->indents++; |
|
149 | + $this->visit($block); |
|
150 | + $this->indents--; |
|
151 | + } |
|
152 | + |
|
153 | + if (isset($data['end'])) { |
|
154 | + $this->buffer($data['end']); |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @param Nodes\CustomKeyword $node |
|
160 | + */ |
|
161 | + protected function visitCustomKeyword(CustomKeyword $node) |
|
162 | + { |
|
163 | + $action = $this->options['customKeywords'][$node->keyWord]; |
|
164 | + |
|
165 | + $data = $action($node->args, $node->block, $node->keyWord); |
|
166 | + |
|
167 | + if (is_string($data)) { |
|
168 | + $data = array( |
|
169 | + 'begin' => $data, |
|
170 | + ); |
|
171 | + } |
|
172 | + |
|
173 | + if (!is_array($data) && !($data instanceof \ArrayAccess)) { |
|
174 | + throw new \ErrorException("The keyword {$node->keyWord} returned an invalid value type, string or array was expected.", 33); |
|
175 | + } |
|
176 | + |
|
177 | + foreach (array('begin', 'end') as $key) { |
|
178 | + $data[$key] = (isset($data[$key . 'Php']) |
|
179 | + ? $this->createCode($data[$key . 'Php']) |
|
180 | + : '' |
|
181 | + ) . (isset($data[$key]) |
|
182 | + ? $data[$key] |
|
183 | + : '' |
|
184 | + ); |
|
185 | + } |
|
186 | + |
|
187 | + $this->bufferCustomKeyword($data, $node->block); |
|
188 | + } |
|
189 | 189 | } |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | $this->visit($node->block); |
72 | 72 | |
73 | 73 | $code = $this->createCode('break;'); |
74 | - $this->buffer($code . $this->newline()); |
|
74 | + $this->buffer($code.$this->newline()); |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | $filter = new $filter(); |
89 | 89 | } |
90 | 90 | if (!is_callable($filter)) { |
91 | - throw new \InvalidArgumentException($node->name . ': Filter must be callable', 18); |
|
91 | + throw new \InvalidArgumentException($node->name.': Filter must be callable', 18); |
|
92 | 92 | } |
93 | 93 | $this->buffer($filter($node, $this)); |
94 | 94 | } |
@@ -175,10 +175,10 @@ discard block |
||
175 | 175 | } |
176 | 176 | |
177 | 177 | foreach (array('begin', 'end') as $key) { |
178 | - $data[$key] = (isset($data[$key . 'Php']) |
|
179 | - ? $this->createCode($data[$key . 'Php']) |
|
178 | + $data[$key] = (isset($data[$key.'Php']) |
|
179 | + ? $this->createCode($data[$key.'Php']) |
|
180 | 180 | : '' |
181 | - ) . (isset($data[$key]) |
|
181 | + ).(isset($data[$key]) |
|
182 | 182 | ? $data[$key] |
183 | 183 | : '' |
184 | 184 | ); |
@@ -15,6 +15,9 @@ discard block |
||
15 | 15 | } |
16 | 16 | } |
17 | 17 | |
18 | + /** |
|
19 | + * @param boolean $containsOnlyArrays |
|
20 | + */ |
|
18 | 21 | protected function parseMixinArguments(&$arguments, &$containsOnlyArrays, &$defaultAttributes) |
19 | 22 | { |
20 | 23 | $newArrayKey = null; |
@@ -72,6 +75,9 @@ discard block |
||
72 | 75 | return $this->parseMixinStringAttribute($data); |
73 | 76 | } |
74 | 77 | |
78 | + /** |
|
79 | + * @param string $defaultAttributes |
|
80 | + */ |
|
75 | 81 | protected function parseMixinAttributes($attributes, $defaultAttributes, $mixinAttributes) |
76 | 82 | { |
77 | 83 | if (!count($attributes)) { |
@@ -112,6 +118,9 @@ discard block |
||
112 | 118 | ); |
113 | 119 | } |
114 | 120 | |
121 | + /** |
|
122 | + * @param string $code |
|
123 | + */ |
|
115 | 124 | protected function renderClosureClosing($code, $arguments = array()) |
116 | 125 | { |
117 | 126 | if (!$this->restrictedScope) { |
@@ -139,7 +148,9 @@ discard block |
||
139 | 148 | } |
140 | 149 | |
141 | 150 | /** |
142 | - * @param Nodes\Mixin $mixin |
|
151 | + * @param Mixin $mixin |
|
152 | + * @param string $name |
|
153 | + * @param string $blockName |
|
143 | 154 | */ |
144 | 155 | protected function visitMixinCall(Mixin $mixin, $name, $blockName, $attributes) |
145 | 156 | { |
@@ -234,7 +245,8 @@ discard block |
||
234 | 245 | } |
235 | 246 | |
236 | 247 | /** |
237 | - * @param Nodes\Mixin $mixin |
|
248 | + * @param Mixin $mixin |
|
249 | + * @param string $name |
|
238 | 250 | */ |
239 | 251 | protected function visitMixinDeclaration(Mixin $mixin, $name) |
240 | 252 | { |
@@ -264,7 +276,7 @@ discard block |
||
264 | 276 | } |
265 | 277 | |
266 | 278 | /** |
267 | - * @param Nodes\Mixin $mixin |
|
279 | + * @param Mixin $mixin |
|
268 | 280 | */ |
269 | 281 | protected function visitMixin(Mixin $mixin) |
270 | 282 | { |
@@ -6,281 +6,281 @@ |
||
6 | 6 | |
7 | 7 | abstract class MixinVisitor extends CodeVisitor |
8 | 8 | { |
9 | - protected function getMixinArgumentAssign($argument) |
|
10 | - { |
|
11 | - $argument = trim($argument); |
|
12 | - |
|
13 | - if (preg_match('`^[a-zA-Z][a-zA-Z0-9:_-]*\s*=`', $argument)) { |
|
14 | - return explode('=', $argument, 2); |
|
15 | - } |
|
16 | - } |
|
17 | - |
|
18 | - protected function parseMixinArguments(&$arguments, &$containsOnlyArrays, &$defaultAttributes) |
|
19 | - { |
|
20 | - $newArrayKey = null; |
|
21 | - $arguments = is_null($arguments) ? array() : explode(',', $arguments); |
|
22 | - foreach ($arguments as $key => &$argument) { |
|
23 | - if ($tab = $this->getMixinArgumentAssign($argument)) { |
|
24 | - if (is_null($newArrayKey)) { |
|
25 | - $newArrayKey = $key; |
|
26 | - $argument = array(); |
|
27 | - } else { |
|
28 | - unset($arguments[$key]); |
|
29 | - } |
|
30 | - |
|
31 | - $defaultAttributes[] = var_export($tab[0], true) . ' => ' . $tab[1]; |
|
32 | - $arguments[$newArrayKey][$tab[0]] = static::decodeValue($tab[1]); |
|
33 | - continue; |
|
34 | - } |
|
35 | - |
|
36 | - $containsOnlyArrays = false; |
|
37 | - $newArrayKey = null; |
|
38 | - } |
|
39 | - |
|
40 | - return array_map(function ($argument) { |
|
41 | - if (is_array($argument)) { |
|
42 | - $argument = var_export($argument, true); |
|
43 | - } |
|
44 | - |
|
45 | - return $argument; |
|
46 | - }, $arguments); |
|
47 | - } |
|
48 | - |
|
49 | - protected function parseMixinStringAttribute($data) |
|
50 | - { |
|
51 | - $value = is_array($data['value']) |
|
52 | - ? preg_split('`\s+`', trim(implode(' ', $data['value']))) |
|
53 | - : trim($data['value']); |
|
54 | - |
|
55 | - return $data['escaped'] === true |
|
56 | - ? is_array($value) |
|
57 | - ? array_map('htmlspecialchars', $value) |
|
58 | - : htmlspecialchars($value) |
|
59 | - : $value; |
|
60 | - } |
|
61 | - |
|
62 | - protected function parseMixinAttribute($data) |
|
63 | - { |
|
64 | - if ($data['value'] === 'null' || $data['value'] === 'undefined' || is_null($data['value'])) { |
|
65 | - return; |
|
66 | - } |
|
67 | - |
|
68 | - if (is_bool($data['value'])) { |
|
69 | - return $data['value']; |
|
70 | - } |
|
71 | - |
|
72 | - return $this->parseMixinStringAttribute($data); |
|
73 | - } |
|
74 | - |
|
75 | - protected function parseMixinAttributes($attributes, $defaultAttributes, $mixinAttributes) |
|
76 | - { |
|
77 | - if (!count($attributes)) { |
|
78 | - return "(isset(\$attributes)) ? \$attributes : array($defaultAttributes)"; |
|
79 | - } |
|
80 | - |
|
81 | - $parsedAttributes = array(); |
|
82 | - foreach ($attributes as $data) { |
|
83 | - $parsedAttributes[$data['name']] = $this->parseMixinAttribute($data); |
|
84 | - } |
|
85 | - |
|
86 | - $attributes = var_export($parsedAttributes, true); |
|
87 | - $mixinAttributes = var_export(static::decodeAttributes($mixinAttributes), true); |
|
88 | - |
|
89 | - return "array_merge(\\Jade\\Compiler::withMixinAttributes($attributes, $mixinAttributes), (isset(\$attributes)) ? \$attributes : array($defaultAttributes))"; |
|
90 | - } |
|
91 | - |
|
92 | - protected function renderClosureOpenning() |
|
93 | - { |
|
94 | - $arguments = func_get_args(); |
|
95 | - $begin = array_shift($arguments); |
|
96 | - $begin = is_array($begin) |
|
97 | - ? $begin[0] . 'function ' . $begin[1] |
|
98 | - : $begin . 'function '; |
|
99 | - $params = implode(', ', array_map(function ($name) { |
|
100 | - return (substr($name, 0, 1) === '$' ? '' : '$') . $name; |
|
101 | - }, $arguments)); |
|
102 | - |
|
103 | - if ($this->restrictedScope) { |
|
104 | - return $this->buffer($this->createCode($begin . '(' . $params . ') {')); |
|
105 | - } |
|
106 | - |
|
107 | - $params = '&$__varHandler, ' . $params; |
|
108 | - |
|
109 | - $this->buffer( |
|
110 | - $this->createCode($begin . '(' . $params . ') {') . |
|
111 | - $this->createCode($this->indent() . 'extract($__varHandler, EXTR_SKIP);') |
|
112 | - ); |
|
113 | - } |
|
114 | - |
|
115 | - protected function renderClosureClosing($code, $arguments = array()) |
|
116 | - { |
|
117 | - if (!$this->restrictedScope) { |
|
118 | - $arguments = array_filter(array_map(function ($argument) { |
|
119 | - $argument = explode('=', $argument); |
|
120 | - $argument = trim($argument[0]); |
|
121 | - |
|
122 | - return substr($argument, 0, 1) === '$' |
|
123 | - ? substr($argument, 1) |
|
124 | - : false; |
|
125 | - }, array_slice($arguments, 1))); |
|
126 | - $exception = count($arguments) |
|
127 | - ? ' && !in_array($key, ' . var_export($arguments, true) . ')' |
|
128 | - : ''; |
|
129 | - $this->buffer($this->createCode( |
|
130 | - 'foreach ($__varHandler as $key => &$val) {' . |
|
131 | - 'if ($key !== \'__varHandler\'' . $exception . ') {' . |
|
132 | - '$val = ${$key};' . |
|
133 | - '}' . |
|
134 | - '}' |
|
135 | - )); |
|
136 | - } |
|
137 | - |
|
138 | - $this->buffer($this->createCode($code)); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @param Nodes\Mixin $mixin |
|
143 | - */ |
|
144 | - protected function visitMixinCall(Mixin $mixin, $name, $blockName, $attributes) |
|
145 | - { |
|
146 | - $arguments = $mixin->arguments; |
|
147 | - $block = $mixin->block; |
|
148 | - $defaultAttributes = array(); |
|
149 | - $containsOnlyArrays = true; |
|
150 | - $arguments = $this->parseMixinArguments($mixin->arguments, $containsOnlyArrays, $defaultAttributes); |
|
151 | - |
|
152 | - $defaultAttributes = implode(', ', $defaultAttributes); |
|
153 | - $attributes = $this->parseMixinAttributes($attributes, $defaultAttributes, $mixin->attributes); |
|
154 | - |
|
155 | - if ($block) { |
|
156 | - $this->renderClosureOpenning("\\Jade\\Compiler::recordMixinBlock($blockName, ", 'attributes'); |
|
157 | - $this->visit($block); |
|
158 | - $this->renderClosureClosing('});'); |
|
159 | - } |
|
160 | - |
|
161 | - $strings = array(); |
|
162 | - $arguments = preg_replace_callback( |
|
163 | - '#([\'"])(.*(?!<\\\\)(?:\\\\{2})*)\\1#U', |
|
164 | - function ($match) use (&$strings) { |
|
165 | - $nextIndex = count($strings); |
|
166 | - $strings[] = $match[0]; |
|
167 | - |
|
168 | - return 'stringToReplaceBy' . $nextIndex . 'ThCapture'; |
|
169 | - }, |
|
170 | - $arguments |
|
171 | - ); |
|
172 | - $arguments = array_map( |
|
173 | - function ($arg) use ($strings) { |
|
174 | - return preg_replace_callback( |
|
175 | - '#stringToReplaceBy([0-9]+)ThCapture#', |
|
176 | - function ($match) use ($strings) { |
|
177 | - return $strings[intval($match[1])]; |
|
178 | - }, |
|
179 | - $arg |
|
180 | - ); |
|
181 | - }, |
|
182 | - $arguments |
|
183 | - ); |
|
184 | - |
|
185 | - array_unshift($arguments, $attributes); |
|
186 | - $arguments = array_filter($arguments, 'strlen'); |
|
187 | - $statements = $this->apply('createStatements', $arguments); |
|
188 | - |
|
189 | - $variables = array_pop($statements); |
|
190 | - if ($mixin->call && $containsOnlyArrays) { |
|
191 | - array_splice($variables, 1, 0, array('null')); |
|
192 | - } |
|
193 | - $variables = implode(', ', $variables); |
|
194 | - array_push($statements, $variables); |
|
195 | - |
|
196 | - $arguments = $statements; |
|
197 | - |
|
198 | - $paramsPrefix = ''; |
|
199 | - if (!$this->restrictedScope) { |
|
200 | - $this->buffer($this->createCode('$__varHandler = get_defined_vars();')); |
|
201 | - $paramsPrefix = '$__varHandler, '; |
|
202 | - } |
|
203 | - $codeFormat = str_repeat('%s;', count($arguments) - 1) . "{$name}({$paramsPrefix}%s)"; |
|
204 | - |
|
205 | - array_unshift($arguments, $codeFormat); |
|
206 | - |
|
207 | - $this->buffer($this->apply('createCode', $arguments)); |
|
208 | - if (!$this->restrictedScope) { |
|
209 | - $this->buffer( |
|
210 | - $this->createCode( |
|
211 | - 'extract(array_diff_key($__varHandler, array(\'__varHandler\' => 1, \'attributes\' => 1)));' |
|
212 | - ) |
|
213 | - ); |
|
214 | - } |
|
215 | - |
|
216 | - if ($block) { |
|
217 | - $code = $this->createCode("\\Jade\\Compiler::terminateMixinBlock($blockName);"); |
|
218 | - $this->buffer($code); |
|
219 | - } |
|
220 | - } |
|
221 | - |
|
222 | - protected function visitMixinCodeAndBlock($name, $block, $arguments) |
|
223 | - { |
|
224 | - $this->renderClosureOpenning( |
|
225 | - $this->allowMixinOverride |
|
226 | - ? "{$name} = " |
|
227 | - : array("if(!function_exists('{$name}')) { ", $name), |
|
228 | - implode(',', $arguments) |
|
229 | - ); |
|
230 | - $this->indents++; |
|
231 | - $this->visit($block); |
|
232 | - $this->indents--; |
|
233 | - $this->renderClosureClosing($this->allowMixinOverride ? '};' : '} }', $arguments); |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * @param Nodes\Mixin $mixin |
|
238 | - */ |
|
239 | - protected function visitMixinDeclaration(Mixin $mixin, $name) |
|
240 | - { |
|
241 | - $arguments = $mixin->arguments; |
|
242 | - $block = $mixin->block; |
|
243 | - $previousVisitedMixin = isset($this->visitedMixin) ? $this->visitedMixin : null; |
|
244 | - $this->visitedMixin = $mixin; |
|
245 | - if ($arguments === null || empty($arguments)) { |
|
246 | - $arguments = array(); |
|
247 | - } elseif (!is_array($arguments)) { |
|
248 | - $arguments = array($arguments); |
|
249 | - } |
|
250 | - |
|
251 | - array_unshift($arguments, 'attributes'); |
|
252 | - $arguments = implode(',', $arguments); |
|
253 | - $arguments = explode(',', $arguments); |
|
254 | - array_walk($arguments, array(get_class(), 'initArgToNull')); |
|
255 | - $this->visitMixinCodeAndBlock($name, $block, $arguments); |
|
256 | - |
|
257 | - if (is_null($previousVisitedMixin)) { |
|
258 | - unset($this->visitedMixin); |
|
259 | - |
|
260 | - return; |
|
261 | - } |
|
262 | - |
|
263 | - $this->visitedMixin = $previousVisitedMixin; |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * @param Nodes\Mixin $mixin |
|
268 | - */ |
|
269 | - protected function visitMixin(Mixin $mixin) |
|
270 | - { |
|
271 | - $name = strtr($mixin->name, '-', '_') . '_mixin'; |
|
272 | - $blockName = var_export($mixin->name, true); |
|
273 | - if ($this->allowMixinOverride) { |
|
274 | - $name = '$GLOBALS[\'' . $name . '\']'; |
|
275 | - } |
|
276 | - $attributes = static::decodeAttributes($mixin->attributes); |
|
277 | - |
|
278 | - if ($mixin->call) { |
|
279 | - $this->visitMixinCall($mixin, $name, $blockName, $attributes); |
|
280 | - |
|
281 | - return; |
|
282 | - } |
|
283 | - |
|
284 | - $this->visitMixinDeclaration($mixin, $name); |
|
285 | - } |
|
9 | + protected function getMixinArgumentAssign($argument) |
|
10 | + { |
|
11 | + $argument = trim($argument); |
|
12 | + |
|
13 | + if (preg_match('`^[a-zA-Z][a-zA-Z0-9:_-]*\s*=`', $argument)) { |
|
14 | + return explode('=', $argument, 2); |
|
15 | + } |
|
16 | + } |
|
17 | + |
|
18 | + protected function parseMixinArguments(&$arguments, &$containsOnlyArrays, &$defaultAttributes) |
|
19 | + { |
|
20 | + $newArrayKey = null; |
|
21 | + $arguments = is_null($arguments) ? array() : explode(',', $arguments); |
|
22 | + foreach ($arguments as $key => &$argument) { |
|
23 | + if ($tab = $this->getMixinArgumentAssign($argument)) { |
|
24 | + if (is_null($newArrayKey)) { |
|
25 | + $newArrayKey = $key; |
|
26 | + $argument = array(); |
|
27 | + } else { |
|
28 | + unset($arguments[$key]); |
|
29 | + } |
|
30 | + |
|
31 | + $defaultAttributes[] = var_export($tab[0], true) . ' => ' . $tab[1]; |
|
32 | + $arguments[$newArrayKey][$tab[0]] = static::decodeValue($tab[1]); |
|
33 | + continue; |
|
34 | + } |
|
35 | + |
|
36 | + $containsOnlyArrays = false; |
|
37 | + $newArrayKey = null; |
|
38 | + } |
|
39 | + |
|
40 | + return array_map(function ($argument) { |
|
41 | + if (is_array($argument)) { |
|
42 | + $argument = var_export($argument, true); |
|
43 | + } |
|
44 | + |
|
45 | + return $argument; |
|
46 | + }, $arguments); |
|
47 | + } |
|
48 | + |
|
49 | + protected function parseMixinStringAttribute($data) |
|
50 | + { |
|
51 | + $value = is_array($data['value']) |
|
52 | + ? preg_split('`\s+`', trim(implode(' ', $data['value']))) |
|
53 | + : trim($data['value']); |
|
54 | + |
|
55 | + return $data['escaped'] === true |
|
56 | + ? is_array($value) |
|
57 | + ? array_map('htmlspecialchars', $value) |
|
58 | + : htmlspecialchars($value) |
|
59 | + : $value; |
|
60 | + } |
|
61 | + |
|
62 | + protected function parseMixinAttribute($data) |
|
63 | + { |
|
64 | + if ($data['value'] === 'null' || $data['value'] === 'undefined' || is_null($data['value'])) { |
|
65 | + return; |
|
66 | + } |
|
67 | + |
|
68 | + if (is_bool($data['value'])) { |
|
69 | + return $data['value']; |
|
70 | + } |
|
71 | + |
|
72 | + return $this->parseMixinStringAttribute($data); |
|
73 | + } |
|
74 | + |
|
75 | + protected function parseMixinAttributes($attributes, $defaultAttributes, $mixinAttributes) |
|
76 | + { |
|
77 | + if (!count($attributes)) { |
|
78 | + return "(isset(\$attributes)) ? \$attributes : array($defaultAttributes)"; |
|
79 | + } |
|
80 | + |
|
81 | + $parsedAttributes = array(); |
|
82 | + foreach ($attributes as $data) { |
|
83 | + $parsedAttributes[$data['name']] = $this->parseMixinAttribute($data); |
|
84 | + } |
|
85 | + |
|
86 | + $attributes = var_export($parsedAttributes, true); |
|
87 | + $mixinAttributes = var_export(static::decodeAttributes($mixinAttributes), true); |
|
88 | + |
|
89 | + return "array_merge(\\Jade\\Compiler::withMixinAttributes($attributes, $mixinAttributes), (isset(\$attributes)) ? \$attributes : array($defaultAttributes))"; |
|
90 | + } |
|
91 | + |
|
92 | + protected function renderClosureOpenning() |
|
93 | + { |
|
94 | + $arguments = func_get_args(); |
|
95 | + $begin = array_shift($arguments); |
|
96 | + $begin = is_array($begin) |
|
97 | + ? $begin[0] . 'function ' . $begin[1] |
|
98 | + : $begin . 'function '; |
|
99 | + $params = implode(', ', array_map(function ($name) { |
|
100 | + return (substr($name, 0, 1) === '$' ? '' : '$') . $name; |
|
101 | + }, $arguments)); |
|
102 | + |
|
103 | + if ($this->restrictedScope) { |
|
104 | + return $this->buffer($this->createCode($begin . '(' . $params . ') {')); |
|
105 | + } |
|
106 | + |
|
107 | + $params = '&$__varHandler, ' . $params; |
|
108 | + |
|
109 | + $this->buffer( |
|
110 | + $this->createCode($begin . '(' . $params . ') {') . |
|
111 | + $this->createCode($this->indent() . 'extract($__varHandler, EXTR_SKIP);') |
|
112 | + ); |
|
113 | + } |
|
114 | + |
|
115 | + protected function renderClosureClosing($code, $arguments = array()) |
|
116 | + { |
|
117 | + if (!$this->restrictedScope) { |
|
118 | + $arguments = array_filter(array_map(function ($argument) { |
|
119 | + $argument = explode('=', $argument); |
|
120 | + $argument = trim($argument[0]); |
|
121 | + |
|
122 | + return substr($argument, 0, 1) === '$' |
|
123 | + ? substr($argument, 1) |
|
124 | + : false; |
|
125 | + }, array_slice($arguments, 1))); |
|
126 | + $exception = count($arguments) |
|
127 | + ? ' && !in_array($key, ' . var_export($arguments, true) . ')' |
|
128 | + : ''; |
|
129 | + $this->buffer($this->createCode( |
|
130 | + 'foreach ($__varHandler as $key => &$val) {' . |
|
131 | + 'if ($key !== \'__varHandler\'' . $exception . ') {' . |
|
132 | + '$val = ${$key};' . |
|
133 | + '}' . |
|
134 | + '}' |
|
135 | + )); |
|
136 | + } |
|
137 | + |
|
138 | + $this->buffer($this->createCode($code)); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @param Nodes\Mixin $mixin |
|
143 | + */ |
|
144 | + protected function visitMixinCall(Mixin $mixin, $name, $blockName, $attributes) |
|
145 | + { |
|
146 | + $arguments = $mixin->arguments; |
|
147 | + $block = $mixin->block; |
|
148 | + $defaultAttributes = array(); |
|
149 | + $containsOnlyArrays = true; |
|
150 | + $arguments = $this->parseMixinArguments($mixin->arguments, $containsOnlyArrays, $defaultAttributes); |
|
151 | + |
|
152 | + $defaultAttributes = implode(', ', $defaultAttributes); |
|
153 | + $attributes = $this->parseMixinAttributes($attributes, $defaultAttributes, $mixin->attributes); |
|
154 | + |
|
155 | + if ($block) { |
|
156 | + $this->renderClosureOpenning("\\Jade\\Compiler::recordMixinBlock($blockName, ", 'attributes'); |
|
157 | + $this->visit($block); |
|
158 | + $this->renderClosureClosing('});'); |
|
159 | + } |
|
160 | + |
|
161 | + $strings = array(); |
|
162 | + $arguments = preg_replace_callback( |
|
163 | + '#([\'"])(.*(?!<\\\\)(?:\\\\{2})*)\\1#U', |
|
164 | + function ($match) use (&$strings) { |
|
165 | + $nextIndex = count($strings); |
|
166 | + $strings[] = $match[0]; |
|
167 | + |
|
168 | + return 'stringToReplaceBy' . $nextIndex . 'ThCapture'; |
|
169 | + }, |
|
170 | + $arguments |
|
171 | + ); |
|
172 | + $arguments = array_map( |
|
173 | + function ($arg) use ($strings) { |
|
174 | + return preg_replace_callback( |
|
175 | + '#stringToReplaceBy([0-9]+)ThCapture#', |
|
176 | + function ($match) use ($strings) { |
|
177 | + return $strings[intval($match[1])]; |
|
178 | + }, |
|
179 | + $arg |
|
180 | + ); |
|
181 | + }, |
|
182 | + $arguments |
|
183 | + ); |
|
184 | + |
|
185 | + array_unshift($arguments, $attributes); |
|
186 | + $arguments = array_filter($arguments, 'strlen'); |
|
187 | + $statements = $this->apply('createStatements', $arguments); |
|
188 | + |
|
189 | + $variables = array_pop($statements); |
|
190 | + if ($mixin->call && $containsOnlyArrays) { |
|
191 | + array_splice($variables, 1, 0, array('null')); |
|
192 | + } |
|
193 | + $variables = implode(', ', $variables); |
|
194 | + array_push($statements, $variables); |
|
195 | + |
|
196 | + $arguments = $statements; |
|
197 | + |
|
198 | + $paramsPrefix = ''; |
|
199 | + if (!$this->restrictedScope) { |
|
200 | + $this->buffer($this->createCode('$__varHandler = get_defined_vars();')); |
|
201 | + $paramsPrefix = '$__varHandler, '; |
|
202 | + } |
|
203 | + $codeFormat = str_repeat('%s;', count($arguments) - 1) . "{$name}({$paramsPrefix}%s)"; |
|
204 | + |
|
205 | + array_unshift($arguments, $codeFormat); |
|
206 | + |
|
207 | + $this->buffer($this->apply('createCode', $arguments)); |
|
208 | + if (!$this->restrictedScope) { |
|
209 | + $this->buffer( |
|
210 | + $this->createCode( |
|
211 | + 'extract(array_diff_key($__varHandler, array(\'__varHandler\' => 1, \'attributes\' => 1)));' |
|
212 | + ) |
|
213 | + ); |
|
214 | + } |
|
215 | + |
|
216 | + if ($block) { |
|
217 | + $code = $this->createCode("\\Jade\\Compiler::terminateMixinBlock($blockName);"); |
|
218 | + $this->buffer($code); |
|
219 | + } |
|
220 | + } |
|
221 | + |
|
222 | + protected function visitMixinCodeAndBlock($name, $block, $arguments) |
|
223 | + { |
|
224 | + $this->renderClosureOpenning( |
|
225 | + $this->allowMixinOverride |
|
226 | + ? "{$name} = " |
|
227 | + : array("if(!function_exists('{$name}')) { ", $name), |
|
228 | + implode(',', $arguments) |
|
229 | + ); |
|
230 | + $this->indents++; |
|
231 | + $this->visit($block); |
|
232 | + $this->indents--; |
|
233 | + $this->renderClosureClosing($this->allowMixinOverride ? '};' : '} }', $arguments); |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * @param Nodes\Mixin $mixin |
|
238 | + */ |
|
239 | + protected function visitMixinDeclaration(Mixin $mixin, $name) |
|
240 | + { |
|
241 | + $arguments = $mixin->arguments; |
|
242 | + $block = $mixin->block; |
|
243 | + $previousVisitedMixin = isset($this->visitedMixin) ? $this->visitedMixin : null; |
|
244 | + $this->visitedMixin = $mixin; |
|
245 | + if ($arguments === null || empty($arguments)) { |
|
246 | + $arguments = array(); |
|
247 | + } elseif (!is_array($arguments)) { |
|
248 | + $arguments = array($arguments); |
|
249 | + } |
|
250 | + |
|
251 | + array_unshift($arguments, 'attributes'); |
|
252 | + $arguments = implode(',', $arguments); |
|
253 | + $arguments = explode(',', $arguments); |
|
254 | + array_walk($arguments, array(get_class(), 'initArgToNull')); |
|
255 | + $this->visitMixinCodeAndBlock($name, $block, $arguments); |
|
256 | + |
|
257 | + if (is_null($previousVisitedMixin)) { |
|
258 | + unset($this->visitedMixin); |
|
259 | + |
|
260 | + return; |
|
261 | + } |
|
262 | + |
|
263 | + $this->visitedMixin = $previousVisitedMixin; |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * @param Nodes\Mixin $mixin |
|
268 | + */ |
|
269 | + protected function visitMixin(Mixin $mixin) |
|
270 | + { |
|
271 | + $name = strtr($mixin->name, '-', '_') . '_mixin'; |
|
272 | + $blockName = var_export($mixin->name, true); |
|
273 | + if ($this->allowMixinOverride) { |
|
274 | + $name = '$GLOBALS[\'' . $name . '\']'; |
|
275 | + } |
|
276 | + $attributes = static::decodeAttributes($mixin->attributes); |
|
277 | + |
|
278 | + if ($mixin->call) { |
|
279 | + $this->visitMixinCall($mixin, $name, $blockName, $attributes); |
|
280 | + |
|
281 | + return; |
|
282 | + } |
|
283 | + |
|
284 | + $this->visitMixinDeclaration($mixin, $name); |
|
285 | + } |
|
286 | 286 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | unset($arguments[$key]); |
29 | 29 | } |
30 | 30 | |
31 | - $defaultAttributes[] = var_export($tab[0], true) . ' => ' . $tab[1]; |
|
31 | + $defaultAttributes[] = var_export($tab[0], true).' => '.$tab[1]; |
|
32 | 32 | $arguments[$newArrayKey][$tab[0]] = static::decodeValue($tab[1]); |
33 | 33 | continue; |
34 | 34 | } |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | $newArrayKey = null; |
38 | 38 | } |
39 | 39 | |
40 | - return array_map(function ($argument) { |
|
40 | + return array_map(function($argument) { |
|
41 | 41 | if (is_array($argument)) { |
42 | 42 | $argument = var_export($argument, true); |
43 | 43 | } |
@@ -94,28 +94,28 @@ discard block |
||
94 | 94 | $arguments = func_get_args(); |
95 | 95 | $begin = array_shift($arguments); |
96 | 96 | $begin = is_array($begin) |
97 | - ? $begin[0] . 'function ' . $begin[1] |
|
98 | - : $begin . 'function '; |
|
99 | - $params = implode(', ', array_map(function ($name) { |
|
100 | - return (substr($name, 0, 1) === '$' ? '' : '$') . $name; |
|
97 | + ? $begin[0].'function '.$begin[1] |
|
98 | + : $begin.'function '; |
|
99 | + $params = implode(', ', array_map(function($name) { |
|
100 | + return (substr($name, 0, 1) === '$' ? '' : '$').$name; |
|
101 | 101 | }, $arguments)); |
102 | 102 | |
103 | 103 | if ($this->restrictedScope) { |
104 | - return $this->buffer($this->createCode($begin . '(' . $params . ') {')); |
|
104 | + return $this->buffer($this->createCode($begin.'('.$params.') {')); |
|
105 | 105 | } |
106 | 106 | |
107 | - $params = '&$__varHandler, ' . $params; |
|
107 | + $params = '&$__varHandler, '.$params; |
|
108 | 108 | |
109 | 109 | $this->buffer( |
110 | - $this->createCode($begin . '(' . $params . ') {') . |
|
111 | - $this->createCode($this->indent() . 'extract($__varHandler, EXTR_SKIP);') |
|
110 | + $this->createCode($begin.'('.$params.') {'). |
|
111 | + $this->createCode($this->indent().'extract($__varHandler, EXTR_SKIP);') |
|
112 | 112 | ); |
113 | 113 | } |
114 | 114 | |
115 | 115 | protected function renderClosureClosing($code, $arguments = array()) |
116 | 116 | { |
117 | 117 | if (!$this->restrictedScope) { |
118 | - $arguments = array_filter(array_map(function ($argument) { |
|
118 | + $arguments = array_filter(array_map(function($argument) { |
|
119 | 119 | $argument = explode('=', $argument); |
120 | 120 | $argument = trim($argument[0]); |
121 | 121 | |
@@ -124,13 +124,13 @@ discard block |
||
124 | 124 | : false; |
125 | 125 | }, array_slice($arguments, 1))); |
126 | 126 | $exception = count($arguments) |
127 | - ? ' && !in_array($key, ' . var_export($arguments, true) . ')' |
|
127 | + ? ' && !in_array($key, '.var_export($arguments, true).')' |
|
128 | 128 | : ''; |
129 | 129 | $this->buffer($this->createCode( |
130 | - 'foreach ($__varHandler as $key => &$val) {' . |
|
131 | - 'if ($key !== \'__varHandler\'' . $exception . ') {' . |
|
132 | - '$val = ${$key};' . |
|
133 | - '}' . |
|
130 | + 'foreach ($__varHandler as $key => &$val) {'. |
|
131 | + 'if ($key !== \'__varHandler\''.$exception.') {'. |
|
132 | + '$val = ${$key};'. |
|
133 | + '}'. |
|
134 | 134 | '}' |
135 | 135 | )); |
136 | 136 | } |
@@ -161,19 +161,19 @@ discard block |
||
161 | 161 | $strings = array(); |
162 | 162 | $arguments = preg_replace_callback( |
163 | 163 | '#([\'"])(.*(?!<\\\\)(?:\\\\{2})*)\\1#U', |
164 | - function ($match) use (&$strings) { |
|
164 | + function($match) use (&$strings) { |
|
165 | 165 | $nextIndex = count($strings); |
166 | 166 | $strings[] = $match[0]; |
167 | 167 | |
168 | - return 'stringToReplaceBy' . $nextIndex . 'ThCapture'; |
|
168 | + return 'stringToReplaceBy'.$nextIndex.'ThCapture'; |
|
169 | 169 | }, |
170 | 170 | $arguments |
171 | 171 | ); |
172 | 172 | $arguments = array_map( |
173 | - function ($arg) use ($strings) { |
|
173 | + function($arg) use ($strings) { |
|
174 | 174 | return preg_replace_callback( |
175 | 175 | '#stringToReplaceBy([0-9]+)ThCapture#', |
176 | - function ($match) use ($strings) { |
|
176 | + function($match) use ($strings) { |
|
177 | 177 | return $strings[intval($match[1])]; |
178 | 178 | }, |
179 | 179 | $arg |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | $this->buffer($this->createCode('$__varHandler = get_defined_vars();')); |
201 | 201 | $paramsPrefix = '$__varHandler, '; |
202 | 202 | } |
203 | - $codeFormat = str_repeat('%s;', count($arguments) - 1) . "{$name}({$paramsPrefix}%s)"; |
|
203 | + $codeFormat = str_repeat('%s;', count($arguments) - 1)."{$name}({$paramsPrefix}%s)"; |
|
204 | 204 | |
205 | 205 | array_unshift($arguments, $codeFormat); |
206 | 206 | |
@@ -268,10 +268,10 @@ discard block |
||
268 | 268 | */ |
269 | 269 | protected function visitMixin(Mixin $mixin) |
270 | 270 | { |
271 | - $name = strtr($mixin->name, '-', '_') . '_mixin'; |
|
271 | + $name = strtr($mixin->name, '-', '_').'_mixin'; |
|
272 | 272 | $blockName = var_export($mixin->name, true); |
273 | 273 | if ($this->allowMixinOverride) { |
274 | - $name = '$GLOBALS[\'' . $name . '\']'; |
|
274 | + $name = '$GLOBALS[\''.$name.'\']'; |
|
275 | 275 | } |
276 | 276 | $attributes = static::decodeAttributes($mixin->attributes); |
277 | 277 |