2
2
3
3
namespace AndreaCivita \ApiCrudGenerator \Commands ;
4
4
5
+ use AndreaCivita \ApiCrudGenerator \Generator ;
5
6
use Illuminate \Console \Command ;
6
7
use Illuminate \Database \QueryException ;
7
- use Illuminate \Support \Facades \Artisan ;
8
8
use Illuminate \Support \Facades \DB ;
9
- use Illuminate \Support \Facades \File ;
10
9
use Illuminate \Support \Facades \Schema ;
10
+ use Illuminate \Support \Str ;
11
11
12
12
class ApiCrudGenerator extends Command
13
13
{
@@ -31,20 +31,40 @@ class ApiCrudGenerator extends Command
31
31
*/
32
32
protected $ description = 'Create CRUD operations ' ;
33
33
34
+ /**
35
+ *
36
+ * Generator support instance
37
+ *
38
+ * @var \AndreaCivita\ApiCrudGenerator\Generator
39
+ */
40
+ protected $ generator ;
41
+
42
+
43
+ /**
44
+ * The String support instance
45
+ *
46
+ * @var \Illuminate\Support\Str
47
+ */
48
+ protected $ str ;
49
+
34
50
/**
35
51
* Create a new command instance.
36
52
*
37
- * @return void
53
+ * @param Generator $generator
54
+ * @param Str $str
38
55
*/
39
- public function __construct ()
56
+ public function __construct (Generator $ generator , Str $ str )
40
57
{
41
58
parent ::__construct ();
59
+ $ this ->generator = $ generator ;
60
+ $ this ->str = $ str ;
42
61
}
43
62
44
63
/**
45
64
* Execute the console command.
46
65
*
47
66
* @return mixed
67
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
48
68
*/
49
69
public function handle ()
50
70
{
@@ -68,140 +88,10 @@ public function handle()
68
88
return 0 ;
69
89
}
70
90
71
- /**
72
- * Get the file from the stub
73
- * @param $type
74
- * @return bool|string
75
- */
76
- protected function getStub ($ type )
77
- {
78
- return file_get_contents (resource_path ("stubs/ $ type.stub " ));
79
- }
80
-
81
-
82
- /**
83
- * Generate model class from stubs
84
- * @param $name string name of model class
85
- * @param $table string name of DB table
86
- * @param $timestamps boolean set timestamps true | false
87
- */
88
- protected function model ($ name , $ table , $ timestamps )
89
- {
90
- $ table === "default " ? $ table = strtolower (str_plural ($ name )) : null ;
91
- $ timeDeclaration = 'public $timestamps = false; ' ;
92
- if ($ timestamps == "true " )
93
- $ timeDeclaration = 'public $timestamps = true; ' ;
94
- $ modelTemplate = str_replace (
95
- [
96
- '{{modelName}} ' ,
97
- '{{tableDeclaration}} ' ,
98
- '{{timestamps}} '
99
- ],
100
- [
101
- $ name ,
102
- $ table ,
103
- $ timeDeclaration ,
104
- ],
105
- $ this ->getStub ('Model ' )
106
- );
107
-
108
- if (!file_exists ($ path = app_path ('/Model ' )))
109
- mkdir ($ path , 0777 , true );
110
-
111
-
112
- file_put_contents (app_path ("Model/ {$ name }.php " ), $ modelTemplate );
113
- }
114
-
115
- /**
116
- * Create controller from controller.stub
117
- * @param $name
118
- */
119
- protected function controller ($ name )
120
- {
121
- $ controllerTemplate = str_replace (
122
- [
123
- '{{modelName}} ' ,
124
- '{{modelNamePluralLowerCase}} ' ,
125
- '{{modelNameSingularLowerCase}} '
126
- ],
127
- [
128
- $ name ,
129
- strtolower (str_plural ($ name )),
130
- strtolower ($ name )
131
- ],
132
- $ this ->getStub ('Controller ' )
133
- );
134
-
135
- file_put_contents (app_path ("/Http/Controllers/ {$ name }Controller.php " ), $ controllerTemplate );
136
- }
137
-
138
- /**
139
- * Generate Request from request.stub
140
- * @param $name
141
- */
142
- protected function request ($ name )
143
- {
144
- $ requestTemplate = str_replace (
145
- ['{{modelName}} ' ],
146
- [$ name ],
147
- $ this ->getStub ('Request ' )
148
- );
149
-
150
- if (!file_exists ($ path = app_path ('/Http/Requests ' )))
151
- mkdir ($ path , 0777 , true );
152
-
153
- file_put_contents (app_path ("/Http/Requests/ {$ name }Request.php " ), $ requestTemplate );
154
- }
155
-
156
- /**
157
- * Generate routes
158
- * @param $name
159
- */
160
- protected function routes ($ name , $ table )
161
- {
162
- $ table === "default " ? $ table = strtolower (str_plural ($ name )) : null ;
163
- $ requestTemplate = str_replace (
164
- [
165
- '{{modelName}} ' ,
166
- '{{modelNamePluralLowerCase}} ' ,
167
- '{{modelNameSingularLowerCase}} '
168
- ],
169
- [
170
- $ name ,
171
- $ table ,
172
- strtolower ($ name )
173
- ],
174
- $ this ->getStub ('Routes ' )
175
- );
176
- File::append (base_path ('routes/api.php ' ), $ requestTemplate );
177
- }
178
-
179
- /**
180
- * Generate unit test
181
- * @param $name
182
- * @param $table
183
- */
184
- protected function test ($ name , $ table )
185
- {
186
-
187
- $ testTemplate = str_replace (
188
- [
189
- '{{modelName}} ' ,
190
- '{{modelNamePluralLowerCase}} ' ,
191
- '{{modelNameSingularLowerCase}} ' ,
192
- ],
193
- [
194
- $ name ,
195
- $ table ,
196
- strtolower ($ name )
197
- ],
198
- $ this ->getStub ('Test ' )
199
- );
200
- File::append (base_path ("tests/Unit/ {$ name }Test.php " ), $ testTemplate );
201
- }
202
91
203
92
/**
204
93
* Generate CRUD in interactive mode
94
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
205
95
*/
206
96
protected function interactive ()
207
97
{
@@ -210,9 +100,9 @@ protected function interactive()
210
100
$ this ->comment ("This command will guide you through creating your CRUD " );
211
101
$ name = $ this ->ask ('What is name of your Model? ' );
212
102
$ name = ucwords ($ name );
213
- $ table = $ this ->ask ("Table name [ " . strtolower (str_plural ($ name )) . "]: " );
103
+ $ table = $ this ->ask ("Table name [ " . strtolower ($ this -> str -> plural ($ name )) . "]: " );
214
104
if ($ table == "" )
215
- $ table = str_plural ($ name );
105
+ $ table = $ this -> str -> plural ($ name );
216
106
$ table = strtolower ($ table );
217
107
$ choice = $ this ->choice ('Do your table has timestamps column? ' , ['No ' , 'Yes ' ], 0 );
218
108
$ choice === "Yes " ? $ timestamps = true : $ timestamps = false ;
@@ -224,7 +114,7 @@ protected function interactive()
224
114
$ confirm = $ this ->ask ("Press y to confirm, type N to restart " );
225
115
if ($ confirm == "y " ) {
226
116
$ this ->generate ($ name , $ table , $ timestamps );
227
- die ;
117
+ return ;
228
118
}
229
119
$ this ->error ("Aborted! " );
230
120
@@ -237,24 +127,30 @@ protected function interactive()
237
127
* @param $name string Model Name
238
128
* @param $table string Table Name
239
129
* @param $timestamps boolean
130
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
240
131
*/
241
132
protected function generate ($ name , $ table , $ timestamps )
242
133
{
243
- $ this ->controller ($ name );
134
+ $ this ->generator -> controller ($ name );
244
135
$ this ->info ("Generated Controller! " );
245
- $ this ->model ($ name , $ table , $ timestamps );
136
+ $ this ->generator -> model ($ name , $ table , $ timestamps );
246
137
$ this ->info ("Generated Model! " );
247
- $ this ->request ($ name );
138
+ $ this ->generator -> request ($ name );
248
139
$ this ->info ("Generated Request! " );
249
- $ this ->routes ($ name , $ table );
140
+ $ this ->generator ->resource ($ name );
141
+ $ this ->info ("Generated Resource! " );
142
+ $ this ->generator ->routes ($ name );
250
143
$ this ->info ("Generated routes! " );
251
- $ this ->test ($ name , $ table );
144
+ $ this ->generator ->factory ($ name );
145
+ $ this ->info ("Generated Factory! " );
146
+ $ this ->generator ->test ($ name );
252
147
$ this ->info ("Generated Test! " );
253
148
}
254
149
255
150
256
151
/**
257
152
* Handle all-db generation
153
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
258
154
*/
259
155
protected function all ()
260
156
{
@@ -264,7 +160,7 @@ protected function all()
264
160
$ this ->comment ("Generating " . $ table ->Tables_in_crud . " CRUD " );
265
161
$ columns = Schema::getColumnListing ($ table ->Tables_in_crud );
266
162
$ table = $ table ->Tables_in_crud ;
267
- $ name = ucwords (str_singular ($ table ));
163
+ $ name = ucwords ($ this -> str -> singular ($ table ));
268
164
in_array ('created_at ' , $ columns ) ? $ timestamps = true : $ timestamps = false ;
269
165
$ this ->generate ($ name , $ table , $ timestamps );
270
166
}
0 commit comments