1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: 14: 15: 16: 17: 18:
19:
20: namespace LightnCandy;
21:
22: use \LightnCandy\Flags;
23:
24: 25: 26:
27: class Context extends Flags
28: {
29: 30: 31: 32: 33: 34: 35:
36: public static function create($options)
37: {
38: if (!is_array($options)) {
39: $options = array();
40: }
41:
42: $flags = isset($options['flags']) ? $options['flags'] : static::FLAG_BESTPERFORMANCE;
43:
44: $context = array(
45: 'flags' => array(
46: 'errorlog' => $flags & static::FLAG_ERROR_LOG,
47: 'exception' => $flags & static::FLAG_ERROR_EXCEPTION,
48: 'skippartial' => $flags & static::FLAG_ERROR_SKIPPARTIAL,
49: 'standalone' => $flags & static::FLAG_STANDALONEPHP,
50: 'noesc' => $flags & static::FLAG_NOESCAPE,
51: 'jstrue' => $flags & static::FLAG_JSTRUE,
52: 'jsobj' => $flags & static::FLAG_JSOBJECT,
53: 'jslen' => $flags & static::FLAG_JSLENGTH,
54: 'hbesc' => $flags & static::FLAG_HBESCAPE,
55: 'this' => $flags & static::FLAG_THIS,
56: 'nohbh' => $flags & static::FLAG_NOHBHELPERS,
57: 'parent' => $flags & static::FLAG_PARENT,
58: 'echo' => $flags & static::FLAG_ECHO,
59: 'advar' => $flags & static::FLAG_ADVARNAME,
60: 'namev' => $flags & static::FLAG_NAMEDARG,
61: 'spvar' => $flags & static::FLAG_SPVARS,
62: 'slash' => $flags & static::FLAG_SLASH,
63: 'else' => $flags & static::FLAG_ELSE,
64: 'exhlp' => $flags & static::FLAG_EXTHELPER,
65: 'lambda' => $flags & static::FLAG_HANDLEBARSLAMBDA,
66: 'mustlok' => $flags & static::FLAG_MUSTACHELOOKUP,
67: 'mustlam' => $flags & static::FLAG_MUSTACHELAMBDA,
68: 'mustsec' => $flags & static::FLAG_MUSTACHESECTION,
69: 'noind' => $flags & static::FLAG_PREVENTINDENT,
70: 'debug' => $flags & static::FLAG_RENDER_DEBUG,
71: 'prop' => $flags & static::FLAG_PROPERTY,
72: 'method' => $flags & static::FLAG_METHOD,
73: 'runpart' => $flags & static::FLAG_RUNTIMEPARTIAL,
74: 'rawblock' => $flags & static::FLAG_RAWBLOCK,
75: 'partnc' => $flags & static::FLAG_PARTIALNEWCONTEXT,
76: 'nostd' => $flags & static::FLAG_IGNORESTANDALONE,
77: 'strpar' => $flags & static::FLAG_STRINGPARAMS,
78: 'knohlp' => $flags & static::FLAG_KNOWNHELPERSONLY,
79: ),
80: 'delimiters' => array(
81: isset($options['delimiters'][0]) ? $options['delimiters'][0] : '{{',
82: isset($options['delimiters'][1]) ? $options['delimiters'][1] : '}}',
83: ),
84: '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: 'partials' => (isset($options['partials']) && is_array($options['partials'])) ? $options['partials'] : array(),
133: 'partialblock' => array(),
134: 'inlinepartial' => array(),
135: 'helpers' => array(),
136: 'renderex' => isset($options['renderex']) ? $options['renderex'] : '',
137: 'prepartial' => (isset($options['prepartial']) && is_callable($options['prepartial'])) ? $options['prepartial'] : false,
138: 'helperresolver' => (isset($options['helperresolver']) && is_callable($options['helperresolver'])) ? $options['helperresolver'] : false,
139: 'partialresolver' => (isset($options['partialresolver']) && is_callable($options['partialresolver'])) ? $options['partialresolver'] : false,
140: 'runtime' => isset($options['runtime']) ? $options['runtime'] : '\\LightnCandy\\Runtime',
141: 'runtimealias' => 'LR',
142: 'safestring' => '\\LightnCandy\\SafeString',
143: 'safestringalias' => isset($options['safestring']) ? $options['safestring'] : 'LS',
144: 'rawblock' => false,
145: 'funcprefix' => uniqid('lcr'),
146: );
147:
148: $context['ops'] = $context['flags']['echo'] ? array(
149: '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: '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: $context['ops']['enc'] = $context['flags']['hbesc'] ? 'encq' : 'enc';
173: $context['ops']['array_check'] = '$inary=is_array($in);';
174: static::updateHelperTable($context, $options);
175:
176: 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: return $context;
181: }
182:
183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197:
198: protected static function updateHelperTable(&$context, $options, $tname = 'helpers')
199: {
200: if (isset($options[$tname]) && is_array($options[$tname])) {
201: foreach ($options[$tname] as $name => $func) {
202: $tn = is_int($name) ? $func : $name;
203: if (is_callable($func)) {
204: $context[$tname][$tn] = $func;
205: } else {
206: if (is_array($func)) {
207: $context['error'][] = "I found an array in $tname with key as $name, please fix it.";
208: } else {
209: if ($context['flags']['exhlp']) {
210:
211: $context[$tname][$tn] = 1;
212: } else {
213: $context['error'][] = "You provide a custom helper named as '$tn' in options['$tname'], but the function $func() is not defined!";
214: }
215: }
216: }
217: }
218: }
219: return $context;
220: }
221:
222: 223: 224: 225: 226: 227:
228: public static function merge(&$context, $tmp)
229: {
230: $context['error'] = $tmp['error'];
231: $context['helpers'] = $tmp['helpers'];
232: $context['partials'] = $tmp['partials'];
233: $context['partialCode'] = $tmp['partialCode'];
234: $context['partialStack'] = $tmp['partialStack'];
235: $context['usedCount'] = $tmp['usedCount'];
236: $context['usedFeature'] = $tmp['usedFeature'];
237: $context['usedPartial'] = $tmp['usedPartial'];
238: }
239: }
240: