Skip to content

Commit

Permalink
fixes couple minor bugs with generating fields
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek committed Jul 15, 2017
1 parent 8db4ae8 commit 9fa418a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 67 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ A clean code generator for Laravel framework that will save you time! This aweso
## Features

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

## Installation

To download this package into your laravel project, use the command-line to execute the following command
1. To download this package into your laravel project, use the command-line to execute the following command

```
composer require crestapps/laravel-code-generator --dev
```

**(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.
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.

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

```
CrestApps\CodeGenerator\CodeGeneratorServiceProvider::class,
if ($this->app->runningInConsole()) {
$this->app->register('CrestApps\CodeGenerator\CodeGeneratorServiceProvider');
}
```

Finally, execute the following command from the command-line to publish the package's config and the default template to start generating awesome code.
3. Execute the following command from the command-line to publish the package's config and the default template to start generating awesome code.
```
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "crestapps/laravel-code-generator",
"license": "MIT",
"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.",
"version": "v2.1.3",
"version": "v2.1.4",
"keywords": [
"laravel","crud","crud generator",
"laravel crud generator","laravel crud builder",
Expand Down
2 changes: 1 addition & 1 deletion src/Support/.php_cs.cache
Original file line number Diff line number Diff line change
@@ -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}}
{"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}}
10 changes: 10 additions & 0 deletions src/Support/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

class Config
{
/**
* Gets the default datetime output format
*
* @return array
*/
public static function getCommonDefinitions()
{
return config('codegenerator.common_definitions', []);
}

/**
* Gets the default datetime output format
*
Expand Down
32 changes: 5 additions & 27 deletions src/Support/FieldOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@ class FieldOptimizer
*/
protected $meta;

/**
* Array of the valid primary key data-types
*
* @return array
*/
protected $validPrimaryDataTypes =
[
'int',
'integer',
'bigint',
'biginteger',
'mediumint',
'mediuminteger',
'uuid'
];

/**
* Create a new optemizer instance.
*
Expand All @@ -73,7 +57,11 @@ public function getField()
return $this->field;
}


/**
* Optimizes the field.
*
* @return $this
*/
public function optimize()
{
$this->optimizeStringField()
Expand Down Expand Up @@ -248,14 +236,4 @@ protected function optimizePrimaryKey()

return $this;
}

/**
* It checks if the field is numeric type
*
* @return bool
*/
protected function isNumericField()
{
return in_array($this->field->dataType, $this->validPrimaryDataTypes);
}
}
61 changes: 30 additions & 31 deletions src/Support/FieldTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ class FieldTransformer
'html-type' => 'htmlType',
'html-value' => 'htmlValue',
'value' => [
'dataValue',
'htmlValue'
'dataValue',
'htmlValue'
],
'is-on-views' => [
'isOnIndexView',
'isOnFormView',
'isOnShowView'
'isOnIndexView',
'isOnFormView',
'isOnShowView'
],
'is-on-index' => 'isOnIndexView',
'is-on-form' => 'isOnFormView',
Expand Down Expand Up @@ -268,18 +268,19 @@ protected function getProperties(array $properties)
unset($properties['html-type']);
}

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

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

if (count($config) > 0 && Helpers::strIs($patterns, $properties['name'])) {
if (isset($config['name'])) {
unset($config['name']);
if (Helpers::strIs($patterns, $properties['name'])) {
//auto add any config from the master config
foreach ($configs as $key => $config) {
if (!$this->isKeyExists($properties, $key)) {
$properties[$key] = $config;
}
}

$properties = array_merge($properties, $config);
}
}

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

if($this->isKeyExists($properties, 'range') && is_array($properties['range'])) {
if ($this->isKeyExists($properties, 'range') && is_array($properties['range'])) {
$field->range = $properties['range'];
}

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

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

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

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

if ( $field->htmlType == 'number' || (in_array($field->dataType, ['decimal','double','float'])
&& isset($params[0]) && ($length = intval($params[0])) > 0
&& isset($params[1]) && ($decimal = intval($params[1])) > 0) )
{
if ($field->htmlType == 'number' || (in_array($field->dataType, ['decimal','double','float'])
&& isset($params[0]) && ($length = intval($params[0])) > 0
&& isset($params[1]) && ($decimal = intval($params[1])) > 0)) {
if (!in_array('numeric', $field->validationRules)) {
$field->validationRules[] = 'numeric';
}

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

if (!$this->inArraySearch($field->validationRules, 'max') && !is_null($maxValue = $field->getMaxValue()) ) {
if (!$this->inArraySearch($field->validationRules, 'max') && !is_null($maxValue = $field->getMaxValue())) {
$field->validationRules[] = sprintf('max:%s', $maxValue);
}
}
Expand Down Expand Up @@ -1000,7 +1001,6 @@ protected function getLabelsFromArray(array $items)
protected function getPlaceholder(Field $field, array $properties)
{
if (isset($properties['placeholder']) && !empty($properties['placeholder'])) {

if (is_array($properties['placeholder'])) {
//At this point we know this the label
return $this->getLabelsFromArray($properties['placeholder']);
Expand All @@ -1013,23 +1013,22 @@ protected function getPlaceholder(Field $field, array $properties)

$labels = [];

if(!isset($properties['placeholder'])) {
if (!isset($properties['placeholder'])) {
$templates = Config::getPlaceholderByHtmlType();

foreach($templates as $type => $title) {
if($field->htmlType == $type) {

foreach ($templates as $type => $title) {
if ($field->htmlType == $type) {
$fieldName = $field->hasForeignRelation() ? $field->getForeignRelation()->name : $field->name;
$this->replaceFieldNamePatterns($title, $fieldName);
$langs = $field->getAvailableLanguages();

if(count($langs) == 0) {
if (count($langs) == 0) {
return [
new Label($title, $this->localeGroup, true, $this->defaultLang)
];
}

foreach($langs as $lang) {
foreach ($langs as $lang) {
$labels[] = new Label($title, $this->localeGroup, false, $lang);
}
}
Expand Down Expand Up @@ -1219,7 +1218,7 @@ protected function replaceFieldNamePatterns(&$stub, $name)
$plural = str_plural($englishSingle);

$stub = $this->strReplace('field_name', $englishSingle, $stub);
$stub = $this->strReplace('field_name_flat', strtolower($name), $stub);
$stub = $this->strReplace('field_name_flat', strtolower($name), $stub);
$stub = $this->strReplace('field_name_sentence', ucfirst($englishSingle), $stub);
$stub = $this->strReplace('field_name_plural', $plural, $stub);
$stub = $this->strReplace('field_name_plural_title', title_case($plural), $stub);
Expand All @@ -1230,7 +1229,7 @@ protected function replaceFieldNamePatterns(&$stub, $name)
$stub = $this->strReplace('field_name_title', Helpers::titleCase($englishSingle), $stub);
$stub = $this->strReplace('field_name_title_lower', strtolower($englishSingle), $stub);
$stub = $this->strReplace('field_name_title_upper', strtoupper($englishSingle), $stub);
$stub = $this->strReplace('field_name_class', $name, $stub);
$stub = $this->strReplace('field_name_class', $name, $stub);
$stub = $this->strReplace('field_name_plural_variable', $this->getPluralVariable($name), $stub);
$stub = $this->strReplace('field_name_singular_variable', $this->getSingularVariable($name), $stub);

Expand Down

0 comments on commit 9fa418a

Please sign in to comment.