4
4
5
5
use Illuminate \Console \Command ;
6
6
use Illuminate \Support \Facades \File ;
7
+ use Illuminate \Support \Str ;
7
8
use Livewire \Commands \ComponentParser ;
8
9
use Livewire \Commands \MakeCommand as LivewireMakeCommand ;
9
10
10
11
class MakeCommand extends Command
11
12
{
12
13
protected $ parser ;
14
+ protected $ model = null ;
13
15
/**
14
16
* The name and signature of the console command.
15
17
*
16
18
* @var string
17
19
*/
18
- protected $ signature = 'make:table {name} {--force} ' ;
20
+ protected $ signature = 'make:table
21
+ {name : The name of your Livewire class}
22
+ {model? : The name of the model you want to use in this table }
23
+ {--force} ' ;
19
24
20
25
/**
21
26
* The console command description.
@@ -40,6 +45,7 @@ public function handle()
40
45
return ;
41
46
}
42
47
48
+ $ this ->model = Str::studly ($ this ->argument ('model ' ));
43
49
$ force = $ this ->option ('force ' );
44
50
45
51
$ this ->createClass ($ force );
@@ -74,12 +80,34 @@ protected function ensureDirectoryExists($path)
74
80
75
81
public function classContents ()
76
82
{
77
- $ template = file_get_contents (__DIR__ .DIRECTORY_SEPARATOR .'table.stub ' );
83
+ if ($ this ->model ) {
84
+ $ template = file_get_contents (__DIR__ .DIRECTORY_SEPARATOR .'table-with-model.stub ' );
85
+
86
+ return preg_replace_array (
87
+ ['/\[namespace\]/ ' , '/\[class\]/ ' , '/\[model\]/ ' , '/\[model_import\]/ ' ],
88
+ [$ this ->parser ->classNamespace (), $ this ->parser ->className (), $ this ->model , $ this ->getModelImport ()],
89
+ $ template
90
+ );
91
+ } else {
92
+ $ template = file_get_contents (__DIR__ .DIRECTORY_SEPARATOR .'table.stub ' );
93
+
94
+ return preg_replace_array (
95
+ ['/\[namespace\]/ ' , '/\[class\]/ ' ],
96
+ [$ this ->parser ->classNamespace (), $ this ->parser ->className ()],
97
+ $ template
98
+ );
99
+ }
100
+ }
78
101
79
- return preg_replace_array (
80
- ['/\[namespace\]/ ' , '/\[class\]/ ' ],
81
- [$ this ->parser ->classNamespace (), $ this ->parser ->className ()],
82
- $ template
83
- );
102
+ public function getModelImport ()
103
+ {
104
+ if (File::exists (app_path ('Models/ ' . $ this ->model . '.php ' ))) {
105
+ return 'App\Models \\' . $ this ->model ;
106
+ }
107
+ if (File::exists (app_path ($ this ->model . '.php ' ))) {
108
+ return 'App \\' . $ this ->model ;
109
+ }
110
+ $ this ->error ('Could not find path to model ' );
111
+ return 'App\Models \\' . $ this ->model ;
84
112
}
85
113
}
0 commit comments