-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
LivewireBladeDirectives.php
55 lines (47 loc) · 1.62 KB
/
LivewireBladeDirectives.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
<?php
namespace Livewire;
use Illuminate\Support\Str;
class LivewireBladeDirectives
{
public static function this()
{
return "window.livewire.find('{{ \$_instance->id }}')";
}
public static function livewireStyles($expression)
{
return '{!! \Livewire\Livewire::styles('.$expression.') !!}';
}
public static function livewireScripts($expression)
{
return '{!! \Livewire\Livewire::scripts('.$expression.') !!}';
}
public static function livewire($expression)
{
$lastArg = trim(last(explode(',', $expression)));
if (Str::startsWith($lastArg, 'key(') && Str::endsWith($lastArg, ')')) {
$cachedKey = Str::replaceFirst('key(', '', Str::replaceLast(')', '', $lastArg));
$args = explode(',', $expression);
array_pop($args);
$expression = implode(',', $args);
} else {
$cachedKey = "'".Str::random(7)."'";
}
return <<<EOT
<?php
if (! isset(\$_instance)) {
\$dom = \Livewire\Livewire::mount({$expression})->dom;
} elseif (\$_instance->childHasBeenRendered($cachedKey)) {
\$componentId = \$_instance->getRenderedChildComponentId($cachedKey);
\$componentTag = \$_instance->getRenderedChildComponentTagName($cachedKey);
\$dom = \Livewire\Livewire::dummyMount(\$componentId, \$componentTag);
\$_instance->preserveRenderedChild($cachedKey);
} else {
\$response = \Livewire\Livewire::mount({$expression});
\$dom = \$response->dom;
\$_instance->logRenderedChild($cachedKey, \$response->id, \Livewire\Livewire::getRootElementTagName(\$dom));
}
echo \$dom;
?>
EOT;
}
}