8
8
9
9
class CreateControllerCommand extends GeneratorCommand
10
10
{
11
-
12
11
use CommonCommand;
13
12
14
13
/**
@@ -27,7 +26,8 @@ class CreateControllerCommand extends GeneratorCommand
27
26
{--routes-prefix= : Prefix of the route group.}
28
27
{--models-per-page=25 : The amount of models per page for index pages.}
29
28
{--lang-file-name= : The languages file name to put the labels in.}
30
- {--form-request : This will extract the validation into a request form class.}
29
+ {--with-form-request : This will extract the validation into a request form class.}
30
+ {--template-name= : The template name to use when generating the code.}
31
31
{--force : This option will override the controller if one already exists.} ' ;
32
32
33
33
/**
@@ -51,7 +51,7 @@ class CreateControllerCommand extends GeneratorCommand
51
51
*/
52
52
protected function getStub ()
53
53
{
54
- return $ this ->getStubByName ('controller ' );
54
+ return $ this ->getStubByName ('controller ' , $ this -> getTemplateName () );
55
55
}
56
56
57
57
/**
@@ -63,16 +63,14 @@ protected function getStub()
63
63
*/
64
64
protected function buildClass ($ name )
65
65
{
66
-
67
66
$ stub = $ this ->files ->get ($ this ->getStub ());
68
-
69
67
$ input = $ this ->getCommandInput ();
70
68
71
69
$ formRequestName = 'Request ' ;
72
70
73
71
if ($ input ->formRequest )
74
72
{
75
- $ stub = $ this ->getStubContent ('controller-with-form-request ' );
73
+ $ stub = $ this ->getStubContent ('controller-with-form-request ' , $ input -> template );
76
74
$ formRequestName = $ input ->formRequestName ;
77
75
$ this ->makeFormRequest ($ input );
78
76
}
@@ -84,14 +82,89 @@ protected function buildClass($name)
84
82
->replaceModelName ($ stub , $ input ->modelName )
85
83
->replaceModelFullName ($ stub , $ this ->getModelFullName ($ input ->modelDirectory , $ input ->modelName ))
86
84
->replaceRouteNames ($ stub , $ input ->modelName , $ input ->prefix )
87
- ->replaceValidationRules ($ stub , $ this ->getValdiationRules ($ fields ))
85
+ ->replaceValidationRules ($ stub , $ this ->getValidationRules ($ fields ))
88
86
->replaceFormRequestName ($ stub , $ formRequestName )
89
- ->replaceFormRequestFullName ($ stub , $ this ->getRequestsPath () . $ formRequestName )
87
+ ->replaceFormRequestFullName ($ stub , $ this ->getRequestsNamespace () . $ formRequestName )
90
88
->replacePaginationNumber ($ stub , $ input ->perPage )
89
+ ->processModelData ($ stub , $ this ->isContainMultipleAnswers ($ fields ))
91
90
->replaceFileSnippet ($ stub , $ this ->getFileReadySnippet ($ fields ))
91
+ ->replaceFileMethod ($ stub , $ this ->getUploadFileMethod ($ fields ))
92
92
->replaceClass ($ stub , $ name );
93
93
}
94
94
95
+ /**
96
+ * Gets the method code that upload files
97
+ *
98
+ * @return string
99
+ */
100
+ protected function getUploadFileMethod (array $ fields )
101
+ {
102
+ if ($ this ->isContainfile ($ fields ))
103
+ {
104
+ return $ this ->getStubContent ('controller-upload-method ' , $ this ->getTemplateName ());
105
+ }
106
+
107
+ return '' ;
108
+ }
109
+
110
+ /**
111
+ * Gets the Requests namespace
112
+ *
113
+ * @return string
114
+ */
115
+ protected function getRequestsNamespace ()
116
+ {
117
+ return ltrim (Helpers::convertSlashToBackslash (str_replace (app_path (), '' , $ this ->getRequestsPath ())), '\\' );
118
+ }
119
+
120
+ /**
121
+ * Gets the methods
122
+ *
123
+ * @return string
124
+ */
125
+ protected function getModelDataConversionMethod ()
126
+ {
127
+ return $ this ->getStubContent ('controller-request-parameters ' , $ this ->getTemplateName ());
128
+ }
129
+
130
+ /**
131
+ * Checks if a giving fields array conatins at least one multiple answers
132
+ *
133
+ * @param string $stub
134
+ * @param bool $withMultipleAnswers
135
+ *
136
+ * @return $this
137
+ */
138
+ protected function processModelData (& $ stub , $ withMultipleAnswers )
139
+ {
140
+ if ($ withMultipleAnswers )
141
+ {
142
+ $ this ->replaceModelData ($ stub , '$this->getModelData($request->all()) ' )
143
+ ->replaceModelDataMethod ($ stub , $ this ->getModelDataConversionMethod ());
144
+ } else
145
+ {
146
+ $ this ->replaceModelData ($ stub , '$request->all() ' )
147
+ ->replaceModelDataMethod ($ stub , '' );
148
+ }
149
+
150
+ return $ this ;
151
+ }
152
+
153
+ /**
154
+ * Checks if a giving fields array conatins at least one multiple answers
155
+ *
156
+ * @param array
157
+ *
158
+ * @return bool
159
+ */
160
+ protected function isContainMultipleAnswers (array $ fields )
161
+ {
162
+ $ filtered = array_filter ($ fields , function ($ field ){
163
+ return $ field ->isMultipleAnswers ;
164
+ });
165
+
166
+ return count ($ filtered ) > 0 ;
167
+ }
95
168
96
169
/**
97
170
* Calls the create:form-request command
@@ -104,10 +177,11 @@ protected function makeFormRequest($input)
104
177
{
105
178
$ this ->callSilent ('create:form-request ' ,
106
179
[
107
- 'form-request -name ' => $ input ->formRequestName ,
180
+ 'class -name ' => $ input ->formRequestName ,
108
181
'--fields ' => $ input ->fields ,
109
182
'--force ' => $ input ->force ,
110
- '--fields-file ' => $ input ->fieldsFile
183
+ '--fields-file ' => $ input ->fieldsFile ,
184
+ '--template-name ' => $ input ->template
111
185
]);
112
186
113
187
return $ this ;
@@ -150,16 +224,45 @@ protected function getCommandInput()
150
224
$ fields = trim ($ this ->option ('fields ' ));
151
225
$ fieldsFile = trim ($ this ->option ('fields-file ' ));
152
226
$ langFile = trim ($ this ->option ('lang-file-name ' )) ?: strtolower (str_plural ($ modelName ));
153
- $ formRequest = $ this ->option ('form-request ' );
227
+ $ formRequest = $ this ->option ('with- form-request ' );
154
228
155
229
$ force = $ this ->option ('force ' );
156
-
157
230
$ modelDirectory = trim ($ this ->option ('model-directory ' ));
158
-
159
231
$ formRequestName = ucfirst ($ modelName ) . 'FormRequest ' ;
232
+ $ template = $ this ->getTemplateName ();
160
233
161
234
return (object ) compact ('viewDirectory ' ,'viewName ' ,'modelName ' ,'prefix ' ,'perPage ' ,'fileSnippet ' ,'modelDirectory ' ,
162
- 'langFile ' ,'fields ' ,'formRequest ' ,'formRequestName ' ,'force ' ,'fieldsFile ' );
235
+ 'langFile ' ,'fields ' ,'formRequest ' ,'formRequestName ' ,'force ' ,'fieldsFile ' ,'template ' );
236
+ }
237
+
238
+ /**
239
+ * Replace the modelDataMethod for the given stub.
240
+ *
241
+ * @param string $stub
242
+ * @param string $method
243
+ *
244
+ * @return $this
245
+ */
246
+ protected function replaceModelDataMethod (&$ stub , $ method )
247
+ {
248
+ $ stub = str_replace ('{{modelDataMethod}} ' , $ method , $ stub );
249
+
250
+ return $ this ;
251
+ }
252
+
253
+ /**
254
+ * Replace the modelData for the given stub.
255
+ *
256
+ * @param string $stub
257
+ * @param string $method
258
+ *
259
+ * @return $this
260
+ */
261
+ protected function replaceModelData (&$ stub , $ method )
262
+ {
263
+ $ stub = str_replace ('{{modelData}} ' , $ method , $ stub );
264
+
265
+ return $ this ;
163
266
}
164
267
165
268
/**
@@ -252,6 +355,22 @@ protected function replaceFileSnippet(&$stub, $fileSnippet)
252
355
return $ this ;
253
356
}
254
357
358
+ /**
359
+ * Replace the uploadMethod for the given stub
360
+ *
361
+ * @param $stub
362
+ * @param $uploadMethod
363
+ *
364
+ * @return $this
365
+ */
366
+ protected function replaceFileMethod (&$ stub , $ uploadMethod )
367
+ {
368
+ $ stub = str_replace ('{{uploadMethod}} ' , $ uploadMethod , $ stub );
369
+
370
+ return $ this ;
371
+ }
372
+
373
+
255
374
/**
256
375
* Replace the fieldName for the given stub
257
376
*
@@ -275,9 +394,7 @@ protected function replaceFieldName(&$stub, $fieldName)
275
394
public function getNameInput ()
276
395
{
277
396
$ nameFromArrgument = Helpers::upperCaseEveyWord (trim ($ this ->argument ('controller-name ' )));
278
-
279
397
$ path = $ this ->getControllersPath ();
280
-
281
398
$ direcoty = trim ($ this ->option ('controller-directory ' ));
282
399
283
400
if (!empty ($ directory ))
@@ -287,54 +404,28 @@ public function getNameInput()
287
404
288
405
return Helpers::convertSlashToBackslash ($ path . Helpers::postFixWith ($ nameFromArrgument , 'Controller ' ));
289
406
}
290
-
407
+
291
408
/**
292
- * Gets laravel ready field validation format from a giving string
409
+ * Gets the code that call the file upload method.
293
410
*
294
- * @param string $validations
411
+ * @param array $fields
295
412
*
296
413
* @return string
297
414
*/
298
- protected function getValdiationRules (array $ fields )
415
+ protected function getFileReadySnippet (array $ fields )
299
416
{
300
- $ validations = '' ;
417
+ $ code = '' ;
301
418
302
419
foreach ($ fields as $ field )
303
420
{
304
- if (! empty ( $ field ->validationRules ))
421
+ if ($ field ->isFile ( ))
305
422
{
306
- $ validations .= sprintf (" '%s' => '%s', \n " , $ field ->name , implode ('| ' , $ field ->validationRules ));
423
+ $ code = ($ code ) ?: '$this ' ;
424
+ $ code .= sprintf ("->uploadFile('%s', \$data) " , $ field ->name );
307
425
}
308
426
}
309
427
310
- return $ validations ;
428
+ return $ code != '' ? $ code . ' ; ' : $ code ;
311
429
}
312
430
313
-
314
- protected function getFileReadySnippet (array $ fields )
315
- {
316
- $ fileSnippet = '' ;
317
-
318
- foreach ($ fields as $ field )
319
- {
320
- //To Do
321
- }
322
-
323
- return $ fileSnippet ;
324
- }
325
-
326
- protected function getSnippet ()
327
- {
328
- return <<<EOD
329
- if ( \$request->hasFile('{{fieldName}}')) {
330
- \$uploadPath = public_path('/uploads/');
331
-
332
- \$extension = \$request->file('{{fieldName}}')->getClientOriginalExtension();
333
- \$fileName = rand(11111, 99999) . '.' . \$extension;
334
-
335
- \$request->file('{{fieldName}}')->move( \$uploadPath, \$fileName);
336
- \$requestData['{{fieldName}}'] = \$fileName;
337
- }
338
- EOD ;
339
- }
340
431
}
0 commit comments