Skip to content

Commit 26a67be

Browse files
committed
Added form-name and form-id to the template
1 parent 27fbae9 commit 26a67be

15 files changed

+180
-88
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
- The content of the `schema-up.stub` changed to allow for alter operation.
4444
- The stub `schema-down.stub` was renamed to `migration-schema-down.stub`.
4545
- The stub `schema-up.stub` was renamed to `migration-schema-up.stub`.
46+
- The `edit.blade.stub` has been modified
47+
- The `create.blade.stub` has been modified
4648

4749
## Command Changes
4850
The following command have been renamed

src/Commands/Bases/ResourceFileCreatorCommandBase.php

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,10 @@
33
namespace CrestApps\CodeGenerator\Commands\Bases;
44

55
use CrestApps\CodeGenerator\Commands\Bases\ResourceFileCommandBase;
6-
use CrestApps\CodeGenerator\Models\ForeignRelationship;
7-
use CrestApps\CodeGenerator\Models\Index;
86
use CrestApps\CodeGenerator\Support\Helpers;
97

108
class ResourceFileCreatorCommandBase extends ResourceFileCommandBase
119
{
12-
/**
13-
* Gets a relation collection.
14-
*
15-
* @param string $relation
16-
*
17-
* @return array
18-
*/
19-
protected function getRelation($rawRelation)
20-
{
21-
//name:assets;type:hasMany;params:App\\Models\\Asset|category_id|id,
22-
// expected string
23-
//name|type|params|field
24-
//assets|hasMany|App\\Models\\Asset,category_id,id|title
25-
26-
$parts = explode(';', $rawRelation);
27-
$collection = [];
28-
foreach ($parts as $part) {
29-
if (!str_contains($part, ':')) {
30-
continue;
31-
}
32-
33-
list($key, $value) = explode(':', $part);
34-
35-
if ($key == 'params' || str_contains($value, '|')) {
36-
$value = explode('|', $value);
37-
}
38-
39-
$collection[$key] = $value;
40-
}
41-
42-
return ForeignRelationship::get($collection);
43-
}
44-
45-
/**
46-
* Gets an index collection.
47-
*
48-
* @param string $index
49-
*
50-
* @return array
51-
*/
52-
protected function getIndex($rawIndex)
53-
{
54-
//name:some_name;columns:full_name|last_name;type:index,
55-
56-
$parts = explode(';', $rawIndex);
57-
58-
foreach ($parts as $part) {
59-
if (!str_contains($part, ':')) {
60-
continue;
61-
}
62-
63-
list($key, $value) = explode(':', $part);
64-
$collection[$key] = $value;
65-
}
66-
67-
return Index::get($collection);
68-
}
69-
7010
/**
7111
* Converts the current command's argument and options into an array.
7212
*
@@ -94,8 +34,8 @@ protected function getCommandInput()
9434
$modelName = trim($this->argument('model-name'));
9535
$filename = trim($this->option('resource-filename'));
9636
$file = $filename ? str_finish($filename, '.json') : Helpers::makeJsonFileName($modelName);
97-
$fieldNames = array_unique(Helpers::convertStringToArray($this->generatorOption('fields')));
98-
$translationFor = array_unique(Helpers::convertStringToArray($this->generatorOption('translation-for')));
37+
$translationFor = array_unique(Helpers::convertStringToArray($this->option('translation-for')));
38+
$fieldNames = array_unique(Helpers::convertStringToArray($this->option('fields')));
9939
$relations = Helpers::convertStringToArray(trim($this->option('relations')));
10040
$indexes = Helpers::convertStringToArray(trim($this->option('indexes')));
10141

src/Commands/Bases/ViewsCommandBase.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,32 @@ protected function canCreateView($file, $force, array $fields = null)
157157
return true;
158158
}
159159

160+
/**
161+
* Replace the create_form_id
162+
*
163+
* @param string $stub
164+
* @param string $name
165+
*
166+
* @return $this
167+
*/
168+
protected function replaceFormId(&$stub, $name)
169+
{
170+
return $this->replaceTemplate('form_id', $name, $stub);
171+
}
172+
173+
/**
174+
* Replace the create_form_name
175+
*
176+
* @param string $stub
177+
* @param string $name
178+
*
179+
* @return $this
180+
*/
181+
protected function replaceFormName(&$stub, $name)
182+
{
183+
return $this->replaceTemplate('form_name', $name, $stub);
184+
}
185+
160186
/**
161187
* Get the view's name of a giving file.
162188
*

src/Commands/CreateCreateViewCommand.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,34 @@ protected function handleCreateView()
5959
->replaceCommonTemplates($stub, $input, $resources->fields)
6060
->replaceFileUpload($stub, $resources->fields)
6161
->replaceModelHeader($stub, $headers)
62+
->replaceFormId($stub, $this->getFormId($input->modelName))
63+
->replaceFormName($stub, $this->getFormName($input->modelName))
6264
->createFile($destenationFile, $stub)
6365
->info('Create view was crafted successfully.');
6466
}
6567
}
68+
69+
/**
70+
* Gets te create form name
71+
*
72+
* @param string $modelName
73+
*
74+
* @return string
75+
*/
76+
protected function getFormName($modelName)
77+
{
78+
return sprintf('create_%s_form', snake_case($modelName));
79+
}
80+
81+
/**
82+
* Gets te create form id
83+
*
84+
* @param string $modelName
85+
*
86+
* @return string
87+
*/
88+
protected function getFormId($modelName)
89+
{
90+
return sprintf('create_%s_form', snake_case($modelName));
91+
}
6692
}

src/Commands/CreateEditViewCommand.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,34 @@ protected function handleCreateView()
5959
->replaceFileUpload($stub, $resources->fields)
6060
->replacePrimaryKey($stub, $this->getPrimaryKeyName($resources->fields))
6161
->replaceModelHeader($stub, $this->getHeaderFieldAccessor($resources->fields, $input->modelName))
62+
->replaceFormId($stub, $this->getFormId($input->modelName))
63+
->replaceFormName($stub, $this->getFormName($input->modelName))
6264
->createFile($destenationFile, $stub)
6365
->info('Edit view was crafted successfully.');
6466
}
6567
}
68+
69+
/**
70+
* Gets te create form name
71+
*
72+
* @param string $modelName
73+
*
74+
* @return string
75+
*/
76+
protected function getFormName($modelName)
77+
{
78+
return sprintf('edit_%s_form', snake_case($modelName));
79+
}
80+
81+
/**
82+
* Gets te create form id
83+
*
84+
* @param string $modelName
85+
*
86+
* @return string
87+
*/
88+
protected function getFormId($modelName)
89+
{
90+
return sprintf('edit_%s_form', snake_case($modelName));
91+
}
6692
}

src/Commands/CreateMappedResourcesCommand.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,12 @@ public function handle()
8181
'--form-request-directory' => $validInput->formRequestDirectory,
8282
'--with-auth' => $validInput->withAuth,
8383
'--table-name' => $validInput->table,
84-
'--fillable' => $validInput->fillable,
8584
'--primary-key' => $validInput->primaryKey,
8685
'--with-soft-delete' => $validInput->withSoftDelete,
8786
'--without-timestamps' => $validInput->withoutTimeStamps,
88-
'--relationships' => $validInput->relationships,
8987
'--with-migration' => $validInput->withMigration,
9088
'--migration-class-name' => $validInput->migrationClass,
9189
'--connection-name' => $validInput->connectionName,
92-
'--indexes' => $validInput->indexes,
93-
'--foreign-keys' => $validInput->foreignKeys,
9490
'--engine-name' => $validInput->engineName,
9591
'--layout-name' => $validInput->layoutName,
9692
'--template-name' => $validInput->template,
@@ -144,15 +140,11 @@ protected function getValidInputs(array $objects, ResourceInput $originalInput)
144140
$input->withMigration = $this->getValue($object, 'with-migration', $input->withMigration);
145141
$input->force = $this->getValue($object, 'force', $input->force);
146142
$input->modelDirectory = $this->getValue($object, 'model-directory', $input->modelDirectory);
147-
$input->fillable = $this->getValue($object, 'fillable', $input->fillable);
148143
$input->primaryKey = $this->getValue($object, 'primary-key', $input->primaryKey);
149-
$input->relationships = $this->getValue($object, 'relationships', $input->relationships);
150144
$input->withSoftDelete = $this->getValue($object, 'with-soft-delete', $input->withSoftDelete);
151145
$input->withoutTimeStamps = $this->getValue($object, 'without-timestamps', $input->withoutTimeStamps);
152146
$input->migrationClass = $this->getValue($object, 'migration-class-name', $input->migrationClass);
153147
$input->connectionName = $this->getValue($object, 'connection-name', $input->connectionName);
154-
$input->indexes = $this->getValue($object, 'indexes', $input->indexes);
155-
$input->foreignKeys = $this->getValue($object, 'foreign-keys', $input->foreignKeys);
156148
$input->engineName = $this->getValue($object, 'engine-name', $input->engineName);
157149
$input->template = $this->getValue($object, 'template-name', $input->template);
158150
$input->layoutName = $this->getValue($object, 'layout-name', $input->layoutName);
@@ -161,9 +153,6 @@ protected function getValidInputs(array $objects, ResourceInput $originalInput)
161153
$input->withAuth = $this->getValue($object, 'with-auth', $input->withAuth);
162154
$input->formRequestDirectory = $this->getValue($object, 'form-request-directory', $input->formRequestDirectory);
163155

164-
$fields = $this->getFields($input->fields, $input->languageFileName, $input->resourceFile);
165-
166-
$this->validateField($fields);
167156
$validInputs[] = $input;
168157
}
169158

src/Commands/ResourceFileAppendCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CrestApps\CodeGenerator\Commands;
44

55
use CrestApps\CodeGenerator\Commands\Bases\ResourceFileCreatorCommandBase;
6+
use CrestApps\CodeGenerator\Models\ForeignRelationship;
67
use CrestApps\CodeGenerator\Models\Index;
78
use CrestApps\CodeGenerator\Models\Resource;
89
use CrestApps\CodeGenerator\Support\Helpers;
@@ -121,7 +122,7 @@ protected function mergeRelations(&$resource, $input)
121122
}
122123
$mergeRelations = 0;
123124
foreach ($input->relations as $relation) {
124-
$newRelation = $this->getRelation($relation);
125+
$newRelation = ForeignRelationship::fromString($relation);
125126

126127
if (is_null($newRelation)) {
127128
continue;
@@ -157,7 +158,7 @@ protected function mergeIndexes(&$resource, $input)
157158
}
158159
$mergeIndexes = 0;
159160
foreach ($input->indexes as $index) {
160-
$newIndex = $this->getIndex($index);
161+
$newIndex = Index::fromString($index);
161162
if (!empty($newIndex->getName()) && in_array($newIndex->getName(), $existingNames)) {
162163
$this->warn('The index "' . $newIndex->getName() . '" already exists in the file.');
163164
continue;

src/Commands/ResourceFileCreateCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace CrestApps\CodeGenerator\Commands;
44

55
use CrestApps\CodeGenerator\Commands\Bases\ResourceFileCreatorCommandBase;
6+
use CrestApps\CodeGenerator\Models\ForeignRelationship;
7+
use CrestApps\CodeGenerator\Models\Index;
68
use CrestApps\CodeGenerator\Models\Resource;
79
use CrestApps\CodeGenerator\Support\Config;
810
use CrestApps\CodeGenerator\Support\Helpers;
@@ -79,7 +81,7 @@ protected function getRelations($relations)
7981
$existingNames = [];
8082
$finalRelations = [];
8183
foreach ($relations as $relation) {
82-
$newRelation = $this->getRelation($relation);
84+
$newRelation = ForeignRelationship::fromString($relation);
8385
if (is_null($newRelation)) {
8486
continue;
8587
}
@@ -107,7 +109,7 @@ protected function getIndexes($indexes)
107109
$existingNames = [];
108110
$finalIndexes = [];
109111
foreach ($indexes as $index) {
110-
$newIndex = $this->getIndex($index);
112+
$newIndex = Index::fromString($index);
111113
if (!empty($newIndex->getName()) && in_array($newIndex->getName(), $existingNames)) {
112114
continue;
113115
}

src/Models/ForeignRelationship.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,21 @@ private function getForeignModelInstance()
322322
}
323323
}
324324

325+
/**
326+
* Gets the relation in an array format.
327+
*
328+
* @return array
329+
*/
330+
public function toArray()
331+
{
332+
return [
333+
'name' => $this->name,
334+
'type' => $this->getType(),
335+
'params' => $this->parameters,
336+
'field' => $this->getField(),
337+
];
338+
}
339+
325340
/**
326341
* Get a foreign relationship from giving array
327342
*
@@ -346,17 +361,36 @@ public static function get(array $options)
346361
}
347362

348363
/**
349-
* Gets the relation in an array format.
364+
* Get a foreign relationship from giving string
350365
*
351-
* @return array
366+
* @param string $rawRelation
367+
*
368+
* @return null | CrestApps\CodeGenerator\Model\ForeignRelationship
352369
*/
353-
public function toArray()
370+
public static function fromString($rawRelation)
354371
{
355-
return [
356-
'name' => $this->name,
357-
'type' => $this->getType(),
358-
'params' => $this->parameters,
359-
'field' => $this->getField(),
360-
];
372+
//name:assets;type:hasMany;params:App\\Models\\Asset|category_id|id,
373+
// expected string
374+
//name|type|params|field
375+
//assets|hasMany|App\\Models\\Asset,category_id,id|title
376+
377+
$parts = explode(';', $rawRelation);
378+
$collection = [];
379+
foreach ($parts as $part) {
380+
if (!str_contains($part, ':')) {
381+
continue;
382+
}
383+
384+
list($key, $value) = explode(':', $part);
385+
386+
if ($key == 'params' || str_contains($value, '|')) {
387+
$value = explode('|', $value);
388+
}
389+
390+
$collection[$key] = $value;
391+
}
392+
393+
return self::get($collection);
361394
}
395+
362396
}

src/Models/Index.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,28 @@ public static function get(array $properties)
248248

249249
return $index;
250250
}
251+
252+
/**
253+
* Gets an index from a raw string.
254+
*
255+
* @var CrestApps\CodeGenerator\Models\Index
256+
*/
257+
public static function fromString($rawIndex)
258+
{
259+
// The expected string format is this
260+
//name:some_name;columns:full_name|last_name;type:index,
261+
262+
$parts = explode(';', $rawIndex);
263+
264+
foreach ($parts as $part) {
265+
if (!str_contains($part, ':')) {
266+
continue;
267+
}
268+
269+
list($key, $value) = explode(':', $part);
270+
$collection[$key] = $value;
271+
}
272+
273+
return self::get($collection);
274+
}
251275
}

0 commit comments

Comments
 (0)