Skip to content

Commit 9fa418a

Browse files
committed
fixes couple minor bugs with generating fields
1 parent 8db4ae8 commit 9fa418a

File tree

6 files changed

+56
-67
lines changed

6 files changed

+56
-67
lines changed

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ A clean code generator for Laravel framework that will save you time! This aweso
99
## Features
1010

1111
* Create very clean code to build on.
12-
* Create full resources using a single command with/without <strong>migration</strong> or from <strong>existing database</strong>.
12+
* Create full resources using a single command with/without **migration** or from **existing database**.
1313
* Create standard CRUD controllers with simple or form-request validation.
1414
* Create model with relations.
1515
* Create named routes.
1616
* Create standard CRUD views.
1717
* Very flexible and rich with configurable options.
18-
* (Beta) Client-side validation.
18+
* Client-side validation.
1919
* File uploading handling.
2020
* Auto multiple-response storing in the database.
2121
* Create form-request for complex validation.
@@ -33,21 +33,23 @@ A clean code generator for Laravel framework that will save you time! This aweso
3333

3434
## Installation
3535

36-
To download this package into your laravel project, use the command-line to execute the following command
37-
36+
1. To download this package into your laravel project, use the command-line to execute the following command
37+
3838
```
3939
composer require crestapps/laravel-code-generator --dev
4040
```
4141

42-
**(Skip this step when using Laravl >= 5.5)** To bootstrap the packages into your project, open the `config/app.php` file in your project. Then, look for the providers array.
42+
2. **(Skip this step when using Laravel >= 5.5)** To bootstrap the packages into your project while using command-line only, open the app/Providers/AppServiceProvider.php file in your project. Then, add the following code to the register() method.
4343

4444
Add the following line to bootstrap laravel-code-generator to the framework.
4545

4646
```
47-
CrestApps\CodeGenerator\CodeGeneratorServiceProvider::class,
47+
if ($this->app->runningInConsole()) {
48+
$this->app->register('CrestApps\CodeGenerator\CodeGeneratorServiceProvider');
49+
}
4850
```
4951

50-
Finally, execute the following command from the command-line to publish the package's config and the default template to start generating awesome code.
52+
3. Execute the following command from the command-line to publish the package's config and the default template to start generating awesome code.
5153
```
5254
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default
5355
```

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "crestapps/laravel-code-generator",
33
"license": "MIT",
44
"description": "A clean code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages or request-forms! It is extremely flexible and customizable to cover many use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.",
5-
"version": "v2.1.3",
5+
"version": "v2.1.4",
66
"keywords": [
77
"laravel","crud","crud generator",
88
"laravel crud generator","laravel crud builder",

src/Support/.php_cs.cache

+1-1
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":{"FieldTransformer.php":-1070777758,"FieldOptimizer.php":-1370109080,"FieldsOptimizer.php":815514108,"Helpers.php":-2064356608,"ViewsCommand.php":1413206202,"ValidationParser.php":-513830996,"CrestAppsTranslator.php":-558689661,"Config.php":1657619464,"ViewLabelsGenerator.php":-488179578}}
1+
{"php":"7.1.6","version":"2.3.2:v2.3.2#597745f744bcce1aed59dfd1bb4603de2a06cda9","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":{"FieldTransformer.php":816830263,"Config.php":-964520305}}

src/Support/Config.php

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
class Config
88
{
9+
/**
10+
* Gets the default datetime output format
11+
*
12+
* @return array
13+
*/
14+
public static function getCommonDefinitions()
15+
{
16+
return config('codegenerator.common_definitions', []);
17+
}
18+
919
/**
1020
* Gets the default datetime output format
1121
*

src/Support/FieldOptimizer.php

+5-27
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,6 @@ class FieldOptimizer
3232
*/
3333
protected $meta;
3434

35-
/**
36-
* Array of the valid primary key data-types
37-
*
38-
* @return array
39-
*/
40-
protected $validPrimaryDataTypes =
41-
[
42-
'int',
43-
'integer',
44-
'bigint',
45-
'biginteger',
46-
'mediumint',
47-
'mediuminteger',
48-
'uuid'
49-
];
50-
5135
/**
5236
* Create a new optemizer instance.
5337
*
@@ -73,7 +57,11 @@ public function getField()
7357
return $this->field;
7458
}
7559

76-
60+
/**
61+
* Optimizes the field.
62+
*
63+
* @return $this
64+
*/
7765
public function optimize()
7866
{
7967
$this->optimizeStringField()
@@ -248,14 +236,4 @@ protected function optimizePrimaryKey()
248236

249237
return $this;
250238
}
251-
252-
/**
253-
* It checks if the field is numeric type
254-
*
255-
* @return bool
256-
*/
257-
protected function isNumericField()
258-
{
259-
return in_array($this->field->dataType, $this->validPrimaryDataTypes);
260-
}
261239
}

src/Support/FieldTransformer.php

+30-31
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ class FieldTransformer
4949
'html-type' => 'htmlType',
5050
'html-value' => 'htmlValue',
5151
'value' => [
52-
'dataValue',
53-
'htmlValue'
52+
'dataValue',
53+
'htmlValue'
5454
],
5555
'is-on-views' => [
56-
'isOnIndexView',
57-
'isOnFormView',
58-
'isOnShowView'
56+
'isOnIndexView',
57+
'isOnFormView',
58+
'isOnShowView'
5959
],
6060
'is-on-index' => 'isOnIndexView',
6161
'is-on-form' => 'isOnFormView',
@@ -268,18 +268,19 @@ protected function getProperties(array $properties)
268268
unset($properties['html-type']);
269269
}
270270

271-
$definitions = (array) config('codegenerator.common_definitions', []);
271+
$definitions = Config::getCommonDefinitions();
272272

273273
foreach ($definitions as $definition) {
274274
$patterns = $this->isKeyExists($definition, 'match') ? (array) $definition['match'] : [];
275-
$config = $this->isKeyExists($definition, 'set') ? (array) $definition['set'] : [];
275+
$configs = $this->isKeyExists($definition, 'set') ? (array) $definition['set'] : [];
276276

277-
if (count($config) > 0 && Helpers::strIs($patterns, $properties['name'])) {
278-
if (isset($config['name'])) {
279-
unset($config['name']);
277+
if (Helpers::strIs($patterns, $properties['name'])) {
278+
//auto add any config from the master config
279+
foreach ($configs as $key => $config) {
280+
if (!$this->isKeyExists($properties, $key)) {
281+
$properties[$key] = $config;
282+
}
280283
}
281-
282-
$properties = array_merge($properties, $config);
283284
}
284285
}
285286

@@ -386,7 +387,7 @@ protected function setRange(Field & $field, array $properties)
386387
$field->range = explode(':', substr($properties['html-type'], 12));
387388
}
388389

389-
if($this->isKeyExists($properties, 'range') && is_array($properties['range'])) {
390+
if ($this->isKeyExists($properties, 'range') && is_array($properties['range'])) {
390391
$field->range = $properties['range'];
391392
}
392393

@@ -585,7 +586,8 @@ protected function getForeignConstraint(array $properties)
585586
$onUpdate = $this->isKeyExists($constraint, 'on-update') ? $constraint['on-update'] : null;
586587
$onDelete = $this->isKeyExists($constraint, 'on-delete') ? $constraint['on-delete'] : null;
587588
$modelPath = $this->getModelsPath();
588-
$model = $this->isKeyExists($constraint, 'references-model') ? $constraint['references-model'] : self::guessModelFullName($name, $modelPath);
589+
$model = $this->isKeyExists($constraint, 'references-model') ? $constraint['references-model'] :
590+
self::guessModelFullName($properties['name'], $modelPath);
589591

590592
return new ForeignConstraint($constraint['field'], $constraint['references'], $constraint['on'], $onDelete, $onUpdate, $model);
591593
}
@@ -762,7 +764,7 @@ protected function setValidationProperty(Field & $field, array $properties)
762764
$field->validationRules[] = 'array';
763765
}
764766

765-
if (in_array($field->dataType, ['char','string']) && in_array($field->htmlType, ['text','textarea']) ) {
767+
if (in_array($field->dataType, ['char','string']) && in_array($field->htmlType, ['text','textarea'])) {
766768
if (!in_array('string', $field->validationRules)) {
767769
$field->validationRules[] = 'string';
768770
}
@@ -771,7 +773,7 @@ protected function setValidationProperty(Field & $field, array $properties)
771773
$field->validationRules[] = sprintf('min:%s', $field->getMinLength());
772774
}
773775

774-
if (!$this->inArraySearch($field->validationRules, 'max') && !is_null($field->getMaxLength()) ) {
776+
if (!$this->inArraySearch($field->validationRules, 'max') && !is_null($field->getMaxLength())) {
775777
$field->validationRules[] = sprintf('max:%s', $field->getMaxLength());
776778
}
777779
}
@@ -782,19 +784,18 @@ protected function setValidationProperty(Field & $field, array $properties)
782784
$params = $this->getDataTypeParams($field->dataType, (array) $properties['data-type-params']);
783785
}
784786

785-
if ( $field->htmlType == 'number' || (in_array($field->dataType, ['decimal','double','float'])
786-
&& isset($params[0]) && ($length = intval($params[0])) > 0
787-
&& isset($params[1]) && ($decimal = intval($params[1])) > 0) )
788-
{
787+
if ($field->htmlType == 'number' || (in_array($field->dataType, ['decimal','double','float'])
788+
&& isset($params[0]) && ($length = intval($params[0])) > 0
789+
&& isset($params[1]) && ($decimal = intval($params[1])) > 0)) {
789790
if (!in_array('numeric', $field->validationRules)) {
790791
$field->validationRules[] = 'numeric';
791792
}
792793

793-
if (!$this->inArraySearch($field->validationRules, 'min') && !is_null($minValue = $field->getMinValue()) ) {
794+
if (!$this->inArraySearch($field->validationRules, 'min') && !is_null($minValue = $field->getMinValue())) {
794795
$field->validationRules[] = sprintf('min:%s', $minValue);
795796
}
796797

797-
if (!$this->inArraySearch($field->validationRules, 'max') && !is_null($maxValue = $field->getMaxValue()) ) {
798+
if (!$this->inArraySearch($field->validationRules, 'max') && !is_null($maxValue = $field->getMaxValue())) {
798799
$field->validationRules[] = sprintf('max:%s', $maxValue);
799800
}
800801
}
@@ -1000,7 +1001,6 @@ protected function getLabelsFromArray(array $items)
10001001
protected function getPlaceholder(Field $field, array $properties)
10011002
{
10021003
if (isset($properties['placeholder']) && !empty($properties['placeholder'])) {
1003-
10041004
if (is_array($properties['placeholder'])) {
10051005
//At this point we know this the label
10061006
return $this->getLabelsFromArray($properties['placeholder']);
@@ -1013,23 +1013,22 @@ protected function getPlaceholder(Field $field, array $properties)
10131013

10141014
$labels = [];
10151015

1016-
if(!isset($properties['placeholder'])) {
1016+
if (!isset($properties['placeholder'])) {
10171017
$templates = Config::getPlaceholderByHtmlType();
10181018

1019-
foreach($templates as $type => $title) {
1020-
if($field->htmlType == $type) {
1021-
1019+
foreach ($templates as $type => $title) {
1020+
if ($field->htmlType == $type) {
10221021
$fieldName = $field->hasForeignRelation() ? $field->getForeignRelation()->name : $field->name;
10231022
$this->replaceFieldNamePatterns($title, $fieldName);
10241023
$langs = $field->getAvailableLanguages();
10251024

1026-
if(count($langs) == 0) {
1025+
if (count($langs) == 0) {
10271026
return [
10281027
new Label($title, $this->localeGroup, true, $this->defaultLang)
10291028
];
10301029
}
10311030

1032-
foreach($langs as $lang) {
1031+
foreach ($langs as $lang) {
10331032
$labels[] = new Label($title, $this->localeGroup, false, $lang);
10341033
}
10351034
}
@@ -1219,7 +1218,7 @@ protected function replaceFieldNamePatterns(&$stub, $name)
12191218
$plural = str_plural($englishSingle);
12201219

12211220
$stub = $this->strReplace('field_name', $englishSingle, $stub);
1222-
$stub = $this->strReplace('field_name_flat', strtolower($name), $stub);
1221+
$stub = $this->strReplace('field_name_flat', strtolower($name), $stub);
12231222
$stub = $this->strReplace('field_name_sentence', ucfirst($englishSingle), $stub);
12241223
$stub = $this->strReplace('field_name_plural', $plural, $stub);
12251224
$stub = $this->strReplace('field_name_plural_title', title_case($plural), $stub);
@@ -1230,7 +1229,7 @@ protected function replaceFieldNamePatterns(&$stub, $name)
12301229
$stub = $this->strReplace('field_name_title', Helpers::titleCase($englishSingle), $stub);
12311230
$stub = $this->strReplace('field_name_title_lower', strtolower($englishSingle), $stub);
12321231
$stub = $this->strReplace('field_name_title_upper', strtoupper($englishSingle), $stub);
1233-
$stub = $this->strReplace('field_name_class', $name, $stub);
1232+
$stub = $this->strReplace('field_name_class', $name, $stub);
12341233
$stub = $this->strReplace('field_name_plural_variable', $this->getPluralVariable($name), $stub);
12351234
$stub = $this->strReplace('field_name_singular_variable', $this->getSingularVariable($name), $stub);
12361235

0 commit comments

Comments
 (0)