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: Copyright 2013-2018 Zordius Chen. All Rights Reserved.
  5: 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:
  6: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  7: 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.
  8: 
  9: Origin: https://github.com/zordius/lightncandy
 10: */
 11: 
 12: /**
 13:  * file to keep LightnCandy partial methods
 14:  *
 15:  * @package    LightnCandy
 16:  * @author     Zordius <zordius@gmail.com>
 17:  */
 18: 
 19: namespace LightnCandy;
 20: 
 21: use \LightnCandy\Compiler;
 22: use \LightnCandy\SafeString;
 23: use \LightnCandy\Context;
 24: 
 25: /**
 26:  * LightnCandy Partial handler
 27:  */
 28: class Partial
 29: {
 30:     public static $TMP_JS_FUNCTION_STR = "!!\aFuNcTiOn\a!!";
 31: 
 32:     /**
 33:      * Include all partials when using dynamic partials
 34:      */
 35:     public static function handleDynamic(&$context)
 36:     {
 37:         if ($context['usedFeature']['dynpartial'] == 0) {
 38:             return;
 39:         }
 40: 
 41:         foreach ($context['partials'] as $name => $code) {
 42:             static::read($context, $name);
 43:         }
 44:     }
 45: 
 46:     /**
 47:      * Read partial file content as string and store in context
 48:      *
 49:      * @param array<string,array|string|integer> $context Current context of compiler progress.
 50:      * @param string $name partial name
 51:      *
 52:      * @return string|null $code compiled PHP code when success
 53:      */
 54:     public static function read(&$context, $name)
 55:     {
 56:         $isPB = ($name === '@partial-block');
 57:         $context['usedFeature']['partial']++;
 58: 
 59:         if (isset($context['usedPartial'][$name])) {
 60:             return;
 61:         }
 62: 
 63:         $cnt = static::resolve($context, $name);
 64: 
 65:         if ($cnt !== null) {
 66:             $context['usedPartial'][$name] = SafeString::escapeTemplate($cnt);
 67:             return static::compileDynamic($context, $name);
 68:         }
 69: 
 70:         if (!$context['flags']['skippartial'] && !$isPB) {
 71:             $context['error'][] = "Can not find partial for '$name', you should provide partials or partialresolver in options";
 72:         }
 73:     }
 74: 
 75:     /**
 76:      * preprocess partial template before it be stored into context
 77:      *
 78:      * @param array<string,array|string|integer> $context Current context of compiler progress.
 79:      * @param string $tmpl partial template
 80:      * @param string $name partial name
 81:      *
 82:      * @return string|null $content processed partial template
 83:      *
 84:      * @expect 'hey' when input Array('prepartial' => false), 'hey', 'haha'
 85:      * @expect 'haha-hoho' when input Array('prepartial' => function ($cx, $tmpl, $name) {return "$name-$tmpl";}), 'hoho', 'haha'
 86:      */
 87:     protected static function prePartial(&$context, $tmpl, &$name)
 88:     {
 89:         return $context['prepartial'] ? $context['prepartial']($context, $tmpl, $name) : $tmpl;
 90:     }
 91: 
 92:     /**
 93:      * resolve partial, return the partial content
 94:      *
 95:      * @param array<string,array|string|integer> $context Current context of compiler progress.
 96:      * @param string $name partial name
 97:      *
 98:      * @return string|null $content partial content
 99:      */
100:     public static function resolve(&$context, &$name)
101:     {
102:         if ($name === '@partial-block') {
103:             $name = "@partial-block{$context['usedFeature']['pblock']}";
104:         }
105:         if (isset($context['partials'][$name])) {
106:             return static::prePartial($context, $context['partials'][$name], $name);
107:         }
108: 
109:         return static::resolver($context, $name);
110:     }
111: 
112:     /**
113:      * use partialresolver to resolve partial, return the partial content
114:      *
115:      * @param array<string,array|string|integer> $context Current context of compiler progress.
116:      * @param string $name partial name
117:      *
118:      * @return string|null $content partial content
119:      */
120:     public static function resolver(&$context, &$name)
121:     {
122:         if ($context['partialresolver']) {
123:             $cnt = $context['partialresolver']($context, $name);
124:             return static::prePartial($context, $cnt, $name);
125:         }
126:     }
127: 
128:     /**
129:      * compile a partial to static embed PHP code
130:      *
131:      * @param array<string,array|string|integer> $context Current context of compiler progress.
132:      * @param string $name partial name
133:      *
134:      * @return string|null $code PHP code string
135:      */
136:     public static function compileStatic(&$context, $name)
137:     {
138:         // Check for recursive partial
139:         if (!$context['flags']['runpart']) {
140:             $context['partialStack'][] = $name;
141:             $diff = count($context['partialStack']) - count(array_unique($context['partialStack']));
142:             if ($diff) {
143:                 $context['error'][] = 'I found recursive partial includes as the path: ' . implode(' -> ', $context['partialStack']) . '! You should fix your template or compile with LightnCandy::FLAG_RUNTIMEPARTIAL flag.';
144:             }
145:         }
146: 
147:         $code = Compiler::compileTemplate($context, preg_replace('/^/m', $context['tokens']['partialind'], $context['usedPartial'][$name]));
148: 
149:         if (!$context['flags']['runpart']) {
150:             array_pop($context['partialStack']);
151:         }
152: 
153:         return $code;
154:     }
155: 
156:     /**
157:      * compile partial as closure, stored in context
158:      *
159:      * @param array<string,array|string|integer> $context Current context of compiler progress.
160:      * @param string $name partial name
161:      *
162:      * @return string|null $code compiled PHP code when success
163:      */
164:     public static function compileDynamic(&$context, $name)
165:     {
166:         if (!$context['flags']['runpart']) {
167:             return;
168:         }
169: 
170:         $func = static::compile($context, $context['usedPartial'][$name], $name);
171: 
172:         if (!isset($context['partialCode'][$name]) && $func) {
173:             $context['partialCode'][$name] = "'$name' => $func";
174:         }
175: 
176:         return $func;
177:     }
178: 
179:     /**
180:      * compile a template into a closure function
181:      *
182:      * @param array<string,array|string|integer> $context Current context of compiler progress.
183:      * @param string $template template string
184:      * @param string|integer $name partial name or 0
185:      *
186:      * @return string $code compiled PHP code
187:      */
188:     public static function compile(&$context, $template, $name = 0)
189:     {
190:         if ((end($context['partialStack']) === $name) && (substr($name, 0, 14) === '@partial-block')) {
191:             return;
192:         }
193: 
194:         $tmpContext = $context;
195:         $tmpContext['inlinepartial'] = array();
196:         $tmpContext['partialblock'] = array();
197: 
198:         if ($name !== 0) {
199:             $tmpContext['partialStack'][] = $name;
200:         }
201: 
202:         $code = Compiler::compileTemplate($tmpContext, str_replace('function', static::$TMP_JS_FUNCTION_STR, $template));
203:         Context::merge($context, $tmpContext);
204:         if (!$context['flags']['noind']) {
205:             $sp = ', $sp';
206:             $code = preg_replace('/^/m', "'{$context['ops']['seperator']}\$sp{$context['ops']['seperator']}'", $code);
207:             // callbacks inside partial should be aware of $sp
208:             $code = preg_replace('/\bfunction\s*\(([^\(]*?)\)\s*{/', 'function(\\1)use($sp){', $code);
209:             $code = preg_replace('/function\(\$cx, \$in, \$sp\)use\(\$sp\){/', 'function($cx, $in)use($sp){', $code);
210:         } else {
211:             $sp = '';
212:         }
213:         $code = str_replace(static::$TMP_JS_FUNCTION_STR, 'function', $code);
214:         return "function (\$cx, \$in{$sp}) {{$context['ops']['array_check']}{$context['ops']['op_start']}'$code'{$context['ops']['op_end']}}";
215:     }
216: }
217: 
API documentation generated by ApiGen