1 | <?php |
||
28 | class Context extends Flags |
||
29 | { |
||
30 | /** |
||
31 | * Create a context from options |
||
32 | * |
||
33 | * @param array<string,array|string|integer> $options input options |
||
34 | * |
||
35 | * @return array<string,array|string|integer> Context from options |
||
36 | */ |
||
37 | 768 | public static function create($options) { |
|
38 | 768 | if (!is_array($options)) { |
|
39 | 7 | $options = array(); |
|
40 | } |
||
41 | |||
42 | 768 | $flags = isset($options['flags']) ? $options['flags'] : static::FLAG_BESTPERFORMANCE; |
|
43 | |||
44 | $context = array( |
||
45 | 'flags' => array( |
||
46 | 768 | 'errorlog' => $flags & static::FLAG_ERROR_LOG, |
|
47 | 768 | 'exception' => $flags & static::FLAG_ERROR_EXCEPTION, |
|
48 | 768 | 'skippartial' => $flags & static::FLAG_ERROR_SKIPPARTIAL, |
|
49 | 768 | 'standalone' => $flags & static::FLAG_STANDALONEPHP, |
|
50 | 768 | 'noesc' => $flags & static::FLAG_NOESCAPE, |
|
51 | 768 | 'jstrue' => $flags & static::FLAG_JSTRUE, |
|
52 | 768 | 'jsobj' => $flags & static::FLAG_JSOBJECT, |
|
53 | 768 | 'jslen' => $flags & static::FLAG_JSLENGTH, |
|
54 | 768 | 'hbesc' => $flags & static::FLAG_HBESCAPE, |
|
55 | 768 | 'this' => $flags & static::FLAG_THIS, |
|
56 | 768 | 'nohbh' => $flags & static::FLAG_NOHBHELPERS, |
|
57 | 768 | 'parent' => $flags & static::FLAG_PARENT, |
|
58 | 768 | 'echo' => $flags & static::FLAG_ECHO, |
|
59 | 768 | 'advar' => $flags & static::FLAG_ADVARNAME, |
|
60 | 768 | 'namev' => $flags & static::FLAG_NAMEDARG, |
|
61 | 768 | 'spvar' => $flags & static::FLAG_SPVARS, |
|
62 | 768 | 'slash' => $flags & static::FLAG_SLASH, |
|
63 | 768 | 'else' => $flags & static::FLAG_ELSE, |
|
64 | 768 | 'exhlp' => $flags & static::FLAG_EXTHELPER, |
|
65 | 768 | 'lambda' => $flags & static::FLAG_HANDLEBARSLAMBDA, |
|
66 | 768 | 'mustlok' => $flags & static::FLAG_MUSTACHELOOKUP, |
|
67 | 768 | 'mustlam' => $flags & static::FLAG_MUSTACHELAMBDA, |
|
68 | 768 | 'mustsec' => $flags & static::FLAG_MUSTACHESECTION, |
|
69 | 768 | 'noind' => $flags & static::FLAG_PREVENTINDENT, |
|
70 | 768 | 'debug' => $flags & static::FLAG_RENDER_DEBUG, |
|
71 | 768 | 'prop' => $flags & static::FLAG_PROPERTY, |
|
72 | 768 | 'method' => $flags & static::FLAG_METHOD, |
|
73 | 768 | 'runpart' => $flags & static::FLAG_RUNTIMEPARTIAL, |
|
74 | 768 | 'rawblock' => $flags & static::FLAG_RAWBLOCK, |
|
75 | 768 | 'partnc' => $flags & static::FLAG_PARTIALNEWCONTEXT, |
|
76 | 768 | 'nostd' => $flags & static::FLAG_IGNORESTANDALONE, |
|
77 | 768 | 'strpar' => $flags & static::FLAG_STRINGPARAMS, |
|
78 | 768 | 'knohlp' => $flags & static::FLAG_KNOWNHELPERSONLY, |
|
79 | 768 | ), |
|
80 | 'delimiters' => array( |
||
81 | 768 | isset($options['delimiters'][0]) ? $options['delimiters'][0] : '{{', |
|
82 | 768 | isset($options['delimiters'][1]) ? $options['delimiters'][1] : '}}', |
|
83 | ), |
||
84 | 768 | 'level' => 0, |
|
85 | 'stack' => array(), |
||
86 | 'currentToken' => null, |
||
87 | 'error' => array(), |
||
88 | 'elselvl' => array(), |
||
89 | 'elsechain' => false, |
||
90 | 'tokens' => array( |
||
91 | 'standalone' => true, |
||
92 | 'ahead' => false, |
||
93 | 'current' => 0, |
||
94 | 'count' => 0, |
||
95 | 'partialind' => '', |
||
96 | ), |
||
97 | 'usedPartial' => array(), |
||
98 | 'partialStack' => array(), |
||
99 | 'partialCode' => array(), |
||
100 | 'usedFeature' => array( |
||
101 | 'rootthis' => 0, |
||
102 | 'enc' => 0, |
||
103 | 'raw' => 0, |
||
104 | 'sec' => 0, |
||
105 | 'isec' => 0, |
||
106 | 'if' => 0, |
||
107 | 'else' => 0, |
||
108 | 'unless' => 0, |
||
109 | 'each' => 0, |
||
110 | 'this' => 0, |
||
111 | 'parent' => 0, |
||
112 | 'with' => 0, |
||
113 | 'comment' => 0, |
||
114 | 'partial' => 0, |
||
115 | 'dynpartial' => 0, |
||
116 | 'inlpartial' => 0, |
||
117 | 'helper' => 0, |
||
118 | 'delimiter' => 0, |
||
119 | 'subexp' => 0, |
||
120 | 'rawblock' => 0, |
||
121 | 'pblock' => 0, |
||
122 | 'lookup' => 0, |
||
123 | 'log' => 0, |
||
124 | ), |
||
125 | 'usedCount' => array( |
||
126 | 'var' => array(), |
||
127 | 'helpers' => array(), |
||
128 | 'runtime' => array(), |
||
129 | ), |
||
130 | 'compile' => false, |
||
131 | 'parsed' => array(), |
||
132 | 768 | 'partials' => (isset($options['partials']) && is_array($options['partials'])) ? $options['partials'] : array(), |
|
133 | 'partialblock' => array(), |
||
134 | 'inlinepartial' => array(), |
||
135 | 'helpers' => array(), |
||
136 | 768 | 'renderex' => isset($options['renderex']) ? $options['renderex'] : '', |
|
137 | 768 | 'prepartial' => (isset($options['prepartial']) && is_callable($options['prepartial'])) ? $options['prepartial'] : false, |
|
138 | 768 | 'helperresolver' => (isset($options['helperresolver']) && is_callable($options['helperresolver'])) ? $options['helperresolver'] : false, |
|
139 | 768 | 'partialresolver' => (isset($options['partialresolver']) && is_callable($options['partialresolver'])) ? $options['partialresolver'] : false, |
|
140 | 768 | 'runtime' => isset($options['runtime']) ? $options['runtime'] : '\\LightnCandy\\Runtime', |
|
141 | 768 | 'runtimealias' => 'LR', |
|
142 | 768 | 'safestring' => '\\LightnCandy\\SafeString', |
|
143 | 768 | 'safestringalias' => isset($options['safestring']) ? $options['safestring'] : 'LS', |
|
144 | 'rawblock' => false, |
||
145 | 768 | 'funcprefix' => uniqid('lcr'), |
|
146 | ); |
||
147 | |||
148 | 768 | $context['ops'] = $context['flags']['echo'] ? array( |
|
149 | 23 | 'seperator' => ',', |
|
150 | 'f_start' => 'echo ', |
||
151 | 'f_end' => ';', |
||
152 | 'op_start' => 'ob_start();echo ', |
||
153 | 'op_end' => ';return ob_get_clean();', |
||
154 | 'cnd_start' => ';if ', |
||
155 | 'cnd_then' => '{echo ', |
||
156 | 'cnd_else' => ';}else{echo ', |
||
157 | 'cnd_end' => ';}echo ', |
||
158 | 'cnd_nend' => ';}', |
||
159 | ) : array( |
||
160 | 745 | 'seperator' => '.', |
|
161 | 'f_start' => 'return ', |
||
162 | 'f_end' => ';', |
||
163 | 'op_start' => 'return ', |
||
164 | 'op_end' => ';', |
||
165 | 'cnd_start' => '.(', |
||
166 | 'cnd_then' => ' ? ', |
||
167 | 'cnd_else' => ' : ', |
||
168 | 'cnd_end' => ').', |
||
169 | 'cnd_nend' => ')', |
||
170 | ); |
||
171 | |||
172 | 768 | $context['ops']['enc'] = $context['flags']['hbesc'] ? 'encq' : 'enc'; |
|
173 | 768 | $context['ops']['array_check'] = '$inary=is_array($in);'; |
|
174 | 768 | static::updateHelperTable($context, $options); |
|
175 | |||
176 | 768 | if ($context['flags']['partnc'] && ($context['flags']['runpart'] == 0)) { |
|
177 | $context['error'][] = 'The FLAG_PARTIALNEWCONTEXT requires FLAG_RUNTIMEPARTIAL! Fix your compile options please'; |
||
178 | } |
||
179 | |||
180 | 768 | return $context; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * update specific custom helper table from options |
||
185 | * |
||
186 | * @param array<string,array|string|integer> $context prepared context |
||
187 | * @param array<string,array|string|integer> $options input options |
||
188 | * @param string $tname helper table name |
||
189 | * |
||
190 | * @return array<string,array|string|integer> context with generated helper table |
||
191 | * |
||
192 | * @expect array() when input array(), array() |
||
193 | * @expect array('flags' => array('exhlp' => 1), 'helpers' => array('abc' => 1)) when input array('flags' => array('exhlp' => 1)), array('helpers' => array('abc')) |
||
194 | * @expect array('error' => array('You provide a custom helper named as \'abc\' in options[\'helpers\'], but the function abc() is not defined!'), 'flags' => array('exhlp' => 0)) when input array('error' => array(), 'flags' => array('exhlp' => 0)), array('helpers' => array('abc')) |
||
195 | * @expect array('flags' => array('exhlp' => 1), 'helpers' => array('\\LightnCandy\\Runtime::raw' => '\\LightnCandy\\Runtime::raw')) when input array('flags' => array('exhlp' => 1), 'helpers' => array()), array('helpers' => array('\\LightnCandy\\Runtime::raw')) |
||
196 | * @expect array('flags' => array('exhlp' => 1), 'helpers' => array('test' => '\\LightnCandy\\Runtime::raw')) when input array('flags' => array('exhlp' => 1), 'helpers' => array()), array('helpers' => array('test' => '\\LightnCandy\\Runtime::raw')) |
||
197 | */ |
||
198 | 769 | protected static function updateHelperTable(&$context, $options, $tname = 'helpers') { |
|
220 | |||
221 | /** |
||
222 | * Merge a context into another |
||
223 | * |
||
224 | * @param array<string,array|string|integer> $context master context |
||
225 | * @param array<string,array|string|integer> $tmp another context will be overwrited into master context |
||
226 | */ |
||
227 | 88 | public static function merge(&$context, $tmp) { |
|
237 | } |
||
238 | |||
239 |