@@ -12,6 +12,7 @@ class MakeCommand extends Command
12
12
{
13
13
protected $ parser ;
14
14
protected $ model = null ;
15
+ protected $ viewPath = null ;
15
16
/**
16
17
* The name and signature of the console command.
17
18
*
@@ -20,7 +21,8 @@ class MakeCommand extends Command
20
21
protected $ signature = 'make:table
21
22
{name : The name of your Livewire class}
22
23
{model? : The name of the model you want to use in this table }
23
- {--force} ' ;
24
+ {--V|view : We will generate a row view for you}
25
+ {--force } ' ;
24
26
25
27
/**
26
28
* The console command description.
@@ -48,6 +50,7 @@ public function handle()
48
50
$ this ->model = Str::studly ($ this ->argument ('model ' ));
49
51
$ force = $ this ->option ('force ' );
50
52
53
+ $ this ->viewPath = $ this ->createView ($ force );
51
54
$ this ->createClass ($ force );
52
55
53
56
$ this ->info ('Livewire Datatable Created: ' . $ this ->parser ->className ());
@@ -71,6 +74,26 @@ protected function createClass($force = false)
71
74
return $ classPath ;
72
75
}
73
76
77
+ protected function createView ($ force = false )
78
+ {
79
+ if (! $ this ->option ('view ' )) {
80
+ return null ;
81
+ }
82
+ $ 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" );
85
+ $ this ->line ("<fg=red;options=bold>View already exists:</> {$ viewPath }" );
86
+
87
+ return false ;
88
+ }
89
+
90
+ $ this ->ensureDirectoryExists ($ viewPath );
91
+
92
+ File::put ($ viewPath , $ this ->viewContents ());
93
+
94
+ return $ viewPath ;
95
+ }
96
+
74
97
protected function ensureDirectoryExists ($ path )
75
98
{
76
99
if (! File::isDirectory (dirname ($ path ))) {
@@ -83,20 +106,38 @@ public function classContents()
83
106
if ($ this ->model ) {
84
107
$ template = file_get_contents (__DIR__ .DIRECTORY_SEPARATOR .'table-with-model.stub ' );
85
108
86
- return preg_replace_array (
109
+ $ contents = preg_replace_array (
87
110
['/\[namespace\]/ ' , '/\[class\]/ ' , '/\[model\]/ ' , '/\[model_import\]/ ' ],
88
111
[$ this ->parser ->classNamespace (), $ this ->parser ->className (), $ this ->model , $ this ->getModelImport ()],
89
112
$ template
90
113
);
91
114
} else {
92
115
$ template = file_get_contents (__DIR__ .DIRECTORY_SEPARATOR .'table.stub ' );
93
116
94
- return preg_replace_array (
117
+ $ contents = preg_replace_array (
95
118
['/\[namespace\]/ ' , '/\[class\]/ ' ],
96
119
[$ this ->parser ->classNamespace (), $ this ->parser ->className ()],
97
120
$ template
98
121
);
99
122
}
123
+
124
+ if ($ this ->viewPath ) {
125
+ $ viewPath = Str::after ($ this ->viewPath , 'resources/views/ ' );
126
+ $ contents = Str::replaceLast ("} \n" , "
127
+ public function rowView(): string
128
+ {
129
+ return ' " . $ viewPath . "';
130
+ }
131
+ } \n" ,
132
+ $ contents );
133
+ }
134
+
135
+ return $ contents ;
136
+ }
137
+
138
+ public function viewContents ()
139
+ {
140
+ return file_get_contents (__DIR__ .DIRECTORY_SEPARATOR .'view.stub ' );
100
141
}
101
142
102
143
public function getModelImport ()
0 commit comments