/
LivewireTagCompiler.php
67 lines (56 loc) · 2.05 KB
/
LivewireTagCompiler.php
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
<?php
namespace Livewire;
use Illuminate\View\Compilers\ComponentTagCompiler;
class LivewireTagCompiler extends ComponentTagCompiler
{
public function compile($value)
{
return $this->compileLivewireSelfClosingTags($value);
}
protected function compileLivewireSelfClosingTags($value)
{
$pattern = "/
<
\s*
livewire\:([\w\-\:\.]*)
\s*
(?<attributes>
(?:
\s+
[\w\-:.@]+
(
=
(?:
\\\"[^\\\"]*\\\"
|
\'[^\']*\'
|
[^\'\\\"=<>]+
)
)?
)*
\s*
)
\/?>
/x";
return preg_replace_callback($pattern, function (array $matches) {
$attributes = $this->getAttributesFromAttributeString($matches['attributes']);
// Convert kebab attributes to camel-case.
$attributes = collect($attributes)->mapWithKeys(function ($value, $key) {
return [(string) str($key)->camel() => $value];
})->toArray();
if ($matches[1] === 'styles') return '@livewireStyles';
if ($matches[1] === 'scripts') return '@livewireScripts';
return $this->componentString($matches[1], $attributes);
}, $value);
}
protected function componentString(string $component, array $attributes)
{
if (isset($attributes['key'])) {
$key = $attributes['key'];
unset($attributes['key']);
return "@livewire('{$component}', [".$this->attributesToString($attributes, $escapeBound = false)."], key({$key}))";
}
return "@livewire('{$component}', [".$this->attributesToString($attributes, $escapeBound = false).'])';
}
}