Skip to content

Commit fb819b0

Browse files
committed
Fixes a bug with multiple resources import
1 parent d4ad695 commit fb819b0

File tree

5 files changed

+372
-63
lines changed

5 files changed

+372
-63
lines changed

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,12 @@
3434
"post-update-cmd": [
3535
"composer dump-autoload"
3636
]
37+
},
38+
"extra": {
39+
"laravel": {
40+
"providers": [
41+
"CrestApps\\CodeGenerator\\CodeGeneratorServiceProvider"
42+
]
43+
}
3744
}
3845
}

src/Commands/CreateResourcesCommand.php

Lines changed: 128 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CrestApps\CodeGenerator\Support\Helpers;
88
use CrestApps\CodeGenerator\Traits\CommonCommand;
99
use CrestApps\CodeGenerator\Support\Config;
10+
use CrestApps\CodeGenerator\Models\ResourceInput;
1011

1112
class CreateResourcesCommand extends Command
1213
{
@@ -81,29 +82,18 @@ public function handle()
8182
$this->createFieldsFile($input);
8283
}
8384

84-
85-
if(starts_with($input->modelName, $this->mapperPrefix)) {
85+
if (starts_with($input->modelName, $this->mapperPrefix)) {
8686
$filename = str_replace($this->mapperPrefix, '', $input->modelName);
8787

8888
$objects = json_decode(Helpers::jsonFileContent($filename));
8989

90-
if(!is_array($objects)) {
91-
throw new Exception('The mapping-file does not contain a valid array. The fields file must be in the following format model-name => fields-file-name');
90+
if (!is_array($objects)) {
91+
throw new Exception('The mapping-file does not contain a valid array.');
9292
}
9393

94-
$validInputs = [];
95-
foreach($objects as $object)
96-
{
97-
$input->modelName = $object->{'model-name'};
98-
$input->fieldsFile = $object->{'fields-file'};
99-
$input->fields = null;
100-
$fields = $this->getFields($input->fields, $input->languageFileName, $input->fieldsFile);
101-
102-
$this->validateField($fields);
103-
$validInputs[] = $input;
104-
}
94+
$validInputs = $this->getValidInputs($objects, $input);
10595

106-
foreach($validInputs as $validInput) {
96+
foreach ($validInputs as $validInput) {
10797
$this->printInfo('Scaffolding resources for ' . $validInput->modelName . '...')
10898
->createModel($validInput)
10999
->createController($validInput)
@@ -130,9 +120,88 @@ public function handle()
130120
->info('All Done!');
131121
}
132122

123+
/**
124+
* Gets valid input collection
125+
*
126+
* @param array $object
127+
* @param CrestApps\CodeGenerator\Models\ResourceInput $input
128+
*
129+
* @return array of CrestApps\CodeGenerator\Models\ResourceInput
130+
*/
131+
protected function getValidInputs(array $objects, ResourceInput $originalInput)
132+
{
133+
$validInputs = [];
134+
135+
foreach ($objects as $object) {
136+
$input = clone $originalInput;
137+
if(!isset($object->{'model-name'}) || !isset($object->{'fields-file'})) {
138+
throw new Exception('Each entry in the mapping file must a have value for model-name and fields-file');
139+
}
140+
141+
$madeupTableName = $this->makeTableName($object->{'model-name'});
142+
$controllerName = ucfirst(Helpers::postFixWith(str_plural($object->{'model-name'}), 'Controller'));
143+
$input->modelName = $object->{'model-name'};
144+
$input->fieldsFile = $object->{'fields-file'};
145+
$input->table = $this->getValue($object, 'table-name', $madeupTableName);
146+
$input->fields = null;
147+
$input->prefix = $this->getValue($object, 'routes-prefix', $madeupTableName);
148+
$input->controllerName = $this->getValue($object, 'controller-name', $controllerName);
149+
$input->languageFileName = $this->getValue($object, 'lang-file-name', $madeupTableName);
150+
$input->table = $this->getValue($object, 'table-name', $madeupTableName);
151+
$input->viewsDirectory = $this->getValue($object, 'views-directory', $input->viewsDirectory);
152+
$input->perPage = $this->getValue($object, 'models-per-page', $input->perPage);
153+
$input->formRequest = $this->getValue($object, 'with-form-request', $input->formRequest);
154+
$input->controllerDirectory = $this->getValue($object, 'controller-directory', $input->controllerDirectory);
155+
$input->controllerExtends = $this->getValue($object, 'controller-extends', $input->controllerExtends);
156+
$input->withoutMigration = $this->getValue($object, 'without-migration', $input->withoutMigration);
157+
$input->force = $this->getValue($object, 'force', $input->force);
158+
$input->modelDirectory = $this->getValue($object, 'model-directory', $input->modelDirectory);
159+
$input->fillable = $this->getValue($object, 'fillable', $input->fillable);
160+
$input->primaryKey = $this->getValue($object, 'primary-key', $input->primaryKey);
161+
$input->relationships = $this->getValue($object, 'relationships', $input->relationships);
162+
$input->withSoftDelete = $this->getValue($object, 'with-soft-delete', $input->withSoftDelete);
163+
$input->withoutTimeStamps = $this->getValue($object, 'without-timestamps', $input->withoutTimeStamps);
164+
$input->migrationClass = $this->getValue($object, 'migration-class-name', $input->migrationClass);
165+
$input->connectionName = $this->getValue($object, 'connection-name', $input->connectionName);
166+
$input->indexes = $this->getValue($object, 'indexes', $input->indexes);
167+
$input->foreignKeys = $this->getValue($object, 'foreign-keys', $input->foreignKeys);
168+
$input->engineName = $this->getValue($object, 'engine-name', $input->engineName);
169+
$input->template = $this->getValue($object, 'template-name', $input->template);
170+
$input->layoutName = $this->getValue($object, 'layout-name', $input->layoutName);
171+
$input->tableExists = $this->getValue($object, 'table-exists', $input->layoutName);
172+
$input->translationFor = $this->getValue($object, 'translation-for', $input->translationFor);
173+
$input->withAuth = $this->getValue($object, 'with-auth', $input->withAuth);
174+
175+
$fields = $this->getFields($input->fields, $input->languageFileName, $input->fieldsFile);
176+
177+
$this->validateField($fields);
178+
$validInputs[] = $input;
179+
}
180+
181+
return $validInputs;
182+
}
183+
184+
/**
185+
* Gets the value of a property of a givig object if exists.
186+
*
187+
* @param object $object
188+
* @param string $name
189+
* @param mix $default
190+
*
191+
* @return mix
192+
*/
193+
protected function getValue($object, $name, $default = null)
194+
{
195+
if (isset($object->{$name})) {
196+
return $object->{$name};
197+
}
198+
199+
return $default;
200+
}
201+
133202
/**
134203
* Prints a message
135-
*
204+
*
136205
* @param string $message
137206
*
138207
* @return $this
@@ -146,7 +215,7 @@ protected function printInfo($message)
146215

147216
/**
148217
* Ensured fields contains at least one field.
149-
*
218+
*
150219
* @param array $fields
151220
*
152221
* @return $this
@@ -162,7 +231,7 @@ protected function validateField($fields)
162231

163232
/**
164233
* Executes the command that generates a migration.
165-
*
234+
*
166235
* @param object $input
167236
*
168237
* @return $this
@@ -192,7 +261,7 @@ protected function createMigration($input)
192261

193262
/**
194263
* Executes the command that generate fields' file.
195-
*
264+
*
196265
* @param object $input
197266
*
198267
* @return $this
@@ -211,7 +280,7 @@ protected function createFieldsFile($input)
211280

212281
/**
213282
* Executes the command that generates a language files.
214-
*
283+
*
215284
* @param object $input
216285
*
217286
* @return $this
@@ -231,7 +300,7 @@ protected function createLanguage($input)
231300

232301
/**
233302
* Executes the command that generates all the views.
234-
*
303+
*
235304
* @param object $input
236305
*
237306
* @return $this
@@ -255,7 +324,7 @@ protected function createViews($input)
255324

256325
/**
257326
* Executes the command that generates the routes.
258-
*
327+
*
259328
* @param object $input
260329
*
261330
* @return $this
@@ -276,7 +345,7 @@ protected function createRoutes($input)
276345

277346
/**
278347
* Executes the command that generates the controller.
279-
*
348+
*
280349
* @param object $input
281350
* @return $this
282351
*/
@@ -305,7 +374,7 @@ protected function createController($input)
305374

306375
/**
307376
* Executes the command that generates a model.
308-
*
377+
*
309378
* @param object $input
310379
*
311380
* @return $this
@@ -338,44 +407,41 @@ protected function createModel($input)
338407
*/
339408
protected function getCommandInput()
340409
{
341-
$modelName = trim($this->argument('model-name'));
410+
$input = new ResourceInput(trim($this->argument('model-name')));
342411

343-
$madeupTableName = $this->makeTableName(str_replace($this->mapperPrefix, '', $modelName));
344-
$controllerName = trim($this->option('controller-name') ?: ucfirst(Helpers::postFixWith(str_plural($modelName), 'Controller')));
345-
$viewsDirectory = $this->option('views-directory');
346412
$prefix = $this->option('routes-prefix');
347-
$prefix = $prefix == 'model-name-as-plural' ? $madeupTableName : $prefix;
348-
$perPage = intval($this->option('models-per-page'));
349-
$fields = $this->option('fields');
350-
$fieldsFile = $this->option('fields-file');
351-
$languageFileName = $this->option('lang-file-name') ?: $madeupTableName;
352-
$formRequest = $this->option('with-form-request');
353-
$controllerDirectory = $this->option('controller-directory');
354-
$controllerExtends = $this->option('controller-extends') ?: null;
355-
$withoutMigration = $this->option('without-migration');
356-
$force = $this->option('force');
357-
$modelDirectory = $this->option('model-directory');
358-
$table = $this->option('table-name') ?: $madeupTableName;
359-
$fillable = $this->option('fillable');
360-
$primaryKey = $this->option('primary-key');
361-
$relationships = $this->option('relationships');
362-
$withSoftDelete = $this->option('with-soft-delete');
363-
$withoutTimeStamps = $this->option('without-timestamps');
364-
$migrationClass = $this->option('migration-class-name');
365-
$connectionName = $this->option('connection-name');
366-
$indexes = $this->option('indexes');
367-
$foreignKeys = $this->option('foreign-keys');
368-
$engineName = $this->option('engine-name');
369-
$template = $this->getTemplateName();
370-
$layoutName = $this->option('layout-name') ?: 'layouts.app';
371-
$tableExists = $this->option('table-exists');
372-
$translationFor = $this->option('translation-for');
373-
$withAuth = $this->option('with-auth');
413+
$madeupTableName = $this->makeTableName($input->modelName);
414+
$input->prefix = ($prefix == 'model-name-as-plural') ? $madeupTableName : $prefix;
415+
$input->languageFileName = $this->option('lang-file-name') ?: $madeupTableName;
416+
$input->table = $this->option('table-name') ?: $madeupTableName;
417+
$input->viewsDirectory = $this->option('views-directory');
418+
$madeupControllerName = ucfirst(Helpers::postFixWith(str_plural($input->modelName), 'Controller'));
419+
$input->controllerName = trim($this->option('controller-name')) ?: $madeupControllerName;
420+
$input->perPage = intval($this->option('models-per-page'));
421+
$input->fields = $this->option('fields');
422+
$input->fieldsFile = $this->option('fields-file');
423+
$input->formRequest = $this->option('with-form-request');
424+
$input->controllerDirectory = $this->option('controller-directory');
425+
$input->controllerExtends = $this->option('controller-extends') ?: null;
426+
$input->withoutMigration = $this->option('without-migration');
427+
$input->force = $this->option('force');
428+
$input->modelDirectory = $this->option('model-directory');
429+
$input->fillable = $this->option('fillable');
430+
$input->primaryKey = $this->option('primary-key');
431+
$input->relationships = $this->option('relationships');
432+
$input->withSoftDelete = $this->option('with-soft-delete');
433+
$input->withoutTimeStamps = $this->option('without-timestamps');
434+
$input->migrationClass = $this->option('migration-class-name');
435+
$input->connectionName = $this->option('connection-name');
436+
$input->indexes = $this->option('indexes');
437+
$input->foreignKeys = $this->option('foreign-keys');
438+
$input->engineName = $this->option('engine-name');
439+
$input->template = $this->getTemplateName();
440+
$input->layoutName = $this->option('layout-name') ?: 'layouts.app';
441+
$input->tableExists = $this->option('table-exists');
442+
$input->translationFor = $this->option('translation-for');
443+
$input->withAuth = $this->option('with-auth');
374444

375-
return (object) compact('modelName', 'controllerName', 'viewsDirectory', 'prefix', 'perPage', 'fields', 'force',
376-
'languageFileName', 'fieldsFile', 'formRequest', 'modelDirectory', 'table', 'fillable', 'primaryKey',
377-
'relationships', 'withSoftDelete', 'withoutTimeStamps', 'controllerDirectory', 'withoutMigration',
378-
'migrationClass', 'connectionName', 'indexes', 'foreignKeys', 'engineName', 'layoutName', 'template',
379-
'tableExists', 'translationFor','controllerExtends','withAuth');
445+
return $input;
380446
}
381-
}
447+
}

src/Models/.php_cs.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"7.1.1","version":"2.3.1:v2.3.1#d5257f7433bb490299c4f300d95598fd911a8ab0","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"ForeignRelationship.php":680242575,"Field.php":1285764076,"ForeignConstraint.php":-1544160441,"ViewInput.php":-1357545504,"Label.php":-25355642,"FieldMapper.php":-245524221}}
1+
{"php":"7.1.1","version":"2.3.1:v2.3.1#d5257f7433bb490299c4f300d95598fd911a8ab0","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"ForeignRelationship.php":680242575,"Field.php":1285764076,"ForeignConstraint.php":-1544160441,"ViewInput.php":-1357545504,"Label.php":-25355642,"FieldMapper.php":-245524221,"ResourceInput.php":512355575}}

src/Models/FieldMapper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class FieldMapper
2020
*/
2121
public $meta;
2222

23+
/**
24+
* Creates a new field instance.
25+
*
26+
* @param CrestApps\CodeGenerator\Models\Field $field
27+
* @param array $meta
28+
*
29+
* @return void
30+
*/
2331
public function __construct(Field $field, array $meta = [])
2432
{
2533
$this->field = $field;

0 commit comments

Comments
 (0)