Skip to content

Commit 8187ea9

Browse files
committed
Fix multiple bugs. Adds the ability to upload files to the servers. Add the ability to change the template at run time
1 parent 016918f commit 8187ea9

38 files changed

+1162
-410
lines changed

config/codegenerator.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
/*
66
|--------------------------------------------------------------------------
7-
| Code generator path to a custom template.
7+
| The default template to use
88
|--------------------------------------------------------------------------
99
|
1010
| Here you change the stub templates to use when generating code.
11-
| You can duplicate the "base_path('resources/codegenerator-templates/default')"
11+
| You can duplicate the "default" template folder
1212
| and call it what ever template your like "ex. skyblue".
1313
| Now, you can change the stubs to have your own templates generated.
1414
|
@@ -19,7 +19,28 @@
1919
|
2020
*/
2121

22-
'template' => base_path('resources/codegenerator-templates/default'),
22+
'template' => 'default',
23+
24+
/*
25+
|--------------------------------------------------------------------------
26+
| The default path of where the templates are located
27+
|--------------------------------------------------------------------------
28+
|
29+
| In this path, you can add more templates.
30+
|
31+
*/
32+
33+
'templates_path' => base_path('resources/codegenerator-templates'),
34+
35+
/*
36+
|--------------------------------------------------------------------------
37+
| The default path of where the uploaded files lives!
38+
|--------------------------------------------------------------------------
39+
|
40+
|
41+
*/
42+
43+
'files_upload_path' => public_path('uploads'),
2344

2445
/*
2546
|--------------------------------------------------------------------------

src/commands/CreateControllerCommand.php

Lines changed: 142 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
class CreateControllerCommand extends GeneratorCommand
1010
{
11-
1211
use CommonCommand;
1312

1413
/**
@@ -27,7 +26,8 @@ class CreateControllerCommand extends GeneratorCommand
2726
{--routes-prefix= : Prefix of the route group.}
2827
{--models-per-page=25 : The amount of models per page for index pages.}
2928
{--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.}
3131
{--force : This option will override the controller if one already exists.}';
3232

3333
/**
@@ -51,7 +51,7 @@ class CreateControllerCommand extends GeneratorCommand
5151
*/
5252
protected function getStub()
5353
{
54-
return $this->getStubByName('controller');
54+
return $this->getStubByName('controller', $this->getTemplateName() );
5555
}
5656

5757
/**
@@ -63,16 +63,14 @@ protected function getStub()
6363
*/
6464
protected function buildClass($name)
6565
{
66-
6766
$stub = $this->files->get($this->getStub());
68-
6967
$input = $this->getCommandInput();
7068

7169
$formRequestName = 'Request';
7270

7371
if($input->formRequest)
7472
{
75-
$stub = $this->getStubContent('controller-with-form-request');
73+
$stub = $this->getStubContent('controller-with-form-request', $input->template);
7674
$formRequestName = $input->formRequestName;
7775
$this->makeFormRequest($input);
7876
}
@@ -84,14 +82,89 @@ protected function buildClass($name)
8482
->replaceModelName($stub, $input->modelName)
8583
->replaceModelFullName($stub, $this->getModelFullName($input->modelDirectory, $input->modelName))
8684
->replaceRouteNames($stub, $input->modelName, $input->prefix)
87-
->replaceValidationRules($stub, $this->getValdiationRules($fields))
85+
->replaceValidationRules($stub, $this->getValidationRules($fields))
8886
->replaceFormRequestName($stub, $formRequestName)
89-
->replaceFormRequestFullName($stub, $this->getRequestsPath() . $formRequestName)
87+
->replaceFormRequestFullName($stub, $this->getRequestsNamespace() . $formRequestName)
9088
->replacePaginationNumber($stub, $input->perPage)
89+
->processModelData($stub, $this->isContainMultipleAnswers($fields))
9190
->replaceFileSnippet($stub, $this->getFileReadySnippet($fields))
91+
->replaceFileMethod($stub, $this->getUploadFileMethod($fields))
9292
->replaceClass($stub, $name);
9393
}
9494

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+
}
95168

96169
/**
97170
* Calls the create:form-request command
@@ -104,10 +177,11 @@ protected function makeFormRequest($input)
104177
{
105178
$this->callSilent('create:form-request',
106179
[
107-
'form-request-name' => $input->formRequestName,
180+
'class-name' => $input->formRequestName,
108181
'--fields' => $input->fields,
109182
'--force' => $input->force,
110-
'--fields-file' => $input->fieldsFile
183+
'--fields-file' => $input->fieldsFile,
184+
'--template-name' => $input->template
111185
]);
112186

113187
return $this;
@@ -150,16 +224,45 @@ protected function getCommandInput()
150224
$fields = trim($this->option('fields'));
151225
$fieldsFile = trim($this->option('fields-file'));
152226
$langFile = trim($this->option('lang-file-name')) ?: strtolower(str_plural($modelName));
153-
$formRequest = $this->option('form-request');
227+
$formRequest = $this->option('with-form-request');
154228

155229
$force = $this->option('force');
156-
157230
$modelDirectory = trim($this->option('model-directory'));
158-
159231
$formRequestName = ucfirst($modelName) . 'FormRequest';
232+
$template = $this->getTemplateName();
160233

161234
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;
163266
}
164267

165268
/**
@@ -252,6 +355,22 @@ protected function replaceFileSnippet(&$stub, $fileSnippet)
252355
return $this;
253356
}
254357

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+
255374
/**
256375
* Replace the fieldName for the given stub
257376
*
@@ -275,9 +394,7 @@ protected function replaceFieldName(&$stub, $fieldName)
275394
public function getNameInput()
276395
{
277396
$nameFromArrgument = Helpers::upperCaseEveyWord(trim($this->argument('controller-name')));
278-
279397
$path = $this->getControllersPath();
280-
281398
$direcoty = trim($this->option('controller-directory'));
282399

283400
if(!empty($directory))
@@ -287,54 +404,28 @@ public function getNameInput()
287404

288405
return Helpers::convertSlashToBackslash($path . Helpers::postFixWith($nameFromArrgument, 'Controller'));
289406
}
290-
407+
291408
/**
292-
* Gets laravel ready field validation format from a giving string
409+
* Gets the code that call the file upload method.
293410
*
294-
* @param string $validations
411+
* @param array $fields
295412
*
296413
* @return string
297414
*/
298-
protected function getValdiationRules(array $fields)
415+
protected function getFileReadySnippet(array $fields)
299416
{
300-
$validations = '';
417+
$code = '';
301418

302419
foreach($fields as $field)
303420
{
304-
if(!empty($field->validationRules))
421+
if($field->isFile())
305422
{
306-
$validations .= sprintf(" '%s' => '%s',\n ", $field->name, implode('|', $field->validationRules));
423+
$code = ($code) ?: '$this';
424+
$code .= sprintf("->uploadFile('%s', \$data)", $field->name);
307425
}
308426
}
309427

310-
return $validations;
428+
return $code != '' ? $code . ';' : $code;
311429
}
312430

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-
}
340431
}

src/commands/CreateCreateViewCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CreateCreateViewCommand extends ViewsCommand
1919
{--views-directory= : The name of the directory to create the views under.}
2020
{--routes-prefix= : The routes prefix.}
2121
{--layout-name=layouts.app : This will extract the validation into a request form class.}
22+
{--template-name= : The template name to use when generating the code.}
2223
{--force : This option will override the view if one already exists.}';
2324

2425
/**
@@ -51,10 +52,13 @@ protected function handleCreateView()
5152
$stub = $this->getStubContent($this->stubName);
5253
$destenationFile = $this->getDestinationViewFullname($input->viewsDirectory, $input->prefix, 'create');
5354

55+
$fields = $this->getFields($input->fields, $input->languageFileName, $input->fieldsFile);
56+
5457
$this->handleNewFilePolicy($destenationFile, $input->force)
5558
->createLanguageFile($input->languageFileName, $input->fields, $input->fieldsFile)
5659
->createMissingViews($input)
5760
->replaceCommonTemplates($stub, $input)
61+
->replaceFileUpload($stub, $fields)
5862
->createViewFile($stub, $destenationFile)
5963
->info('Create view was created successfully.');
6064

src/commands/CreateEditViewCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CreateEditViewCommand extends ViewsCommand
1919
{--views-directory= : The name of the directory to create the views under.}
2020
{--routes-prefix= : The routes prefix.}
2121
{--layout-name=layouts.app : This will extract the validation into a request form class.}
22+
{--template-name= : The template name to use when generating the code.}
2223
{--force : This option will override the view if one already exists.}';
2324

2425
/**
@@ -58,6 +59,7 @@ protected function handleCreateView()
5859
->createLanguageFile($input->languageFileName, $input->fields, $input->fieldsFile)
5960
->createMissingViews($input)
6061
->replaceCommonTemplates($stub, $input)
62+
->replaceFileUpload($stub, $fields)
6163
->replacePrimaryKey($stub, $this->getPrimaryKeyName($fields))
6264
->createViewFile($stub, $destenationFile)
6365
->info('Create view was created successfully.');

0 commit comments

Comments
 (0)