From c279b331900469d92371b0b0d4a41d665287efd2 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Wed, 24 Jan 2024 08:24:26 -0800 Subject: [PATCH] cleanup collective --- CHANGELOG.md | 2 +- README.md | 2 +- config/default.php | 21 +------ src/CodeGeneratorServiceProvider.php | 5 -- .../Api/CreateApiControllerCommand.php | 4 +- src/Commands/Api/CreateApiResourceCommand.php | 55 ++++++------------- .../ApiDocs/CreateApiDocsViewCommand.php | 2 +- src/Commands/Bases/ControllerCommandBase.php | 2 +- src/Commands/Bases/ViewsCommandBase.php | 16 ++---- .../Framework/CreateLanguageCommand.php | 2 +- src/Support/Config.php | 10 ---- src/Support/ViewLabelsGenerator.php | 24 +------- src/Traits/CommonCommand.php | 14 +---- 13 files changed, 31 insertions(+), 128 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f10a84b..9856258 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ - The `create:routes` command now has a new option `without-route-clause` which will create the routes without `there` clause for the id. It may be used when the primary key is not an integer. Additionally, the options `--for-api` and `--api-version` have been added to add support for created api-based routes. - added command to create api-resource with/without collection when using Laravel 5.5+. - The `create:api-scaffold` command have been added to allow you to create resources for the api. - - The user is no longer required to publish the default templates. This is much better step to prevent upgrade braking when the template are updated during a patch release. The user should publish templates only if he/she want to modify it and rename it. **IMPORTANT** Delete existing default and default-collective templates from the folders from the default publish path of your project. + - The user is no longer required to publish the default templates. This is much better step to prevent upgrade braking when the template are updated during a patch release. The user should publish templates only if he/she want to modify it and rename it. **IMPORTANT** Delete existing `default` templates from the folders from the default publish path of your project. - The user no longer have to publish resource to install the package! One line only is required to install the package on laravel 5.5+ (i.e, composer require crestapps/laravel-code-generator --dev) - **IMPORTANT**: delete the the `codegenerator.php` file from your config folder, then rename the `codegenerator_custom.php` file to `laravel-code-generator.php`. Alternatively, you can delete both `codegenerator.php` and `codegenerator_custom.php` - Added `--model-extends` option to the create:model command to allow the use to extend a custom default base class. diff --git a/README.md b/README.md index 43a0156..f8c9561 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ For full documentation and live demo please visit [ 'text' => 'Delete [% model_name_title %]', 'template' => 'delete_model', - 'in-function-with-collective' => true, ], 'edit' => [ 'text' => 'Edit [% model_name_title %]', @@ -600,17 +584,14 @@ 'add' => [ 'text' => 'Add', 'template' => 'add', - 'in-function-with-collective' => true, ], 'update' => [ 'text' => 'Update', 'template' => 'update', - 'in-function-with-collective' => true, ], 'confirm_delete' => [ 'text' => 'Click Ok to delete [% model_name_title %].', 'template' => 'confirm_delete', - 'in-function-with-collective' => true, ], 'none_available' => [ 'text' => 'No [% model_name_plural_title %] Available.', diff --git a/src/CodeGeneratorServiceProvider.php b/src/CodeGeneratorServiceProvider.php index 68bb7ee..d6f52ca 100644 --- a/src/CodeGeneratorServiceProvider.php +++ b/src/CodeGeneratorServiceProvider.php @@ -26,11 +26,6 @@ public function boot() $this->publishes([ $dir . 'templates/default' => $this->codeGeneratorBase('templates/default'), ], 'default-template'); - - // publish the defaultcollective-template - $this->publishes([ - $dir . 'templates/default-collective' => $this->codeGeneratorBase('templates/default-collective'), - ], 'default-collective-template'); } /** diff --git a/src/Commands/Api/CreateApiControllerCommand.php b/src/Commands/Api/CreateApiControllerCommand.php index f8e1895..7b1261f 100644 --- a/src/Commands/Api/CreateApiControllerCommand.php +++ b/src/Commands/Api/CreateApiControllerCommand.php @@ -267,7 +267,7 @@ protected function getSuccessCall($modelName, array $fields, $method) { $stub = $this->getStubContent('api-controller-call-' . $method . '-success-method'); - $viewLabels = new ViewLabelsGenerator($modelName, $fields, $this->isCollectiveTemplate()); + $viewLabels = new ViewLabelsGenerator($modelName, $fields); $this->replaceModelName($stub, $modelName) ->replaceStandardLabels($stub, $viewLabels->getLabels()) @@ -289,7 +289,7 @@ protected function getApiResourceCall($modelName, $fields, $method) { $stub = $this->getStubContent('api-controller-call-' . $method . '-api-resource'); - $viewLabels = new ViewLabelsGenerator($modelName, $fields, $this->isCollectiveTemplate()); + $viewLabels = new ViewLabelsGenerator($modelName, $fields); $this->replaceModelName($stub, $modelName) ->replaceStandardLabels($stub, $viewLabels->getLabels()) diff --git a/src/Commands/Api/CreateApiResourceCommand.php b/src/Commands/Api/CreateApiResourceCommand.php index 30e2018..fc327e3 100644 --- a/src/Commands/Api/CreateApiResourceCommand.php +++ b/src/Commands/Api/CreateApiResourceCommand.php @@ -37,7 +37,6 @@ class CreateApiResourceCommand extends Command {--api-resource-collection-name= : The api-resource-collection file name.} {--model-directory= : The path where the model should be created under.} {--template-name= : The template name to use when generating the code.} - {--collection : Create a resource collection.} {--api-version= : The api version to prefix your resources with.} {--force : Override the model if one already exists.}'; @@ -58,22 +57,22 @@ public function handle() $input = $this->getCommandInput(); $resource = Resource::fromFile($input->resourceFile, 'CrestApps'); - $apiResourceFileName = $this->getApiFileName($input->modelName, $input->isCollection); + $apiResourceFileName = $this->getApiFileName($input->modelName); - $destenationFile = $this->getDestenationFile($apiResourceFileName, $input->isCollection); + $destenationFile = $this->getDestenationFile($apiResourceFileName); - if ($this->hasErrors($resource, $destenationFile, $input->isCollection)) { + if ($this->hasErrors($resource, $destenationFile)) { return false; } - $stub = $this->getStubContent($this->getFileTitle($input->isCollection)); - $viewLabels = new ViewLabelsGenerator($input->modelName, $resource->fields, $this->isCollectiveTemplate()); + $stub = $this->getStubContent($this->getFileTitle()); + $viewLabels = new ViewLabelsGenerator($input->modelName, $resource->fields); - return $this->replaceNamespace($stub, $this->getClassNamepace($input->isCollection)) - ->replaceModelApiArray($stub, $this->getModelApiArray($resource->fields, $input->modelName, $input->isCollection)) + return $this->replaceNamespace($stub, $this->getClassNamepace()) + ->replaceModelApiArray($stub, $this->getModelApiArray($resource->fields, $input->modelName)) ->replaceApiResourceClass($stub, $apiResourceFileName) ->replaceApiResourceCollectionClass($stub, $this->getApiResourceCollectionClassName($input->modelName)) - ->replaceTransformMethod($stub, $this->getTransformMethod($input, $resource->fields, $input->isCollection, $input->isCollection)) + ->replaceTransformMethod($stub, $this->getTransformMethod($input, $resource->fields)) ->replaceStandardLabels($stub, $viewLabels->getLabels()) ->replaceModelName($stub, $input->modelName) ->replaceModelFullname($stub, self::getModelNamespace($input->modelName, $input->modelDirectory)) @@ -82,35 +81,24 @@ public function handle() } /** - * Gets the namespace for the api class. - * - * @param bool $isCollection + * Gets the namespace for the API class. * * @return string */ - protected function getClassNamepace($isCollection) + protected function getClassNamepace() { - if ($isCollection) { - return $this->getApiResourceCollectionNamespace(); - } - return $this->getApiResourceNamespace(); } /** - * Gets the file name for the api class. + * Gets the file name for the API class. * * @param string $modelName - * @param bool $isCollection * * @return string */ - protected function getApiFileName($modelName, $isCollection) + protected function getApiFileName($modelName) { - if ($isCollection) { - return $this->getApiResourceCollectionClassName($modelName); - } - return $this->getApiResourceClassName($modelName); } @@ -123,10 +111,10 @@ protected function getApiFileName($modelName, $isCollection) * * @return bool */ - protected function hasErrors(Resource $resource, $destenationFile, $isCollection) + protected function hasErrors(Resource $resource, $destenationFile) { $hasErrors = false; - $title = $this->getFileTitle($isCollection); + $title = $this->getFileTitle(); if ($resource->isProtected($title)) { $this->warn('The ' . $title . ' is protected and cannot be regenerated. To regenerate the file, unprotect it from the resource-file.'); @@ -147,19 +135,12 @@ protected function hasErrors(Resource $resource, $destenationFile, $isCollection * Gets the destination file to be created. * * @param string $name - * @param bool $isCollection * * @return string */ - protected function getDestenationFile($name, $isCollection = false) - { - if ($isCollection) { - $path = $this->getApiResourceCollectionPath(); - } else { - $path = $this->getApiResourcePath(); - } - - $path = Str::trimStart($path, Helpers::getAppNamespace()); + protected function getDestenationFile($name) + { + $path = Str::trimStart($this->getApiResourcePath(), Helpers::getAppNamespace()); return app_path($path . $name . '.php'); } @@ -174,7 +155,6 @@ protected function getCommandInput() $modelName = trim($this->argument('model-name')); $resourceFile = trim($this->option('resource-file')) ?: Helpers::makeJsonFileName($modelName); $template = $this->getTemplateName(); - $isCollection = $this->option('collection'); $modelDirectory = trim($this->option('model-directory')); $apiVersion = trim($this->option('api-version')); @@ -182,7 +162,6 @@ protected function getCommandInput() 'modelName', 'resourceFile', 'template', - 'isCollection', 'modelDirectory' ); } diff --git a/src/Commands/ApiDocs/CreateApiDocsViewCommand.php b/src/Commands/ApiDocs/CreateApiDocsViewCommand.php index 47be65d..ab2b798 100644 --- a/src/Commands/ApiDocs/CreateApiDocsViewCommand.php +++ b/src/Commands/ApiDocs/CreateApiDocsViewCommand.php @@ -64,7 +64,7 @@ public function handle() $stub = $this->getStubContent('api-documentation-index'); - $viewLabels = new ViewLabelsGenerator($input->modelName, $resource->fields, $this->isCollectiveTemplate()); + $viewLabels = new ViewLabelsGenerator($input->modelName, $resource->fields); // The replaceAuthorizedRequestForIndex() method must be executed before replaceAuthorizationCall() return $this->replaceAuthorizedRequestForIndex($stub, $this->getAuthorizedRequestForIndex($input->withAuth, $resource->getApiDocLabels(), $viewLabels->getLabels())) diff --git a/src/Commands/Bases/ControllerCommandBase.php b/src/Commands/Bases/ControllerCommandBase.php index 7b09310..bbfaef0 100644 --- a/src/Commands/Bases/ControllerCommandBase.php +++ b/src/Commands/Bases/ControllerCommandBase.php @@ -917,7 +917,7 @@ protected function processCommonTasks($input, $resource, &$stub) $dataMethod = $this->getDataMethod($resource->fields, $this->requestNameSpace . '\\' . $this->requestName, $input); $languages = array_keys(self::getLanguageItems($resource->fields)); - $viewLabels = new ViewLabelsGenerator($input->modelName, $resource->fields, $this->isCollectiveTemplate()); + $viewLabels = new ViewLabelsGenerator($input->modelName, $resource->fields); $namespacesToUse = $this->getRequiredUseClasses($resource->fields, $this->getAdditionalNamespaces($input)); return $this->replaceGetDataMethod($stub, $dataMethod) diff --git a/src/Commands/Bases/ViewsCommandBase.php b/src/Commands/Bases/ViewsCommandBase.php index e12722f..3c9c92b 100644 --- a/src/Commands/Bases/ViewsCommandBase.php +++ b/src/Commands/Bases/ViewsCommandBase.php @@ -140,7 +140,7 @@ protected function getDestinationViewName($action) */ protected function replaceCommonTemplates(&$stub, $input, array $fields) { - $viewLabels = new ViewLabelsGenerator($input->modelName, $fields, $this->isCollectiveTemplate()); + $viewLabels = new ViewLabelsGenerator($input->modelName, $fields); $standardLabels = $viewLabels->getLabels(); @@ -270,10 +270,6 @@ protected function replaceFileUpload(&$stub, array $fields) */ protected function getFileUploadAttribute($template) { - if ($this->isCollectiveTemplate($template)) { - return "'files' => true,"; - } - return ' enctype="multipart/form-data"'; } @@ -309,7 +305,7 @@ protected function getViewCommand($view) } /** - * It checks of a destination view exists or not + * It checks of a destination view exists or not. * * @param string $viewsDirectory * @param string $routesPrefix @@ -325,7 +321,7 @@ protected function isViewExists($viewsDirectory, $routesPrefix, $viewName) } /** - * It called tha create-locale command to generate the locale config + * It calls the create-locale command to generate the locale config * * @param string $langFile * @param string $fields @@ -395,7 +391,7 @@ protected function getHeaderFieldAccessor(array $fields, $modelName) } /** - * Gets a new instance of the proper html generator. + * Gets a new instance of the proper HTML generator. * * @param array $fields * @param string $modelName @@ -405,10 +401,6 @@ protected function getHeaderFieldAccessor(array $fields, $modelName) */ protected function getHtmlGenerator(array $fields, $modelName, $template) { - if ($this->isCollectiveTemplate($template)) { - return new LaravelCollectiveHtml($fields, $modelName, $template); - } - return new StandardHtml($fields, $modelName, $template); } diff --git a/src/Commands/Framework/CreateLanguageCommand.php b/src/Commands/Framework/CreateLanguageCommand.php index 97fb89d..5b60d4e 100644 --- a/src/Commands/Framework/CreateLanguageCommand.php +++ b/src/Commands/Framework/CreateLanguageCommand.php @@ -83,7 +83,7 @@ protected function getAvailableLabels(Resource $resource, $modelName) $languages[$lang] = array_merge($current, $docLabels); } - $viewLabels = new ViewLabelsGenerator($modelName, $resource->fields, $this->isCollectiveTemplate()); + $viewLabels = new ViewLabelsGenerator($modelName, $resource->fields); $standardLabels = $viewLabels->getTranslatedLabels(array_keys($languages)); //Merge the standard labels to the fields label diff --git a/src/Support/Config.php b/src/Support/Config.php index a2c207b..1f49901 100644 --- a/src/Support/Config.php +++ b/src/Support/Config.php @@ -157,16 +157,6 @@ public static function getKeyPatterns() return self::getArrayBaseValue('common_key_patterns'); } - /** - * Gets the template names that uses Laravel-Collective - * - * @return array - */ - public static function getCollectiveTemplates() - { - return self::getArrayBaseValue('laravel_collective_templates'); - } - /** * Gets the default template name. * diff --git a/src/Support/ViewLabelsGenerator.php b/src/Support/ViewLabelsGenerator.php index 80cc141..0935142 100644 --- a/src/Support/ViewLabelsGenerator.php +++ b/src/Support/ViewLabelsGenerator.php @@ -42,19 +42,12 @@ class ViewLabelsGenerator */ protected $defaultLang; - /** - * Generate labels for collective template? - * - * @var bool - */ - protected $isCollectiveTemplate; - /** * Create a new transformer instance. * * @return void */ - public function __construct($modelName, array $fields, $isCollectiveTemplate) + public function __construct($modelName, array $fields) { if (empty($modelName)) { throw new Exception("$modelName must have a valid value"); @@ -64,7 +57,6 @@ public function __construct($modelName, array $fields, $isCollectiveTemplate) $this->fields = $fields; $this->localeGroup = self::makeLocaleGroup($modelName); $this->defaultLang = App::getLocale(); - $this->isCollectiveTemplate = $isCollectiveTemplate; } /** @@ -142,21 +134,7 @@ protected function makeModelLabel($key, array $properties, $isPlain, $lang) $label = new Label($text, $this->localeGroup, $isPlain, $lang, $key); $label->template = $properties['template']; - $label->isInFunction = $this->isInFunction($properties); return $label; } - - /** - * Checks if the given properties request to put the label in a function. - * - * @param array $properties - * - * @return bool - */ - protected function isInFunction(array $properties) - { - return $this->isCollectiveTemplate - && (isset($properties['in-function-with-collective']) && $properties['in-function-with-collective']); - } } diff --git a/src/Traits/CommonCommand.php b/src/Traits/CommonCommand.php index 80c9b66..8d47825 100644 --- a/src/Traits/CommonCommand.php +++ b/src/Traits/CommonCommand.php @@ -514,7 +514,7 @@ protected function getPathToTemplates($templateName = null) $templateName = $templateName ?: Config::getDefaultTemplateName(); $path = base_path(Config::getTemplatesPath() . Helpers::getPathWithSlash($templateName)); - if (!File::isDirectory($path) && in_array($templateName, ['default', 'default-collective'])) { + if (!File::isDirectory($path) && in_array($templateName, ['default'])) { // If the default templates are not published, utilize the default package path. $path = __DIR__ . '/../../templates/' . $templateName; @@ -527,18 +527,6 @@ protected function getPathToTemplates($templateName = null) return Helpers::getPathWithSlash($path); } - /** - * Checks the given template if it is a Laravel-Collective template or not. - * - * @param string $template - * - * @return bool - */ - protected function isCollectiveTemplate($template = null) - { - return in_array($template ?: $this->getTemplateName(), Config::getCollectiveTemplates()); - } - /** * Checks if a given fields array contains at least one file field *