Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\View\Compilers\Concerns;
4
 
5
use Illuminate\Contracts\View\ViewCompilationException;
6
 
7
trait CompilesLoops
8
{
9
    /**
10
     * Counter to keep track of nested forelse statements.
11
     *
12
     * @var int
13
     */
14
    protected $forElseCounter = 0;
15
 
16
    /**
17
     * Compile the for-else statements into valid PHP.
18
     *
19
     * @param  string  $expression
20
     * @return string
21
     *
22
     * @throws \Illuminate\Contracts\View\ViewCompilationException
23
     */
24
    protected function compileForelse($expression)
25
    {
26
        $empty = '$__empty_'.++$this->forElseCounter;
27
 
28
        preg_match('/\( *(.+) +as +(.+)\)$/is', $expression ?? '', $matches);
29
 
30
        if (count($matches) === 0) {
31
            throw new ViewCompilationException('Malformed @forelse statement.');
32
        }
33
 
34
        $iteratee = trim($matches[1]);
35
 
36
        $iteration = trim($matches[2]);
37
 
38
        $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";
39
 
40
        $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';
41
 
42
        return "<?php {$empty} = true; {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} {$empty} = false; ?>";
43
    }
44
 
45
    /**
46
     * Compile the for-else-empty and empty statements into valid PHP.
47
     *
48
     * @param  string  $expression
49
     * @return string
50
     */
51
    protected function compileEmpty($expression)
52
    {
53
        if ($expression) {
54
            return "<?php if(empty{$expression}): ?>";
55
        }
56
 
57
        $empty = '$__empty_'.$this->forElseCounter--;
58
 
59
        return "<?php endforeach; \$__env->popLoop(); \$loop = \$__env->getLastLoop(); if ({$empty}): ?>";
60
    }
61
 
62
    /**
63
     * Compile the end-for-else statements into valid PHP.
64
     *
65
     * @return string
66
     */
67
    protected function compileEndforelse()
68
    {
69
        return '<?php endif; ?>';
70
    }
71
 
72
    /**
73
     * Compile the end-empty statements into valid PHP.
74
     *
75
     * @return string
76
     */
77
    protected function compileEndEmpty()
78
    {
79
        return '<?php endif; ?>';
80
    }
81
 
82
    /**
83
     * Compile the for statements into valid PHP.
84
     *
85
     * @param  string  $expression
86
     * @return string
87
     */
88
    protected function compileFor($expression)
89
    {
90
        return "<?php for{$expression}: ?>";
91
    }
92
 
93
    /**
94
     * Compile the for-each statements into valid PHP.
95
     *
96
     * @param  string  $expression
97
     * @return string
98
     *
99
     * @throws \Illuminate\Contracts\View\ViewCompilationException
100
     */
101
    protected function compileForeach($expression)
102
    {
103
        preg_match('/\( *(.+) +as +(.*)\)$/is', $expression ?? '', $matches);
104
 
105
        if (count($matches) === 0) {
106
            throw new ViewCompilationException('Malformed @foreach statement.');
107
        }
108
 
109
        $iteratee = trim($matches[1]);
110
 
111
        $iteration = trim($matches[2]);
112
 
113
        $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";
114
 
115
        $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';
116
 
117
        return "<?php {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} ?>";
118
    }
119
 
120
    /**
121
     * Compile the break statements into valid PHP.
122
     *
123
     * @param  string  $expression
124
     * @return string
125
     */
126
    protected function compileBreak($expression)
127
    {
128
        if ($expression) {
129
            preg_match('/\(\s*(-?\d+)\s*\)$/', $expression, $matches);
130
 
131
            return $matches ? '<?php break '.max(1, $matches[1]).'; ?>' : "<?php if{$expression} break; ?>";
132
        }
133
 
134
        return '<?php break; ?>';
135
    }
136
 
137
    /**
138
     * Compile the continue statements into valid PHP.
139
     *
140
     * @param  string  $expression
141
     * @return string
142
     */
143
    protected function compileContinue($expression)
144
    {
145
        if ($expression) {
146
            preg_match('/\(\s*(-?\d+)\s*\)$/', $expression, $matches);
147
 
148
            return $matches ? '<?php continue '.max(1, $matches[1]).'; ?>' : "<?php if{$expression} continue; ?>";
149
        }
150
 
151
        return '<?php continue; ?>';
152
    }
153
 
154
    /**
155
     * Compile the end-for statements into valid PHP.
156
     *
157
     * @return string
158
     */
159
    protected function compileEndfor()
160
    {
161
        return '<?php endfor; ?>';
162
    }
163
 
164
    /**
165
     * Compile the end-for-each statements into valid PHP.
166
     *
167
     * @return string
168
     */
169
    protected function compileEndforeach()
170
    {
171
        return '<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>';
172
    }
173
 
174
    /**
175
     * Compile the while statements into valid PHP.
176
     *
177
     * @param  string  $expression
178
     * @return string
179
     */
180
    protected function compileWhile($expression)
181
    {
182
        return "<?php while{$expression}: ?>";
183
    }
184
 
185
    /**
186
     * Compile the end-while statements into valid PHP.
187
     *
188
     * @return string
189
     */
190
    protected function compileEndwhile()
191
    {
192
        return '<?php endwhile; ?>';
193
    }
194
}