4
4
5
5
use Illuminate \Support \Facades \File ;
6
6
use Illuminate \Support \Str ;
7
+ use Nwidart \Modules \Helpers \Path ;
7
8
8
9
trait LivewireComponentParser
9
10
{
@@ -15,6 +16,8 @@ trait LivewireComponentParser
15
16
16
17
protected $ directories ;
17
18
19
+ protected $ file ;
20
+
18
21
protected function parser (): self |bool
19
22
{
20
23
if (! $ module = $ this ->getModule ()) {
@@ -23,9 +26,10 @@ protected function parser(): self|bool
23
26
24
27
$ this ->module = $ module ;
25
28
26
- $ this ->directories = collect (
27
- preg_split ('/[.\/( \\\\)]+/ ' , $ this ->argument ('component ' ))
28
- )->map ([Str::class, 'studly ' ]);
29
+ $ this ->file = Path::studly ($ this ->argument ('component ' ));
30
+
31
+ $ this ->directories = collect (preg_split ('/[.\/( \\\\)]+/ ' , Path::directory ($ this ->argument ('component ' ))))
32
+ ->map ([Str::class, 'studly ' ]);
29
33
30
34
$ this ->component = $ this ->getComponent ();
31
35
@@ -43,50 +47,36 @@ protected function getComponent()
43
47
44
48
protected function class ()
45
49
{
46
- $ modulePath = $ this ->getModulePath (true );
47
-
48
- $ moduleLivewireNamespace = $ this ->getModuleLivewireNamespace ();
49
-
50
- $ classDir = (string ) Str::of ($ modulePath )
51
- ->append ('/ ' .$ moduleLivewireNamespace )
52
- ->replace (['\\' ], '/ ' );
53
-
54
- $ classPath = $ this ->directories ->implode ('/ ' );
55
-
56
- $ namespace = $ this ->getNamespace ($ classPath );
57
-
58
- $ className = $ this ->directories ->last ();
59
-
60
- $ componentTag = $ this ->getComponentTag ();
50
+ $ dir = $ this ->path ($ this ->getModulePath ($ this ->getModuleLivewirePath ())); // todo: examine app/ path handling.
51
+ $ path = $ this ->directories ->implode ('/ ' );
52
+ $ filename = Path::join ($ dir , $ this ->file );
61
53
62
54
return (object ) [
63
- 'dir ' => $ classDir ,
64
- 'path ' => $ classPath ,
65
- 'file ' => $ classDir . ' / ' . $ classPath . ' .php ' ,
66
- 'namespace ' => $ namespace ,
67
- 'name ' => $ className ,
68
- 'tag ' => $ componentTag ,
55
+ 'name ' => Path:: filename ( $ this -> file ) ,
56
+ 'path ' => $ path ,
57
+ 'namespace ' => $ this -> getNamespace ( $ path ) ,
58
+ 'file ' => "{ $ filename } .php " ,
59
+ 'dir ' => $ dir ,
60
+ 'tag ' => $ this -> getComponentTag () ,
69
61
];
70
62
}
71
63
72
64
protected function view ()
73
65
{
74
- $ moduleLivewireViewDir = $ this ->getModuleLivewireViewDir ();
75
-
76
- $ path = $ this ->directories
77
- ->map ([Str::class, 'kebab ' ])
78
- ->implode ('/ ' );
79
-
66
+ $ dir = $ this ->getModuleLivewireViewDir ();
67
+ $ path = $ this ->directories ->map ([Str::class, 'kebab ' ])->implode ('/ ' );
80
68
if ($ this ->option ('view ' )) {
81
69
$ path = strtr ($ this ->option ('view ' ), ['. ' => '/ ' ]);
82
70
}
71
+ $ file = Path::lower ($ this ->file );
72
+ $ filename = Path::join ($ dir , $ file );
83
73
84
74
return (object ) [
85
- 'dir ' => $ moduleLivewireViewDir ,
75
+ 'name ' => strtr ( $ file , [ ' / ' => ' . ' ]) ,
86
76
'path ' => $ path ,
87
- 'folder ' => Str:: after ( $ moduleLivewireViewDir , ' views/ ' ) ,
88
- 'file ' => $ moduleLivewireViewDir . ' / ' . $ path . ' .blade.php ' ,
89
- 'name ' => strtr ( $ path , [ ' / ' => ' . ' ]) ,
77
+ 'file ' => "{ $ filename } .blade.php " ,
78
+ 'folder ' => Str:: after ( $ dir , ' views/ ' ) ,
79
+ 'dir ' => $ dir ,
90
80
];
91
81
}
92
82
@@ -186,20 +176,17 @@ protected function getViewSourcePath()
186
176
187
177
protected function getComponentTag ()
188
178
{
189
- $ directoryAsView = $ this ->directories
190
- ->map ([Str::class, 'kebab ' ])
191
- ->implode ('. ' );
192
-
179
+ $ directoryAsView = Str::of ($ this ->file )->explode ('/ ' )->map ([Str::class, 'kebab ' ])->implode ('. ' );
193
180
$ tag = "<livewire: {$ this ->getModuleLowerName ()}:: {$ directoryAsView } /> " ;
194
181
195
- $ tagWithOutIndex = Str::replaceLast ('.index ' , '' , $ tag );
196
-
197
- return $ tagWithOutIndex ;
182
+ return Str::replaceLast ('.index ' , '' , $ tag );
198
183
}
199
184
200
185
protected function getComponentQuote ()
201
186
{
202
- return "The <code> {$ this ->getClassName ()}</code> livewire component is loaded from the " .($ this ->isCustomModule () ? 'custom ' : '' )."<code> {$ this ->getModuleName ()}</code> module. " ;
187
+ $ file = Str::of ($ this ->file )->explode ('/ ' )->implode (' / ' );
188
+
189
+ return "<code> {$ this ->getModuleName ()}" .($ this ->isCustomModule () ? ' (custom) ' : '' ).": {$ file }</code> " ;
203
190
}
204
191
205
192
/**
0 commit comments