GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 21f791...86a886 )
by Zordius
04:17
created

src/Partial.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 <[email protected]>
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 715
    public static function handleDynamic(&$context) {
36 715
        if ($context['usedFeature']['dynpartial'] == 0) {
37 709
            return;
38
        }
39
40 6
        foreach ($context['partials'] as $name => $code) {
41 6
            static::read($context, $name);
42
        }
43 6
    }
44
45
    /**
46
     * Read partial file content as string and store in context
47
     *
48
     * @param array<string,array|string|integer> $context Current context of compiler progress.
49
     * @param string $name partial name
50
     *
51
     * @return string|null $code compiled PHP code when success
52
     */
53 108
    public static function read(&$context, $name) {
54 108
        $isPB = ($name === '@partial-block');
55 108
        $context['usedFeature']['partial']++;
56
57 108
        if (isset($context['usedPartial'][$name])) {
58 25
            return;
59
        }
60
61 102
        $cnt = static::resolve($context, $name);
62
63 102
        if ($cnt !== null) {
64 91
            $context['usedPartial'][$name] = SafeString::escapeTemplate($cnt);
65 91
            return static::compileDynamic($context, $name);
66
        }
67
68 14
        if (!$context['flags']['skippartial'] && !$isPB) {
69 5
            $context['error'][] = "Can not find partial for '$name', you should provide partials or partialresolver in options";
70
        }
71 14
    }
72
73
    /**
74
     * preprocess partial template before it be stored into context
75
     *
76
     * @param array<string,array|string|integer> $context Current context of compiler progress.
77
     * @param string $tmpl partial template
78
     * @param string $name partial name
79
     *
80
     * @return string|null $content processed partial template
81
     *
82
     * @expect 'hey' when input Array('prepartial' => false), 'hey', 'haha'
83
     * @expect 'haha-hoho' when input Array('prepartial' => function ($cx, $tmpl, $name) {return "$name-$tmpl";}), 'hoho', 'haha'
84
     */
85 92
    protected static function prePartial(&$context, $tmpl, &$name) {
86 92
        return $context['prepartial'] ? $context['prepartial']($context, $tmpl, $name) : $tmpl;
87
    }
88
89
    /**
90
     * resolve partial, return the partial content
91
     *
92
     * @param array<string,array|string|integer> $context Current context of compiler progress.
93
     * @param string $name partial name
94
     *
95
     * @return string|null $content partial content
96
     */
97 110
    public static function resolve(&$context, &$name) {
98 110
        if ($name === '@partial-block') {
99 17
            $name = "@partial-block{$context['usedFeature']['pblock']}";
100
        }
101 110
        if (isset($context['partials'][$name])) {
102 90
            return static::prePartial($context, $context['partials'][$name], $name);
103
        }
104
105 25
        return static::resolver($context, $name);
106
    }
107
108
    /**
109
     * use partialresolver to resolve partial, return the partial content
110
     *
111
     * @param array<string,array|string|integer> $context Current context of compiler progress.
112
     * @param string $name partial name
113
     *
114
     * @return string|null $content partial content
115
     */
116 25
    public static function resolver(&$context, &$name) {
117 25
        if ($context['partialresolver']) {
118 1
            $cnt = $context['partialresolver']($context, $name);
119 1
            return static::prePartial($context, $cnt, $name);
120
        }
121 24
    }
122
123
    /**
124
     * compile a partial to static embed PHP code
125
     *
126
     * @param array<string,array|string|integer> $context Current context of compiler progress.
127
     * @param string $name partial name
128
     *
129
     * @return string|null $code PHP code string
130
     */
131 9
    public static function compileStatic(&$context, $name) {
132
        // Check for recursive partial
133 9
        if (!$context['flags']['runpart']) {
134 9
            $context['partialStack'][] = $name;
135 9
            $diff = count($context['partialStack']) - count(array_unique($context['partialStack']));
136 9
            if ($diff) {
137 1
                $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.';
138
            }
139
        }
140
141 9
        $code = Compiler::compileTemplate($context, preg_replace('/^/m', $context['tokens']['partialind'], $context['usedPartial'][$name]));
142
143 9
        if (!$context['flags']['runpart']) {
144 9
            array_pop($context['partialStack']);
145
        }
146
147 9
        return $code;
148
    }
149
150
    /**
151
     * compile partial as closure, stored in context
152
     *
153
     * @param array<string,array|string|integer> $context Current context of compiler progress.
154
     * @param string $name partial name
155
     *
156
     * @return string|null $code compiled PHP code when success
157
     */
158 107
    public static function compileDynamic(&$context, $name) {
159 107
        if (!$context['flags']['runpart']) {
160 11
            return;
161
        }
162
163 96
        $func = static::compile($context, $context['usedPartial'][$name], $name);
164
165 96
        if (!isset($context['partialCode'][$name]) && $func) {
166 96
            $context['partialCode'][$name] = "'$name' => $func";
167
        }
168
169 96
        return $func;
170
    }
171
172
    /**
173
     * compile a template into a closure function
174
     *
175
     * @param array<string,array|string|integer> $context Current context of compiler progress.
176
     * @param string $template template string
177
     * @param string|integer $name partial name or 0
0 ignored issues
show
Consider making the type for parameter $name a bit more specific; maybe use integer.
Loading history...
178
     *
179
     * @return string $code compiled PHP code
180
     */
181 96
    public static function compile(&$context, $template, $name = 0) {
182 96
        if ((end($context['partialStack']) === $name) && (substr($name, 0, 14) === '@partial-block')) {
183 3
            return;
184
        }
185
186 96
        $tmpContext = $context;
187 96
        $tmpContext['inlinepartial'] = array();
188 96
        $tmpContext['partialblock'] = array();
189
190 96
        if ($name !== 0) {
191 96
            $tmpContext['partialStack'][] = $name;
192
        }
193
194 96
        $code = Compiler::compileTemplate($tmpContext, str_replace('function', static::$TMP_JS_FUNCTION_STR, $template));
195 96
        Context::merge($context, $tmpContext);
196 96
        if (!$context['flags']['noind']) {
197 91
            $sp = ', $sp';
198 91
            $code = preg_replace('/^/m', "'{$context['ops']['seperator']}\$sp{$context['ops']['seperator']}'", $code);
199
            // callbacks inside partial should be aware of $sp
200 91
            $code = preg_replace('/\bfunction\s*\(([^\(]*?)\)\s*{/', 'function(\\1)use($sp){', $code);
201 91
            $code = preg_replace('/function\(\$cx, \$in, \$sp\)use\(\$sp\){/', 'function($cx, $in)use($sp){', $code);
202
        } else {
203 5
            $sp = '';
204
        }
205 96
        $code = str_replace(static::$TMP_JS_FUNCTION_STR, 'function', $code);
206 96
        return "function (\$cx, \$in{$sp}) {{$context['ops']['array_check']}{$context['ops']['op_start']}'$code'{$context['ops']['op_end']}}";
207
    }
208
}
209
210