Skip to content

Commit

Permalink
cleanup collective
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek committed Jan 24, 2024
1 parent 91b08bb commit c279b33
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 128 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ For full documentation and live demo please visit <a href="https://crestapps.com
<li>Allows you to save the fields in a JSON file and recreate resources when the business needs changes.</li>
<li>Utilizes JSON based resource-file to allow you to define your resources. Resource-file allows you to easily regenerate the resource at any time even when the business rules change.</li>
<li>Create standard CRUD controllers with simple or form-request validation.</li>
<li>Customizable view’s templates to enable you to change the standard look and feel of your application.</li>
<li>Customizable view templates to enable you to change the standard look and feel of your application.</li>
<li>Create model with relations.</li>
<li>Create named routes with and without group.</li>
<li>Create standard CRUD views.</li>
Expand Down
21 changes: 1 addition & 20 deletions config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@
*/
'templates_path' => 'resources/laravel-code-generator/templates',

/*
|--------------------------------------------------------------------------
| Array of templates that should be generated with Laravel-Collective.
|--------------------------------------------------------------------------
|
| If you want to generate code by using laravel-collective, you must first
| install the package. Then add the template name that should be using
| Laravel-Collective extensions when generating code.
|
*/
'laravel_collective_templates' => [
'default-collective',
],

/*
|--------------------------------------------------------------------------
| The default path of where the uploaded files live.
Expand All @@ -59,7 +45,7 @@

/*
|--------------------------------------------------------------------------
| The default output format for datetime fields.
| The default output format for DateTime fields.
|--------------------------------------------------------------------------
|
| This output format can also be changed at the field level using the
Expand Down Expand Up @@ -546,7 +532,6 @@
| The "key" of the array is the value to be used in the locale files.
| The "text" key of the sub array, is the string to display in the view or add to the locale files.
| The "template" key of the sub array, is the string to be use in the view for replacement.
| The "in-function-with-collective" key of the sub array, tell the generator that,
| this string would be used in a function or not.
|
| The following templates can be used. Assuming the model name is AssetCategory
Expand Down Expand Up @@ -583,7 +568,6 @@
'delete' => [
'text' => 'Delete [% model_name_title %]',
'template' => 'delete_model',
'in-function-with-collective' => true,
],
'edit' => [
'text' => 'Edit [% model_name_title %]',
Expand All @@ -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.',
Expand Down
5 changes: 0 additions & 5 deletions src/CodeGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Api/CreateApiControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down
55 changes: 17 additions & 38 deletions src/Commands/Api/CreateApiResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.}';

Expand All @@ -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))
Expand All @@ -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);
}

Expand All @@ -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.');
Expand All @@ -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');
}
Expand All @@ -174,15 +155,13 @@ 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'));

return (object) compact(
'modelName',
'resourceFile',
'template',
'isCollection',
'modelDirectory'
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ApiDocs/CreateApiDocsViewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Bases/ControllerCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 4 additions & 12 deletions src/Commands/Bases/ViewsCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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"';
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Framework/CreateLanguageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions src/Support/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
24 changes: 1 addition & 23 deletions src/Support/ViewLabelsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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']);
}
}
Loading

0 comments on commit c279b33

Please sign in to comment.