diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a264bb..0ccabff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### Upgrade - - If you are upgrading from v2.0 or v2.1 to v2.3 make sure you publish the vendor resource. There are some changes to the templates that required to be updated. + - If you are upgrading from v2.0, v2.1 or v2.2 to v2.3 make sure you publish the vendor resource. There are some updates to the config file. + - If you are upgrading from v2.0 or v2.1 to v2.2 make sure you publish the vendor resource. There are some changes to the templates that required to be updated. -- If you are upgrading any version prior v2.2 follow the upgrate instruction on https://crestapps.com/laravel-code-generator/docs/2.2#upgrade-guide ### Options Changes diff --git a/README.md b/README.md index 3a61b7f..755b022 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ -# An awesome code generator for laravel framework - with client-side validation - -For full documentation and live demo please visit CrestApps.com - ## Introduction -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 on the use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application. +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. + +For full documentation and live demo please visit CrestApps.com ## Features @@ -40,31 +38,32 @@ A clean code generator for Laravel framework that will save you time! This aweso 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 -``` + ``` + composer require crestapps/laravel-code-generator --dev + ``` 2. **(You may 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. + Add the following line to bootstrap laravel-code-generator to the framework. -``` -if ($this->app->runningInConsole()) { - $this->app->register('CrestApps\CodeGenerator\CodeGeneratorServiceProvider'); -} -``` + ``` + if ($this->app->runningInConsole()) { + $this->app->register('CrestApps\CodeGenerator\CodeGeneratorServiceProvider'); + } + ``` 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 -``` + + ``` + php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default + ``` > A layout is required for the default views! The code generator allows you to create a layout using the command-line. Of cource you can use your own layout. You'll only need to include [CSS bootstrap framework](http://getbootstrap.com/ "CSS bootstrap framework") in your layout for the default templates to work properly. Additionally, you can chose to you design your own templetes using a different or no css framework. ## Lessons -Checkout our YouTube channel -> https://youtu.be/l21qNcsMAWg -> https://youtu.be/infoecfXOCw +Checkout our YouTube channel on YouTube.com +* https://youtu.be/l21qNcsMAWg +* https://youtu.be/infoecfXOCw ## Available Commands diff --git a/composer.json b/composer.json index 07cea8e..a40fa9f 100644 --- a/composer.json +++ b/composer.json @@ -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.2.2", + "version": "v2.2.3", "keywords": [ "laravel","crud","crud generator", "laravel crud generator","laravel crud builder", diff --git a/config/codegenerator.php b/config/codegenerator.php index d13a119..0a6c9a9 100644 --- a/config/codegenerator.php +++ b/config/codegenerator.php @@ -208,15 +208,17 @@ | Key phrases that are will be used to determine if a field name should be | used for header. |-------------------------------------------------------------------------- + | + | You may use * as a while card in the name. For example, "head*" will + | match any field name that starts with the word "head" | */ 'common_header_patterns' => [ 'title', 'name', 'label', - 'header', 'subject', - 'head', + 'head*', ], /* @@ -284,6 +286,7 @@ [ 'match' => '*', 'set' => [ + // You may use any of the field templates to create the label 'labels' => '[% field_name_title %]', ], ], @@ -302,7 +305,7 @@ ], ], [ - 'match' => ['title', 'name', 'label', 'header'], + 'match' => ['title', 'name', 'label', 'subject', 'head*'], 'set' => [ 'is-nullable' => false, 'data-type' => 'string', diff --git a/src/Models/Field.php b/src/Models/Field.php index fb52e92..0a5542c 100644 --- a/src/Models/Field.php +++ b/src/Models/Field.php @@ -141,13 +141,6 @@ class Field implements JsonWriter */ private $labels = []; - /** - * The languages for the labels - * - * @var array - */ - private $languages = []; - /** * The html-type of the field * @@ -369,15 +362,13 @@ class Field implements JsonWriter * * @param string $name * @param string $localeGroup - * @param array $languages * * @return void */ - public function __construct($name, $localeGroup, $languages) + public function __construct($name, $localeGroup) { $this->name = $name; $this->localeGroup = $localeGroup; - $this->languages = $languages; $this->defaultLang = App::getLocale(); } @@ -1133,16 +1124,6 @@ protected function getLabelsFromProperties(array $properties) ]; } - /** - * Checks if the field has languages - * - * @return bool - */ - protected function isMultiLingual() - { - return !empty($this->langauges); - } - /** * It set the validationRules property for a giving field * @@ -1785,11 +1766,10 @@ public static function getNameFromArray(array $properties) * * @param array $properties * @param string $localeGroup - * @param array $languages * * @return $this */ - public static function fromArray(array $properties, $localeGroup, $languages = []) + public static function fromArray(array $properties, $localeGroup) { $fieldName = self::getNameFromArray($properties); @@ -1797,7 +1777,7 @@ public static function fromArray(array $properties, $localeGroup, $languages = [ unset($properties['html-type']); } - $field = new self($fieldName, $localeGroup, $languages); + $field = new self($fieldName, $localeGroup); $field->setPredefindProperties($properties) ->setLabelsProperty($properties) diff --git a/src/Models/ForeignRelationship.php b/src/Models/ForeignRelationship.php index eb72fbc..eb0ac0f 100644 --- a/src/Models/ForeignRelationship.php +++ b/src/Models/ForeignRelationship.php @@ -169,25 +169,24 @@ public function getName() */ protected function guessForeignField() { + // First we try to find a column that match the header pattern $columns = $this->getModelColumns(); $names = Config::getHeadersPatterns(); - foreach ($columns as $column) { - $matchedPattern = ''; - if (Helpers::strIs($names, $column, $matchedPattern)) { + if (Helpers::strIs($names, $column)) { + // At this point a column that match the header patter was found return $column; } } + // At this point we know no column that match the header patters found. + // Second we try to find a non-primary/non-foreign key column $primary = $this->getPrimaryKeyForForeignModel(); $idPatterns = Config::getKeyPatterns(); - $columns = array_filter($columns, function ($column) use ($primary, $idPatterns) { - - return $column != $primary && !Helpers::strIs($idPatterns, $column); - }); - - if (count($columns) > 0) { - return current($columns); + foreach ($columns as $column) { + if ($column != $primary && !Helpers::strIs($idPatterns, $column)) { + return $column; + } } return $primary; @@ -293,11 +292,17 @@ public function getModelColumns() $model = $this->getForeignModelInstance(); $columns = []; if ($this->isModel($model)) { + // At this point we know a model class exists + // Try to get the database column listing from the database directly $tableName = $model->getTable(); $columns = DB::getSchemaBuilder()->getColumnListing($tableName); } if (count($columns) == 0) { + // At this poing we know the column have not yet been identified + // which also mean that the model does not exists or the table + // does not existing in the database. + // Try to find the columns from the resource-file if one found. $columns = $this->getFieldNamesFromResource(); } @@ -344,10 +349,10 @@ protected function getFieldNamesFromResource() protected function getForeignResource() { $modelName = $this->getForeignModelName(); + // Find the resource file from the resource-map or make a standard name. $resourceFile = ResourceMapper::pluckFirst($modelName) ?: Helpers::makeJsonFileName($modelName); - $resourceFileFullName = Config::getResourceFilePath($resourceFile); - if (File::exists($resourceFileFullName)) { + if (File::exists(Config::getResourceFilePath($resourceFile))) { return Resource::fromFile($resourceFile, 'crestapps'); } diff --git a/src/Support/FieldTransformer.php b/src/Support/FieldTransformer.php index 400fd88..54b3b05 100644 --- a/src/Support/FieldTransformer.php +++ b/src/Support/FieldTransformer.php @@ -355,10 +355,6 @@ protected function getOptions(array $options) // At this point we are just formatting the labels foreach ($options as $key => $option) { - if (is_array($option)) { - dd($option, $this->hasLanguages(), $this->languages); - } - $labels[$key] = Helpers::convertNameToLabel($option); } } diff --git a/src/Support/Helpers.php b/src/Support/Helpers.php index 2f30e80..4b60def 100644 --- a/src/Support/Helpers.php +++ b/src/Support/Helpers.php @@ -18,7 +18,8 @@ class Helpers */ public static function makeControllerName($modelName) { - $case = ucfirst(camel_case(self::getProperCaseFor($modelName, 'controller-name'))); + $name = self::getProperCaseFor($modelName, 'controller-name'); + $case = ucfirst(camel_case($name)); if (!empty($postFix = Config::getControllerNamePostFix())) { return str_finish($case, $postFix); @@ -81,7 +82,8 @@ public static function convertNameToLabel($name) */ public static function makeFormRequestName($modelName) { - $case = ucfirst(camel_case(self::getProperCaseFor($modelName, 'request-form-name'))); + $name = self::getProperCaseFor($modelName, 'request-form-name'); + $case = ucfirst(camel_case($name)); if (!empty($postFix = Config::getFormRequestNamePostFix())) { return str_finish($case, $postFix); @@ -150,7 +152,9 @@ public static function guessModelFullName($name, $modelsPath) */ public static function extractModelName($name) { - return ucfirst(studly_case(Str::singular(str_replace('_id', '', $name)))); + $name = self::removePostFixWith($name, '_id'); + + return ucfirst(studly_case(Str::singular($name))); } /** @@ -266,9 +270,9 @@ public static function isNewerThanOrEqualTo($version = '5.3') */ public static function strReplaceOnce($pattern, $replacment, $subject) { - if (strpos($subject, $pattern) !== false) { - $occurrence = strpos($subject, $pattern); - return substr_replace($subject, $replacment, strpos($subject, $pattern), strlen($pattern)); + $index = strpos($subject, $pattern); + if ($index !== false) { + return substr_replace($subject, $replacment, $index, strlen($pattern)); } return $subject; @@ -392,14 +396,16 @@ public static function stringToBool($str) * Removes a string from the end of another giving string if it already ends with it. * * @param string $name - * @param string $postFix + * @param string $fix * * @return string */ - public static function removePostFixWith($name, $postFix = '/') + public static function removePostFixWith($name, $fix = '/') { - if (ends_with($name, $postFix)) { - return strstr($name, $postFix, true); + $position = strripos($name, $fix); + + if ($position !== false) { + return substr($name, 0, $position); } return $name; @@ -409,14 +415,14 @@ public static function removePostFixWith($name, $postFix = '/') * Adds a postFix string at the end of another giving string if it does not already ends with it. * * @param string $name - * @param string $postFix + * @param string $fix * * @return string */ - public static function postFixWith($name, $postFix = '/') + public static function postFixWith($name, $fix = '/') { - if (!ends_with($name, $postFix)) { - return $name . $postFix; + if (!ends_with($name, $fix)) { + return $name . $fix; } return $name; @@ -426,14 +432,14 @@ public static function postFixWith($name, $postFix = '/') * Adds a preFix string at the begining of another giving string if it does not already ends with it. * * @param string $name - * @param string $preFix + * @param string $fix * * @return string */ - public static function preFixWith($name, $preFix = '/') + public static function preFixWith($name, $fix = '/') { - if (!starts_with($name, $preFix)) { - return $preFix . $name; + if (!starts_with($name, $fix)) { + return $fix . $name; } return $name;