| 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 | 642 | public static function create($options) { |
|
| 38 | 642 | if (!is_array($options)) { |
|
| 39 | 9 | $options = array(); |
|
| 40 | } |
||
| 41 | |||
| 42 | 642 | $flags = isset($options['flags']) ? $options['flags'] : static::FLAG_BESTPERFORMANCE; |
|
| 43 | |||
| 44 | $context = array( |
||
| 45 | 'flags' => array( |
||
| 46 | 642 | 'errorlog' => $flags & static::FLAG_ERROR_LOG, |
|
| 47 | 642 | 'exception' => $flags & static::FLAG_ERROR_EXCEPTION, |
|
| 48 | 642 | 'skippartial' => $flags & static::FLAG_ERROR_SKIPPARTIAL, |
|
| 49 | 642 | 'standalone' => $flags & static::FLAG_STANDALONEPHP, |
|
| 50 | 642 | 'bare' => $flags & static::FLAG_BARE, |
|
| 51 | 642 | 'noesc' => $flags & static::FLAG_NOESCAPE, |
|
| 52 | 642 | 'jstrue' => $flags & static::FLAG_JSTRUE, |
|
| 53 | 642 | 'jsobj' => $flags & static::FLAG_JSOBJECT, |
|
| 54 | 642 | 'hbesc' => $flags & static::FLAG_HBESCAPE, |
|
| 55 | 642 | 'this' => $flags & static::FLAG_THIS, |
|
| 56 | 642 | 'nohbh' => $flags & static::FLAG_NOHBHELPERS, |
|
| 57 | 642 | 'parent' => $flags & static::FLAG_PARENT, |
|
| 58 | 642 | 'echo' => $flags & static::FLAG_ECHO, |
|
| 59 | 642 | 'advar' => $flags & static::FLAG_ADVARNAME, |
|
| 60 | 642 | 'namev' => $flags & static::FLAG_NAMEDARG, |
|
| 61 | 642 | 'spvar' => $flags & static::FLAG_SPVARS, |
|
| 62 | 642 | 'slash' => $flags & static::FLAG_SLASH, |
|
| 63 | 642 | 'else' => $flags & static::FLAG_ELSE, |
|
| 64 | 642 | 'exhlp' => $flags & static::FLAG_EXTHELPER, |
|
| 65 | 642 | 'lambda' => $flags & static::FLAG_HANDLEBARSLAMBDA, |
|
| 66 | 642 | 'mustlok' => $flags & static::FLAG_MUSTACHELOOKUP, |
|
| 67 | 642 | 'mustlam' => $flags & static::FLAG_MUSTACHELAMBDA, |
|
| 68 | 642 | 'noind' => $flags & static::FLAG_PREVENTINDENT, |
|
| 69 | 642 | 'debug' => $flags & static::FLAG_RENDER_DEBUG, |
|
| 70 | 642 | 'prop' => $flags & static::FLAG_PROPERTY, |
|
| 71 | 642 | 'method' => $flags & static::FLAG_METHOD, |
|
| 72 | 642 | 'runpart' => $flags & static::FLAG_RUNTIMEPARTIAL, |
|
| 73 | 642 | 'rawblock' => $flags & static::FLAG_RAWBLOCK, |
|
| 74 | 642 | 'partnc' => $flags & static::FLAG_PARTIALNEWCONTEXT, |
|
| 75 | 642 | 'nostd' => $flags & static::FLAG_IGNORESTANDALONE, |
|
| 76 | 642 | ), |
|
| 77 | 642 | 'level' => 0, |
|
| 78 | 'stack' => array(), |
||
| 79 | 'currentToken' => null, |
||
| 80 | 'error' => array(), |
||
| 81 | 642 | 'basedir' => static::prepareBasedir($options), |
|
| 82 | 642 | 'fileext' => static::prepareFileext($options), |
|
| 83 | 'tokens' => array( |
||
| 84 | 'standalone' => true, |
||
| 85 | 'ahead' => false, |
||
| 86 | 'current' => 0, |
||
| 87 | 'count' => 0, |
||
| 88 | 'partialind' => '', |
||
| 89 | ), |
||
| 90 | 'usedPartial' => array(), |
||
| 91 | 'partialStack' => array(), |
||
| 92 | 642 | 'partialCode' => '', |
|
| 93 | 'usedFeature' => array( |
||
| 94 | 'rootthis' => 0, |
||
| 95 | 'enc' => 0, |
||
| 96 | 'raw' => 0, |
||
| 97 | 'sec' => 0, |
||
| 98 | 'isec' => 0, |
||
| 99 | 'if' => 0, |
||
| 100 | 'else' => 0, |
||
| 101 | 'unless' => 0, |
||
| 102 | 'each' => 0, |
||
| 103 | 'this' => 0, |
||
| 104 | 'parent' => 0, |
||
| 105 | 'with' => 0, |
||
| 106 | 'comment' => 0, |
||
| 107 | 'partial' => 0, |
||
| 108 | 'dynpartial' => 0, |
||
| 109 | 'helper' => 0, |
||
| 110 | 'bhelper' => 0, |
||
| 111 | 'hbhelper' => 0, |
||
| 112 | 'delimiter' => 0, |
||
| 113 | 'subexp' => 0, |
||
| 114 | 'rawblock' => 0, |
||
| 115 | 'lookup' => 0, |
||
| 116 | ), |
||
| 117 | 'usedCount' => array( |
||
| 118 | 'var' => array(), |
||
| 119 | 'helpers' => array(), |
||
| 120 | 'blockhelpers' => array(), |
||
| 121 | 'hbhelpers' => array(), |
||
| 122 | 'runtime' => array(), |
||
| 123 | ), |
||
| 124 | 'parsed' => array(), |
||
| 125 | 642 | 'partials' => (isset($options['partials']) && is_array($options['partials'])) ? $options['partials'] : array(), |
|
| 126 | 'helpers' => array(), |
||
| 127 | 'blockhelpers' => array(), |
||
| 128 | 'hbhelpers' => array(), |
||
| 129 | 642 | 'renderex' => isset($options['renderex']) ? $options['renderex'] : '', |
|
| 130 | 642 | 'prepartial' => (isset($options['prepartial']) && is_callable($options['prepartial'])) ? $options['prepartial'] : false, |
|
| 131 | 642 | 'runtime' => isset($options['runtime']) ? $options['runtime'] : '\\LightnCandy\\Runtime', |
|
| 132 | 'rawblock' => false, |
||
| 133 | ); |
||
| 134 | |||
| 135 | 642 | $context['ops'] = $context['flags']['echo'] ? array( |
|
| 136 | 27 | 'seperator' => ',', |
|
| 137 | 'f_start' => 'echo ', |
||
| 138 | 'f_end' => ';', |
||
| 139 | 'op_start' => 'ob_start();echo ', |
||
| 140 | 'op_end' => ';return ob_get_clean();', |
||
| 141 | 'cnd_start' => ';if ', |
||
| 142 | 'cnd_then' => '{echo ', |
||
| 143 | 'cnd_else' => ';}else{echo ', |
||
| 144 | 'cnd_end' => ';}echo ', |
||
| 145 | ) : array( |
||
| 146 | 615 | 'seperator' => '.', |
|
| 147 | 'f_start' => 'return ', |
||
| 148 | 'f_end' => ';', |
||
| 149 | 'op_start' => 'return ', |
||
| 150 | 'op_end' => ';', |
||
| 151 | 'cnd_start' => '.(', |
||
| 152 | 'cnd_then' => ' ? ', |
||
| 153 | 'cnd_else' => ' : ', |
||
| 154 | 'cnd_end' => ').', |
||
| 155 | ); |
||
| 156 | |||
| 157 | 642 | $context['ops']['enc'] = $context['flags']['hbesc'] ? 'encq' : 'enc'; |
|
| 158 | 642 | static::updateHelperTable($context, $options); |
|
| 159 | 642 | static::updateHelperTable($context, $options, 'blockhelpers'); |
|
| 160 | 642 | static::updateHelperTable($context, $options, 'hbhelpers'); |
|
| 161 | |||
| 162 | 642 | if ($context['flags']['partnc'] && ($context['flags']['runpart'] == 0)) { |
|
| 163 | $context['error'][] = 'The FLAG_PARTIALNEWCONTEXT requires FLAG_RUNTIMEPARTIAL! Fix your compile options please'; |
||
| 164 | } |
||
| 165 | |||
| 166 | 642 | return $context; |
|
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * prepare list of template file extensions from options |
||
| 171 | * |
||
| 172 | * @param array<string,array|string|integer> $options current compile option |
||
| 173 | * |
||
| 174 | * @return array<string> file extensions |
||
| 175 | * |
||
| 176 | * @expect array('.tmpl') when input array() |
||
| 177 | * @expect array('test') when input array('fileext' => 'test') |
||
| 178 | * @expect array('test1') when input array('fileext' => array('test1')) |
||
| 179 | * @expect array('test2', 'test3') when input array('fileext' => array('test2', 'test3')) |
||
| 180 | */ |
||
| 181 | 643 | protected static function prepareFileExt($options) { |
|
| 182 | 643 | $exts = isset($options['fileext']) ? $options['fileext'] : '.tmpl'; |
|
| 183 | 643 | return is_array($exts) ? $exts : array($exts); |
|
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * prepare list of base directory from options |
||
| 188 | * |
||
| 189 | * @param array<string,array|string|integer> $options current compile option |
||
| 190 | * |
||
| 191 | * @return array<string> base directories |
||
| 192 | * |
||
| 193 | * @expect array() when input array() |
||
| 194 | * @expect array() when input array('basedir' => array()) |
||
| 195 | * @expect array('src') when input array('basedir' => array('src')) |
||
| 196 | * @expect array('src') when input array('basedir' => array('src', 'dir_not_found')) |
||
| 197 | * @expect array('src', 'tests') when input array('basedir' => array('src', 'tests')) |
||
| 198 | */ |
||
| 199 | 643 | protected static function prepareBaseDir($options) { |
|
| 200 | 643 | $dirs = isset($options['basedir']) ? $options['basedir'] : 0; |
|
| 201 | 643 | $dirs = is_array($dirs) ? $dirs : array($dirs); |
|
| 202 | 643 | $ret = array(); |
|
| 203 | |||
| 204 | 643 | foreach ($dirs as $dir) { |
|
| 205 | 643 | if (is_string($dir) && is_dir($dir)) { |
|
| 206 | 643 | $ret[] = $dir; |
|
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | 643 | return $ret; |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * update specific custom helper table from options |
||
| 215 | * |
||
| 216 | * @param array<string,array|string|integer> $context prepared context |
||
| 217 | * @param array<string,array|string|integer> $options input options |
||
| 218 | * @param string $tname helper table name |
||
| 219 | * |
||
| 220 | * @return array<string,array|string|integer> context with generated helper table |
||
| 221 | * |
||
| 222 | * @expect array() when input array(), array() |
||
| 223 | * @expect array('flags' => array('exhlp' => 1)) when input array('flags' => array('exhlp' => 1)), array('helpers' => array('abc')) |
||
| 224 | * @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')) |
||
| 225 | * @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')) |
||
| 226 | * @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')) |
||
| 227 | */ |
||
| 228 | 643 | protected static function updateHelperTable(&$context, $options, $tname = 'helpers') { |
|
| 229 | 643 | if (isset($options[$tname]) && is_array($options[$tname])) { |
|
| 230 | 384 | foreach ($options[$tname] as $name => $func) { |
|
|
1 ignored issue
–
show
|
|||
| 231 | 175 | $tn = is_int($name) ? $func : $name; |
|
| 232 | 175 | if (is_callable($func)) { |
|
| 233 | 173 | $context[$tname][$tn] = $func; |
|
| 234 | } else { |
||
| 235 | 4 | if (is_array($func)) { |
|
| 236 | 1 | $context['error'][] = "I found an array in $tname with key as $name, please fix it."; |
|
| 237 | } else { |
||
| 238 | 3 | if (!$context['flags']['exhlp']) { |
|
| 239 | 175 | $context['error'][] = "You provide a custom helper named as '$tn' in options['$tname'], but the function $func() is not defined!"; |
|
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | 643 | return $context; |
|
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Merge a context into another |
||
| 250 | * |
||
| 251 | * @param array<string,array|string|integer> $context master context |
||
| 252 | * @param array<string,array|string|integer> $tmp another context will be overwrited into master context |
||
| 253 | */ |
||
| 254 | 56 | public static function merge(&$context, $tmp) { |
|
| 260 | } |
||
| 261 | |||
| 262 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.