Overview

Namespaces

  • LightnCandy

Classes

  • LightnCandy\Compiler
  • LightnCandy\Context
  • LightnCandy\Encoder
  • LightnCandy\Exporter
  • LightnCandy\Expression
  • LightnCandy\Flags
  • LightnCandy\LightnCandy
  • LightnCandy\Parser
  • LightnCandy\Partial
  • LightnCandy\Runtime
  • LightnCandy\SafeString
  • LightnCandy\StringObject
  • LightnCandy\Token
  • LightnCandy\Validator
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /*
  3: 
  4: MIT License
  5: Copyright 2013-2018 Zordius Chen. All Rights Reserved.
  6: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  8: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  9: 
 10: Origin: https://github.com/zordius/lightncandy
 11: */
 12: 
 13: /**
 14:  * file to handle LightnCandy Context
 15:  *
 16:  * @package    LightnCandy
 17:  * @author     Zordius <zordius@gmail.com>
 18:  */
 19: 
 20: namespace LightnCandy;
 21: 
 22: use \LightnCandy\Flags;
 23: 
 24: /**
 25:  * LightnCandy class to handle Context
 26:  */
 27: class Context extends Flags
 28: {
 29:     /**
 30:      * Create a context from options
 31:      *
 32:      * @param array<string,array|string|integer> $options input options
 33:      *
 34:      * @return array<string,array|string|integer> Context from options
 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:      * 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:     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:                             // Regist helper names only
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:      * Merge a context into another
224:      *
225:      * @param array<string,array|string|integer> $context master context
226:      * @param array<string,array|string|integer> $tmp another context will be overwrited into master context
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: 
API documentation generated by ApiGen