99use Tempest \Discovery \DiscoveryLocation ;
1010use Tempest \Discovery \IsDiscovery ;
1111use Tempest \Reflection \ClassReflector ;
12+ use Tempest \Support \Str \ImmutableString ;
1213use Tempest \View \Components \AnonymousViewComponent ;
1314
1415use 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