Skip to content

Commit c2e3921

Browse files
committed
wip
1 parent 923f038 commit c2e3921

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

packages/view/src/ViewComponentDiscovery.php

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Tempest\Discovery\DiscoveryLocation;
1010
use Tempest\Discovery\IsDiscovery;
1111
use Tempest\Reflection\ClassReflector;
12+
use Tempest\Support\Str\ImmutableString;
1213
use Tempest\View\Components\AnonymousViewComponent;
1314

1415
use function Tempest\Support\str;
@@ -43,38 +44,47 @@ public function discoverPath(DiscoveryLocation $location, string $path): void
4344
return;
4445
}
4546

47+
$contents = str(file_get_contents($path))->ltrim();
48+
49+
if ($contents->contains('<x-component name="')) {
50+
$this->registerElementComponent(
51+
location: $location,
52+
path: $path,
53+
contents: $contents,
54+
);
55+
56+
return;
57+
}
58+
4659
$fileName = str(pathinfo($path, PATHINFO_FILENAME))->before('.');
4760

48-
$contents = str(file_get_contents($path))->ltrim();
61+
if ($fileName->startsWith('x-')) {
62+
$this->registerFileComponent(
63+
location: $location,
64+
path: $path,
65+
fileName: $fileName,
66+
contents: $contents,
67+
);
68+
}
69+
}
4970

50-
preg_match('/(?<header>(.|\n)*?)<x-component/', $contents->toString(), $header);
51-
$header = $header['header'] ?? null;
71+
private function registerElementComponent(DiscoveryLocation $location, string $path, ImmutableString $contents): void
72+
{
73+
$header = $contents
74+
->before('<x-component name="')
75+
->toString();
5276

53-
preg_match('/(.|\n)*?<x-component name="(?<name>[\w\-]+)">/', $contents->toString(), $name);
54-
$name = $name['name'] ?? null;
77+
$name = $contents
78+
->afterFirst('<x-component name="')
79+
->before('"')
80+
->toString();
5581

5682
$view = trim(preg_replace(
5783
['/^(.|\n)*?<x-component name="[\w\-]+">/', '/<\/x-component>$/'],
5884
'',
5985
$contents->toString(),
6086
) ?? '');
6187

62-
if ($fileName->startsWith('x-') && $name === null) {
63-
$this->discoveryItems->add($location, [
64-
$fileName->toString(),
65-
new AnonymousViewComponent(
66-
contents: $view ?: $contents->toString(),
67-
file: $path,
68-
),
69-
]);
70-
71-
return;
72-
}
73-
74-
if ($name === null || $header === null) {
75-
return;
76-
}
77-
7888
$this->discoveryItems->add($location, [
7989
$name,
8090
new AnonymousViewComponent(
@@ -84,6 +94,21 @@ public function discoverPath(DiscoveryLocation $location, string $path): void
8494
]);
8595
}
8696

97+
private function registerFileComponent(
98+
DiscoveryLocation $location,
99+
string $path,
100+
ImmutableString $fileName,
101+
ImmutableString $contents
102+
): void {
103+
$this->discoveryItems->add($location, [
104+
$fileName->toString(),
105+
new AnonymousViewComponent(
106+
contents: $contents->toString(),
107+
file: $path,
108+
),
109+
]);
110+
}
111+
87112
public function apply(): void
88113
{
89114
foreach ($this->discoveryItems as [$name, $viewComponent]) {

0 commit comments

Comments
 (0)