|
9 | 9 | use Tempest\Discovery\DiscoveryLocation; |
10 | 10 | use Tempest\Discovery\IsDiscovery; |
11 | 11 | use Tempest\Reflection\ClassReflector; |
| 12 | +use Tempest\Support\Str\ImmutableString; |
12 | 13 | use Tempest\View\Components\AnonymousViewComponent; |
13 | 14 |
|
14 | 15 | use function Tempest\Support\str; |
@@ -43,35 +44,61 @@ public function discoverPath(DiscoveryLocation $location, string $path): void |
43 | 44 | return; |
44 | 45 | } |
45 | 46 |
|
46 | | - $fileName = str(pathinfo($path, PATHINFO_FILENAME))->before('.'); |
47 | 47 | $contents = str(file_get_contents($path))->ltrim(); |
48 | 48 |
|
49 | | - preg_match( |
50 | | - pattern: '/(?<header>(.|\n)*?)<x-component name="(?<name>[\w\-]+)">(?<view>(.|\n)*?)<\/x-component>/', |
51 | | - subject: $contents->toString(), |
52 | | - matches: $matches, |
53 | | - ); |
54 | | - |
55 | | - if ($fileName->startsWith('x-') && ! isset($matches['name'])) { |
56 | | - $this->discoveryItems->add($location, [ |
57 | | - $fileName->toString(), |
58 | | - new AnonymousViewComponent( |
59 | | - contents: $matches['view'] ?? $contents->toString(), |
60 | | - file: $path, |
61 | | - ), |
62 | | - ]); |
| 49 | + $fileName = str(pathinfo($path, PATHINFO_FILENAME))->before('.'); |
63 | 50 |
|
64 | | - return; |
| 51 | + if ($fileName->startsWith('x-')) { |
| 52 | + $this->registerFileComponent( |
| 53 | + location: $location, |
| 54 | + path: $path, |
| 55 | + fileName: $fileName, |
| 56 | + contents: $contents, |
| 57 | + ); |
| 58 | + } elseif ($contents->contains('<x-component name="')) { |
| 59 | + $this->registerElementComponent( |
| 60 | + location: $location, |
| 61 | + path: $path, |
| 62 | + contents: $contents, |
| 63 | + ); |
65 | 64 | } |
| 65 | + } |
66 | 66 |
|
67 | | - if (! isset($matches['name'], $matches['header'])) { |
68 | | - return; |
69 | | - } |
| 67 | + private function registerElementComponent(DiscoveryLocation $location, string $path, ImmutableString $contents): void |
| 68 | + { |
| 69 | + $header = $contents |
| 70 | + ->before('<x-component name="') |
| 71 | + ->toString(); |
| 72 | + |
| 73 | + $name = $contents |
| 74 | + ->afterFirst('<x-component name="') |
| 75 | + ->before('"') |
| 76 | + ->toString(); |
| 77 | + |
| 78 | + $view = $contents |
| 79 | + ->afterFirst('<x-component name="' . $name . '">') |
| 80 | + ->beforeLast('</x-component>') |
| 81 | + ->toString(); |
| 82 | + |
| 83 | + $this->discoveryItems->add($location, [ |
| 84 | + $name, |
| 85 | + new AnonymousViewComponent( |
| 86 | + contents: $header . $view, |
| 87 | + file: $path, |
| 88 | + ), |
| 89 | + ]); |
| 90 | + } |
70 | 91 |
|
| 92 | + private function registerFileComponent( |
| 93 | + DiscoveryLocation $location, |
| 94 | + string $path, |
| 95 | + ImmutableString $fileName, |
| 96 | + ImmutableString $contents, |
| 97 | + ): void { |
71 | 98 | $this->discoveryItems->add($location, [ |
72 | | - $matches['name'], |
| 99 | + $fileName->toString(), |
73 | 100 | new AnonymousViewComponent( |
74 | | - contents: $matches['header'] . $matches['view'], |
| 101 | + contents: $contents->toString(), |
75 | 102 | file: $path, |
76 | 103 | ), |
77 | 104 | ]); |
|
0 commit comments