DynamicComponent.php
4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Illuminate\View;
use Illuminate\Container\Container;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\ComponentTagCompiler;
class DynamicComponent extends Component
{
/**
* The name of the component.
*
* @var string
*/
public $component;
/**
* The component tag compiler instance.
*
* @var \Illuminate\View\Compilers\BladeTagCompiler
*/
protected static $compiler;
/**
* The cached component classes.
*
* @var array
*/
protected static $componentClasses = [];
/**
* Create a new component instance.
*
* @param string $component
* @return void
*/
public function __construct(string $component)
{
$this->component = $component;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
$template = <<<'EOF'
<?php extract(collect($attributes->getAttributes())->mapWithKeys(function ($value, $key) { return [Illuminate\Support\Str::camel(str_replace([':', '.'], ' ', $key)) => $value]; })->all(), EXTR_SKIP); ?>
{{ props }}
<x-{{ component }} {{ bindings }} {{ attributes }}>
{{ slots }}
{{ defaultSlot }}
</x-{{ component }}>
EOF;
return function ($data) use ($template) {
$bindings = $this->bindings($class = $this->classForComponent());
return str_replace(
[
'{{ component }}',
'{{ props }}',
'{{ bindings }}',
'{{ attributes }}',
'{{ slots }}',
'{{ defaultSlot }}',
],
[
$this->component,
$this->compileProps($bindings),
$this->compileBindings($bindings),
class_exists($class) ? '{{ $attributes }}' : '',
$this->compileSlots($data['__laravel_slots']),
'{{ $slot ?? "" }}',
],
$template
);
};
}
/**
* Compile the @props directive for the component.
*
* @param array $bindings
* @return string
*/
protected function compileProps(array $bindings)
{
if (empty($bindings)) {
return '';
}
return '@props('.'[\''.implode('\',\'', collect($bindings)->map(function ($dataKey) {
return Str::camel($dataKey);
})->all()).'\']'.')';
}
/**
* Compile the bindings for the component.
*
* @param array $bindings
* @return string
*/
protected function compileBindings(array $bindings)
{
return collect($bindings)->map(function ($key) {
return ':'.$key.'="$'.Str::camel(str_replace([':', '.'], ' ', $key)).'"';
})->implode(' ');
}
/**
* Compile the slots for the component.
*
* @param array $slots
* @return string
*/
protected function compileSlots(array $slots)
{
return collect($slots)->map(function ($slot, $name) {
return $name === '__default' ? null : '<x-slot name="'.$name.'" '.((string) $slot->attributes).'>{{ $'.$name.' }}</x-slot>';
})->filter()->implode(PHP_EOL);
}
/**
* Get the class for the current component.
*
* @return string
*/
protected function classForComponent()
{
if (isset(static::$componentClasses[$this->component])) {
return static::$componentClasses[$this->component];
}
return static::$componentClasses[$this->component] =
$this->compiler()->componentClass($this->component);
}
/**
* Get the names of the variables that should be bound to the component.
*
* @param string $class
* @return array
*/
protected function bindings(string $class)
{
[$data, $attributes] = $this->compiler()->partitionDataAndAttributes($class, $this->attributes->getAttributes());
return array_keys($data->all());
}
/**
* Get an instance of the Blade tag compiler.
*
* @return \Illuminate\View\Compilers\ComponentTagCompiler
*/
protected function compiler()
{
if (! static::$compiler) {
static::$compiler = new ComponentTagCompiler(
Container::getInstance()->make('blade.compiler')->getClassComponentAliases(),
Container::getInstance()->make('blade.compiler')->getClassComponentNamespaces(),
Container::getInstance()->make('blade.compiler')
);
}
return static::$compiler;
}
}