Skip to content

Commit a0bef72

Browse files
committed
Adds the ability to generate multiple resources at once using a mapping-file
1 parent a7cfa94 commit a0bef72

File tree

5 files changed

+121
-16
lines changed

5 files changed

+121
-16
lines changed

config/codegenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@
165165
[
166166
'match' => ['*_at'],
167167
'set' => [
168-
'data-type' => 'dateTime'
168+
'data-type' => 'datetime'
169169
]
170170
],
171171
[
172172
'match' => ['created_at','updated_at','deleted_at'],
173173
'set' => [
174-
'data-type' => 'dateTime',
174+
'data-type' => 'datetime',
175175
'is-on-form' => false,
176176
'is-on-index' => false,
177177
'is-on-show' => true,

src/Commands/CreateResourcesCommand.php

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,48 @@ public function handle()
7474
$this->createFieldsFile($input);
7575
}
7676

77-
$fields = $this->getFields($input->fields, $input->languageFileName, $input->fieldsFile);
7877

79-
if (empty($fields) || !isset($fields[0])) {
80-
throw new Exception('You must provide at least one field to generate the views!');
78+
if(starts_with($input->modelName, 'mapping-file=')) {
79+
$filename = str_replace('mapping-file=', '', $input->modelName);
80+
81+
$objects = json_decode(Helpers::jsonFileContent($filename));
82+
83+
if(!is_array($objects)) {
84+
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');
85+
}
86+
87+
$validInputs = [];
88+
foreach($objects as $object)
89+
{
90+
$input->modelName = $object->{'model-name'};
91+
$input->fieldsFile = $object->{'fields-file'};
92+
$input->fields = null;
93+
$fields = $this->getFields($input->fields, $input->languageFileName, $input->fieldsFile);
94+
95+
$this->validateField($fields);
96+
$validInputs[] = $input;
97+
}
98+
99+
100+
foreach($validInputs as $validInput) {
101+
$this->printInfo('Scaffolding resources for ' . $validInput->modelName . '...')
102+
->createModel($validInput)
103+
->createController($validInput)
104+
->createRoutes($validInput)
105+
->createViews($validInput)
106+
->createLanguage($validInput)
107+
->createMigration($validInput)
108+
->info('---------------------------------');
109+
}
110+
111+
return $this->printInfo('All Done!');
81112
}
82113

83-
$this->info('Scaffolding...');
114+
$fields = $this->getFields($input->fields, $input->languageFileName, $input->fieldsFile);
84115

85-
$this->createModel($input)
116+
$this->validateField($fields)
117+
->printInfo('Scaffolding...')
118+
->createModel($input)
86119
->createController($input)
87120
->createRoutes($input)
88121
->createViews($input)
@@ -91,6 +124,36 @@ public function handle()
91124
->info('All Done!');
92125
}
93126

127+
/**
128+
* Prints a message
129+
*
130+
* @param string $message
131+
*
132+
* @return $this
133+
*/
134+
protected function printInfo($message)
135+
{
136+
$this->info($message);
137+
138+
return $this;
139+
}
140+
141+
/**
142+
* Ensured fields contains at least one field.
143+
*
144+
* @param array $fields
145+
*
146+
* @return $this
147+
*/
148+
protected function validateField($fields)
149+
{
150+
if (empty($fields) || !isset($fields[0])) {
151+
throw new Exception('You must provide at least one field to generate the views!');
152+
}
153+
154+
return $this;
155+
}
156+
94157
/**
95158
* Executes the command that generates a migration.
96159
*

src/Models/Field.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace CrestApps\CodeGenerator\Models;
44

5+
use App;
56
use CrestApps\CodeGenerator\Models\Label;
67
use CrestApps\CodeGenerator\Support\Helpers;
78
use CrestApps\CodeGenerator\Models\ForeignRelationhip;
8-
use App;
9+
use CrestApps\CodeGenerator\Support\Config;
910

1011
class Field
1112
{
@@ -571,6 +572,7 @@ public function isFile()
571572
*/
572573
public function toArray()
573574
{
575+
574576
return [
575577
'name' => $this->name,
576578
'labels' => $this->labelsToRaw($this->getLabels()),
@@ -582,7 +584,7 @@ public function toArray()
582584
'is-on-index' => $this->isOnIndexView,
583585
'is-on-show' => $this->isOnShowView,
584586
'is-on-form' => $this->isOnFormView,
585-
'data-type' => $this->dataType,
587+
'data-type' => $this->getRawDataType(),
586588
'data-type-params' => $this->methodParams,
587589
'data-value' => $this->dataValue,
588590
'is-index' => $this->isIndex,
@@ -608,6 +610,18 @@ public function toArray()
608610
];
609611
}
610612

613+
/**
614+
* Gets the data type in a raw format.
615+
*
616+
* @return string
617+
*/
618+
protected function getRawDataType()
619+
{
620+
$type = array_search($this->dataType, Config::dataTypeMap());
621+
622+
return $type !== false ? $type : $this->dataType;
623+
}
624+
611625
/**
612626
* Gets a relation properties.
613627
*

src/Support/Config.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ public static function getHeadersPatterns()
1616
return config('codegenerator.common_header_patterns', []);
1717
}
1818

19+
/**
20+
* Gets the path to the field files.
21+
*
22+
* @param string $file = '';
23+
*
24+
* @return string
25+
*/
26+
public static function pathToFieldFiles($file = '')
27+
{
28+
$path = config('codegenerator.fields_file_path', '');
29+
30+
return Helpers::getPathWithSlash($path) . $file;
31+
}
32+
1933
/**
2034
* Gets the common key patterns.
2135
*

src/Support/Helpers.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use File;
77
use Exception;
88
use CrestApps\CodeGenerator\Support\FieldTransformer;
9+
use CrestApps\CodeGenerator\Support\Config;
910

1011
class Helpers
1112
{
@@ -162,21 +163,34 @@ public static function getFields($fieldsLine, $langFile = 'generic')
162163
/**
163164
* Converts a string of field to an array
164165
*
165-
* @param $fieldsLine
166+
* @param string $filename
167+
* @param string $langFile
166168
*
167169
* @return array
168170
*/
169-
public static function getFieldsFromFile($fileName, $langFile = 'generic')
171+
public static function getFieldsFromFile($filename, $langFile = 'generic')
170172
{
171-
$fileFullname = self::getPathWithSlash(config('codegenerator.fields_file_path')) . $fileName;
173+
$content = self::jsonFileContent($filename);
174+
175+
return FieldTransformer::fromJson($content, $langFile);
176+
}
177+
178+
/**
179+
* Gets the content of a json file.
180+
*
181+
* @param $filename
182+
*
183+
* @return string
184+
*/
185+
public static function jsonFileContent($filename)
186+
{
187+
$fileFullname = Config::pathToFieldFiles($filename);
172188

173189
if (!File::exists($fileFullname)) {
174-
throw new Exception('the file ' . $fileFullname . ' was not found!');
190+
throw new Exception('The file ' . $fileFullname . ' was not found!');
175191
}
176192

177-
$file = File::get($fileFullname);
178-
179-
return FieldTransformer::fromJson($file, $langFile);
193+
return File::get($fileFullname);
180194
}
181195

182196
/**

0 commit comments

Comments
 (0)