8
8
use Livewire \Commands \ComponentParser ;
9
9
use Livewire \Commands \MakeCommand as LivewireMakeCommand ;
10
10
11
+ /**
12
+ * Class MakeCommand
13
+ *
14
+ * @package Rappasoft\LaravelLivewireTables\Commands
15
+ */
11
16
class MakeCommand extends Command
12
17
{
18
+
19
+ /**
20
+ * @var
21
+ */
13
22
protected $ parser ;
14
- protected $ model = null ;
15
- protected $ viewPath = null ;
23
+
24
+ /**
25
+ * @var
26
+ */
27
+ protected $ model ;
28
+
29
+ /**
30
+ * @var
31
+ */
32
+ protected $ viewPath ;
33
+
16
34
/**
17
35
* The name and signature of the console command.
18
36
*
19
37
* @var string
20
38
*/
21
- protected $ signature = 'make:table
39
+ protected $ signature = 'make:datatable
22
40
{name : The name of your Livewire class}
23
- {model? : The name of the model you want to use in this table }
41
+ {model? : The name of the model you want to use in this table}
24
42
{--view : We will generate a row view for you}
25
- {--force } ' ;
43
+ {--force} ' ;
26
44
27
45
/**
28
46
* The console command description.
29
47
*
30
48
* @var string
31
49
*/
32
- protected $ description = 'Generate a Laravel Livewire table class and view ' ;
50
+ protected $ description = 'Generate a Laravel Livewire datatable class and view ' ;
33
51
34
- public function handle ()
52
+ public function handle (): void
35
53
{
36
54
$ this ->parser = new ComponentParser (
37
55
config ('livewire.class_namespace ' ),
@@ -42,7 +60,6 @@ public function handle()
42
60
$ livewireMakeCommand = new LivewireMakeCommand ();
43
61
44
62
if ($ livewireMakeCommand ->isReservedClassName ($ name = $ this ->parser ->className ())) {
45
- $ this ->line ("<options=bold,reverse;fg=red> WHOOPS! </> 😳 \n" );
46
63
$ this ->line ("<fg=red;options=bold>Class is reserved:</> {$ name }" );
47
64
return ;
48
65
}
@@ -56,12 +73,16 @@ public function handle()
56
73
$ this ->info ('Livewire Datatable Created: ' . $ this ->parser ->className ());
57
74
}
58
75
59
- protected function createClass ($ force = false )
76
+ /**
77
+ * @param false $force
78
+ *
79
+ * @return false
80
+ */
81
+ protected function createClass (bool $ force = false )
60
82
{
61
83
$ classPath = $ this ->parser ->classPath ();
62
84
63
- if (File::exists ($ classPath ) && ! $ force ) {
64
- $ this ->line ("<options=bold,reverse;fg=red> WHOOPS-IE-TOOTLES </> 😳 \n" );
85
+ if (! $ force && File::exists ($ classPath )) {
65
86
$ this ->line ("<fg=red;options=bold>Class already exists:</> {$ this ->parser ->relativeClassPath ()}" );
66
87
67
88
return false ;
@@ -74,14 +95,20 @@ protected function createClass($force = false)
74
95
return $ classPath ;
75
96
}
76
97
98
+ /**
99
+ * @param false $force
100
+ *
101
+ * @return false|string|null
102
+ */
77
103
protected function createView ($ force = false )
78
104
{
79
105
if (! $ this ->option ('view ' )) {
80
106
return null ;
81
107
}
108
+
82
109
$ viewPath = base_path ('resources/views/livewire-tables/rows/ ' . Str::snake ($ this ->parser ->className ()->__toString ()) . '.blade.php ' );
83
- if (File:: exists ( $ viewPath ) && ! $ force ) {
84
- $ this -> line ( " <options=bold,reverse;fg=red> WHOOPS-IE-TOOTLES </> 😳 \n" );
110
+
111
+ if (! $ force && File:: exists ( $ viewPath )) {
85
112
$ this ->line ("<fg=red;options=bold>View already exists:</> {$ viewPath }" );
86
113
87
114
return false ;
@@ -94,14 +121,20 @@ protected function createView($force = false)
94
121
return $ viewPath ;
95
122
}
96
123
97
- protected function ensureDirectoryExists ($ path )
124
+ /**
125
+ * @param $path
126
+ */
127
+ protected function ensureDirectoryExists ($ path ): void
98
128
{
99
129
if (! File::isDirectory (dirname ($ path ))) {
100
- File::makeDirectory (dirname ($ path ), 0777 , $ recursive = true , $ force = true );
130
+ File::makeDirectory (dirname ($ path ), 0777 , true , true );
101
131
}
102
132
}
103
133
104
- public function classContents ()
134
+ /**
135
+ * @return string
136
+ */
137
+ public function classContents (): string
105
138
{
106
139
if ($ this ->model ) {
107
140
$ template = file_get_contents (__DIR__ .DIRECTORY_SEPARATOR .'table-with-model.stub ' );
@@ -134,25 +167,37 @@ public function rowView(): string
134
167
return $ contents ;
135
168
}
136
169
170
+ /**
171
+ * @return string
172
+ */
137
173
private function getViewPathForRowView (): string
138
174
{
139
- return Str::before (Str::after ($ this ->viewPath , 'resources/views/ ' ), '.blade.php ' );
175
+ return Str::replace ( ' / ' , ' . ' , Str:: before (Str::after ($ this ->viewPath , 'resources/views/ ' ), '.blade.php ' ) );
140
176
}
141
177
178
+ /**
179
+ * @return false|string
180
+ */
142
181
public function viewContents ()
143
182
{
144
183
return file_get_contents (__DIR__ .DIRECTORY_SEPARATOR .'view.stub ' );
145
184
}
146
185
147
- public function getModelImport ()
186
+ /**
187
+ * @return string
188
+ */
189
+ public function getModelImport (): string
148
190
{
149
191
if (File::exists (app_path ('Models/ ' . $ this ->model . '.php ' ))) {
150
192
return 'App\Models \\' . $ this ->model ;
151
193
}
194
+
152
195
if (File::exists (app_path ($ this ->model . '.php ' ))) {
153
196
return 'App \\' . $ this ->model ;
154
197
}
155
- $ this ->error ('Could not find path to model ' );
198
+
199
+ $ this ->error ('Could not find path to model. ' );
200
+
156
201
return 'App\Models \\' . $ this ->model ;
157
202
}
158
203
}
0 commit comments