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\Support\CanBeEscapedWhenCastToString;
6
use Illuminate\Support\Str;
7
use Illuminate\View\ComponentAttributeBag;
8
 
9
trait CompilesComponents
10
{
11
    /**
12
     * The component name hash stack.
13
     *
14
     * @var array
15
     */
16
    protected static $componentHashStack = [];
17
 
18
    /**
19
     * Compile the component statements into valid PHP.
20
     *
21
     * @param  string  $expression
22
     * @return string
23
     */
24
    protected function compileComponent($expression)
25
    {
26
        [$component, $alias, $data] = str_contains($expression, ',')
27
                    ? array_map('trim', explode(',', trim($expression, '()'), 3)) + ['', '', '']
28
                    : [trim($expression, '()'), '', ''];
29
 
30
        $component = trim($component, '\'"');
31
 
32
        $hash = static::newComponentHash($component);
33
 
34
        if (Str::contains($component, ['::class', '\\'])) {
35
            return static::compileClassComponentOpening($component, $alias, $data, $hash);
36
        }
37
 
38
        return "<?php \$__env->startComponent{$expression}; ?>";
39
    }
40
 
41
    /**
42
     * Get a new component hash for a component name.
43
     *
44
     * @param  string  $component
45
     * @return string
46
     */
47
    public static function newComponentHash(string $component)
48
    {
49
        static::$componentHashStack[] = $hash = sha1($component);
50
 
51
        return $hash;
52
    }
53
 
54
    /**
55
     * Compile a class component opening.
56
     *
57
     * @param  string  $component
58
     * @param  string  $alias
59
     * @param  string  $data
60
     * @param  string  $hash
61
     * @return string
62
     */
63
    public static function compileClassComponentOpening(string $component, string $alias, string $data, string $hash)
64
    {
65
        return implode("\n", [
66
            '<?php if (isset($component)) { $__componentOriginal'.$hash.' = $component; } ?>',
67
            '<?php $component = '.$component.'::resolve('.($data ?: '[]').' + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?>',
68
            '<?php $component->withName('.$alias.'); ?>',
69
            '<?php if ($component->shouldRender()): ?>',
70
            '<?php $__env->startComponent($component->resolveView(), $component->data()); ?>',
71
        ]);
72
    }
73
 
74
    /**
75
     * Compile the end-component statements into valid PHP.
76
     *
77
     * @return string
78
     */
79
    protected function compileEndComponent()
80
    {
81
        return '<?php echo $__env->renderComponent(); ?>';
82
    }
83
 
84
    /**
85
     * Compile the end-component statements into valid PHP.
86
     *
87
     * @return string
88
     */
89
    public function compileEndComponentClass()
90
    {
91
        $hash = array_pop(static::$componentHashStack);
92
 
93
        return $this->compileEndComponent()."\n".implode("\n", [
94
            '<?php endif; ?>',
95
            '<?php if (isset($__componentOriginal'.$hash.')): ?>',
96
            '<?php $component = $__componentOriginal'.$hash.'; ?>',
97
            '<?php unset($__componentOriginal'.$hash.'); ?>',
98
            '<?php endif; ?>',
99
        ]);
100
    }
101
 
102
    /**
103
     * Compile the slot statements into valid PHP.
104
     *
105
     * @param  string  $expression
106
     * @return string
107
     */
108
    protected function compileSlot($expression)
109
    {
110
        return "<?php \$__env->slot{$expression}; ?>";
111
    }
112
 
113
    /**
114
     * Compile the end-slot statements into valid PHP.
115
     *
116
     * @return string
117
     */
118
    protected function compileEndSlot()
119
    {
120
        return '<?php $__env->endSlot(); ?>';
121
    }
122
 
123
    /**
124
     * Compile the component-first statements into valid PHP.
125
     *
126
     * @param  string  $expression
127
     * @return string
128
     */
129
    protected function compileComponentFirst($expression)
130
    {
131
        return "<?php \$__env->startComponentFirst{$expression}; ?>";
132
    }
133
 
134
    /**
135
     * Compile the end-component-first statements into valid PHP.
136
     *
137
     * @return string
138
     */
139
    protected function compileEndComponentFirst()
140
    {
141
        return $this->compileEndComponent();
142
    }
143
 
144
    /**
145
     * Compile the prop statement into valid PHP.
146
     *
147
     * @param  string  $expression
148
     * @return string
149
     */
150
    protected function compileProps($expression)
151
    {
152
        return "<?php \$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag; ?>
153
<?php foreach(\$attributes->onlyProps{$expression} as \$__key => \$__value) {
154
    \$\$__key = \$\$__key ?? \$__value;
155
} ?>
156
<?php \$attributes = \$attributes->exceptProps{$expression}; ?>
157
<?php foreach (array_filter({$expression}, 'is_string', ARRAY_FILTER_USE_KEY) as \$__key => \$__value) {
158
    \$\$__key = \$\$__key ?? \$__value;
159
} ?>
160
<?php \$__defined_vars = get_defined_vars(); ?>
161
<?php foreach (\$attributes as \$__key => \$__value) {
162
    if (array_key_exists(\$__key, \$__defined_vars)) unset(\$\$__key);
163
} ?>
164
<?php unset(\$__defined_vars); ?>";
165
    }
166
 
167
    /**
168
     * Compile the aware statement into valid PHP.
169
     *
170
     * @param  string  $expression
171
     * @return string
172
     */
173
    protected function compileAware($expression)
174
    {
175
        return "<?php foreach ({$expression} as \$__key => \$__value) {
176
    \$__consumeVariable = is_string(\$__key) ? \$__key : \$__value;
177
    \$\$__consumeVariable = is_string(\$__key) ? \$__env->getConsumableComponentData(\$__key, \$__value) : \$__env->getConsumableComponentData(\$__value);
178
} ?>";
179
    }
180
 
181
    /**
182
     * Sanitize the given component attribute value.
183
     *
184
     * @param  mixed  $value
185
     * @return mixed
186
     */
187
    public static function sanitizeComponentAttribute($value)
188
    {
189
        if ($value instanceof CanBeEscapedWhenCastToString) {
190
            return $value->escapeWhenCastingToString();
191
        }
192
 
193
        return is_string($value) ||
194
               (is_object($value) && ! $value instanceof ComponentAttributeBag && method_exists($value, '__toString'))
195
                        ? e($value)
196
                        : $value;
197
    }
198
}