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