Skip to content

Commit 4cbd834

Browse files
committed
Fixed #66
1 parent 78db6a8 commit 4cbd834

File tree

4 files changed

+175
-3
lines changed

4 files changed

+175
-3
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "crestapps/laravel-code-generator",
33
"license": "MIT",
44
"description": "An intelligent code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages and/or form-requests! It is extremely flexible and customizable to cover many on the use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.",
5-
"version": "v2.2.10",
5+
"version": "v2.2.11",
66
"keywords": [
77
"laravel","crud","crud generator",
88
"laravel crud generator","laravel crud builder",
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace CrestApps\CodeGenerator\Commands\Resources;
4+
5+
use CrestApps\CodeGenerator\Commands\Bases\ResourceFileCommandBase;
6+
use CrestApps\CodeGenerator\Models\ForeignRelationship;
7+
use CrestApps\CodeGenerator\Models\Index;
8+
use CrestApps\CodeGenerator\Support\Arr;
9+
use CrestApps\CodeGenerator\Support\Helpers;
10+
use CrestApps\CodeGenerator\Traits\LanguageTrait;
11+
12+
class ResourceFileRefreshCommand extends ResourceFileCommandBase
13+
{
14+
use LanguageTrait;
15+
16+
/**
17+
* The name and signature of the console command.
18+
*
19+
* @var string
20+
*/
21+
protected $signature = 'resource-file:refresh
22+
{model-name : The model name that these files represent.}
23+
{--resource-filename= : The destination file name to append too.}
24+
{--translation-for= : A comma seperated string of languages to create fields for.}';
25+
/**
26+
* The console command description.
27+
*
28+
* @var string
29+
*/
30+
protected $description = 'Create a new resource-file.';
31+
32+
/**
33+
* Executes the console command.
34+
*
35+
* @return mixed
36+
*/
37+
public function handle()
38+
{
39+
$input = $this->getCommandInput();
40+
$file = $this->getFilename($input->file);
41+
42+
if (!$this->isFileExists($file)) {
43+
$this->error('The resource-file does not exists!');
44+
45+
return false;
46+
}
47+
48+
$resource = $this->getResources($file, $input->translationFor);
49+
50+
$resource->setDefaultApiDocLabels($input->modelName, self::makeLocaleGroup($input->modelName), $input->translationFor);
51+
52+
dd($resource->toArray());
53+
54+
$content = Helpers::prettifyJson($resource->toArray());
55+
56+
$this->putContentInFile($file, $content);
57+
}
58+
59+
/**
60+
* Get the relations from an existing array.
61+
*
62+
* @param array $relations
63+
*
64+
* @return array
65+
*/
66+
protected function getRelations($relations)
67+
{
68+
$existingNames = [];
69+
$finalRelations = [];
70+
foreach ($relations as $relation) {
71+
$newRelation = ForeignRelationship::fromString($relation);
72+
if (is_null($newRelation)) {
73+
continue;
74+
}
75+
76+
if (!empty($newRelation->name) && in_array($newRelation->name, $existingNames)) {
77+
continue;
78+
}
79+
80+
$finalRelations[] = $newRelation;
81+
$existingNames[] = $newRelation->name;
82+
}
83+
84+
return $finalRelations;
85+
}
86+
87+
/**
88+
* Get the indexes from an existing array.
89+
*
90+
* @param array $relations
91+
*
92+
* @return array
93+
*/
94+
protected function getIndexes($indexes)
95+
{
96+
$existingNames = [];
97+
$finalIndexes = [];
98+
foreach ($indexes as $index) {
99+
$newIndex = Index::fromString($index);
100+
if (!empty($newIndex->getName()) && in_array($newIndex->getName(), $existingNames)) {
101+
continue;
102+
}
103+
104+
$finalIndexes[] = $newIndex;
105+
$existingNames[] = $newIndex->getName();
106+
}
107+
108+
return $finalIndexes;
109+
}
110+
111+
/**
112+
* Gets a clean user inputs.
113+
*
114+
* @return object
115+
*/
116+
protected function getCommandInput()
117+
{
118+
$modelName = trim($this->argument('model-name'));
119+
$filename = trim($this->option('resource-filename'));
120+
$file = $filename ? str_finish($filename, '.json') : Helpers::makeJsonFileName($modelName);
121+
$translationFor = array_unique(Arr::fromString($this->option('translation-for')));
122+
123+
return (object) compact(
124+
'modelName',
125+
'file',
126+
'translationFor'
127+
);
128+
}
129+
}

src/Models/Field.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ protected function getLabelsFromProperties(array $properties)
11171117
if (!Helpers::isKeyExists($properties, 'labels')) {
11181118
throw new Exception('The resource-file is missing the labels entry for the ' . $this->name . ' field.');
11191119
}
1120-
1120+
11211121
if (is_array($properties['labels'])) {
11221122
//At this point we know this the label
11231123
return $this->getLabelsFromArray($properties['labels']);
@@ -1381,7 +1381,23 @@ public function isTime()
13811381
*/
13821382
public function isNumeric()
13831383
{
1384-
return $this->isDecimal() || in_array($this->getEloquentDataMethod(), ['bigIncrements', 'bigInteger', 'increments', 'integer', 'mediumIncrements', 'mediumInteger', 'smallIncrements', 'smallInteger', 'tinyInteger', 'unsignedBigInteger', 'unsignedInteger', 'unsignedMediumInteger', 'unsignedSmallInteger', 'unsignedTinyInteger']);
1384+
return $this->isDecimal() || in_array($this->getEloquentDataMethod(),
1385+
[
1386+
'bigIncrements',
1387+
'bigInteger',
1388+
'increments',
1389+
'integer',
1390+
'mediumIncrements',
1391+
'mediumInteger',
1392+
'smallIncrements',
1393+
'smallInteger',
1394+
'tinyInteger',
1395+
'unsignedBigInteger',
1396+
'unsignedInteger',
1397+
'unsignedMediumInteger',
1398+
'unsignedSmallInteger',
1399+
'unsignedTinyInteger',
1400+
]);
13851401
}
13861402

13871403
/**

src/Support/Config.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,23 +641,50 @@ public static function getStringBaseValue($index, $default)
641641
public static function getCommonDefinitions()
642642
{
643643
$customValues = (array) self::getCustom('common_definitions', []);
644+
644645
$defaultValues = self::get('common_definitions', []);
645646
$final = [];
647+
$finalMatchingKeys = [];
646648

649+
// Merge properties with existing patterns
647650
foreach ($defaultValues as $key => $defaultValue) {
648651
if (is_array($defaultValue)
649652
&& array_key_exists('match', $defaultValue)
650653
&& array_key_exists('set', $defaultValue)
651654
) {
652655
$matches = (array) $defaultValue['match'];
653656

657+
$finalMatchingKeys = array_merge($finalMatchingKeys, $matches);
658+
654659
$final[] = [
655660
'match' => $matches,
656661
'set' => self::mergeDefinitions($matches, $customValues, (array) $defaultValue['set']),
657662
];
658663
}
659664
}
660665

666+
// Add new patterns
667+
foreach ($customValues as $key => $customValue) {
668+
if (is_array($customValue)
669+
&& array_key_exists('match', $customValue)
670+
&& array_key_exists('set', $customValue)
671+
) {
672+
$newPatterns = array_diff((array) $customValue['match'], $finalMatchingKeys);
673+
674+
if (empty($newPatterns)) {
675+
// At this point there are no patters that we don't already have
676+
continue;
677+
}
678+
679+
$finalMatchingKeys = array_merge($finalMatchingKeys, $newPatterns);
680+
681+
$final[] = [
682+
'match' => $newPatterns,
683+
'set' => $customValue['set'],
684+
];
685+
}
686+
}
687+
661688
return $final;
662689
}
663690

0 commit comments

Comments
 (0)