Skip to content

Commit

Permalink
=Releases v2.1 for production
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek committed Jun 30, 2017
1 parent 3e45f80 commit e238ca1
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 156 deletions.
78 changes: 59 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# An awesome code generator for laravel framework - with client-side validation

For full documentation and live demo please visit <a href="https://crestapps.com/laravel-code-generator/docs/2.0" target="_blank" title="Laravel Code Generator Documentation">CrestApps.com</a>
For full documentation and live demo please visit <a href="https://crestapps.com/laravel-code-generator/docs/2.1" target="_blank" title="Laravel Code Generator Documentation">CrestApps.com</a>

## Introduction

Expand Down Expand Up @@ -31,10 +31,6 @@ A clean code generator for Laravel framework that will save you time! This aweso
* Create a very clean and reusable code.
* Lots of documentation.

## Prologue
* <a href="https://crestapps.com/laravel-code-generator/docs/2.0#release-notes">Release Notes</a>
* <a href="https://crestapps.com/laravel-code-generator/docs/2.0#upgrade-guide">Upgrade Guide</a>

## Installation

To download this package into your laravel project, use the command-line to execute the following command
Expand All @@ -43,7 +39,7 @@ A clean code generator for Laravel framework that will save you time! This aweso
composer require crestapps/laravel-code-generator --dev
```

To bootstrap the packages into your project, open the `config/app.php` file in your project. Then, look for the providers array.
**(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.

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

Expand All @@ -58,34 +54,78 @@ php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServ

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

## Available Commands

> The command in between the square brackets [] must be replaced with a variable of your choice.
* php artisan create:resources [model-name]
* php artisan create:layout [application-name]
* php artisan create:controller [controller-name]
* php artisan create:resources [model-name]
* php artisan create:mapped-resources
* php artisan create:controller [model-name]
* php artisan create:model [model-name]
* php artisan create:routes [controller-name]
* php artisan create:routes [model-name]
* php artisan create:views [model-name]
* php artisan create:create-view [model-name]
* php artisan create:edit-view [model-name]
* php artisan create:index-view [model-name]
* php artisan create:show-view [model-name]
* php artisan create:form-view [model-name]
* php artisan create:migration [table-name]
* php artisan create:form-request [class-name]
* php artisan create:language [language-file-name]
* php artisan create:fields-file [table-name]
* php artisan fields-file:create [file-name]
* php artisan fields-file:append [file-name]
* php artisan fields-file:reduce [file-name]
* php artisan create:migration [model-name]
* php artisan create:form-request [model-name]
* php artisan create:language [model-name]
* php artisan create:fields-file [model-name]
* php artisan fields-file:create [model-name]
* php artisan fields-file:append [model-name]
* php artisan fields-file:reduce [model-name]
* php artisan fields-file:delete [model-name]

> Full documentation available at [CrestApps.com](https://www.crestapps.com/laravel-code-generator/docs/2.1 "Laravel Code Generator Documentation").
> Live demo is available at [CrestApps.com](https://www.crestapps.com/laravel-code-generator/demos/v2-1 "Laravel Code Generator Live Demo").

## Examples

Lets create a CRUD called <var>AssetCategory</var> with the fields listed below.

> Full documentation available at [CrestApps.com](https://www.crestapps.com/laravel-code-generator/docs/2.0 "Laravel Code Generator Documentation").
- id
- name
- description
- is_active

> Live demo is available at [CrestApps.com](https://www.crestapps.com/laravel-code-generator/demos/2.0 "Laravel Code Generator Live Demo").

### Basic example

<blockquote>
<p><code>php artisan fields-file:create AssetCategory --names=id,name,description,is_active</code></p>
<p><small>The above command will create fields-file names <var>/resources/codegenerator-files/asset_categories.json</var></small></p>
<p><code>php artisan create:resources AssetCategory</code></p>
<p><small>The above command will create a model <var>app/Models/AssetCategory</var>, a controller <var>app/Http/Controllers/AsseyCategoriesController, all views, the routes, and migration file!</var></small></p>
</blockquote>


### Basic example using translations for english and arabic

<blockquote>
<p><code>php artisan fields-file:create AssetCategory --names=id,name,description,is_active --translation-for=en,ar</code></p>
<p><small>The above command will create fields-file names <var>/resources/codegenerator-files/asset_categories.json</var></small></p>
<p><code>php artisan create:resources AssetCategory</code></p>
<p><small>The above command will create a model <var>app/Models/AssetCategory</var>, a controller <var>app/Http/Controllers/AsseyCategoriesController, all views, the routes, and migration file!</var></small></p>
</blockquote>


### Creating resources from existing database with translation for english and arabic

<blockquote>
<p><code>php artisan create:resources AssetCategory --table-exists --translation-for=en,ar</code></p>
<p><small>The above command will create fields-file names <var>/resources/codegenerator-files/asset_categories.json</var></small></p>
<p><small>Then it will create a model <var>app/Models/AssetCategory</var>, a controller <var>app/Http/Controllers/AsseyCategoriesController, all views and the routes!</var></small></p>
<p><small>You may also create a fields-file from existing database separately using <code>create:fields-file AssetCategory --translation-for=en,ar</code></small></p>
</blockquote>

## Prologue
* <a href="https://crestapps.com/laravel-code-generator/docs/2.1#release-notes">Release Notes</a>
* <a href="https://crestapps.com/laravel-code-generator/docs/2.1#upgrade-guide">Upgrade Guide</a>

## License

Expand Down
86 changes: 47 additions & 39 deletions src/Commands/CreateControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CreateControllerCommand extends Command
{--with-form-request : This will extract the validation into a request form class.}
{--with-auth : Generate the controller with Laravel auth middlewear. }
{--template-name= : The template name to use when generating the code.}
{--form-request-directory= : The directory of the form-request.}
{--controller-extends=Http\Controllers\Controller : The base controller to be extend.}
{--force : This option will override the controller if one already exists.}';

Expand All @@ -57,16 +58,6 @@ class CreateControllerCommand extends Command
*/
protected $requestVariable = '$request';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return $this->getStubByName('controller', $this->getTemplateName());
}

/**
* Build the model class with the given name.
*
Expand All @@ -77,7 +68,7 @@ protected function getStub()
public function handle()
{
$input = $this->getCommandInput();
$destenationFile = $this->getDestenationFile($input->controllerName);
$destenationFile = $this->getDestenationFile($input->controllerName, $input->controllerDirectory);

if ($this->alreadyExists($destenationFile)) {
$this->error('The controller already exists!');
Expand All @@ -90,18 +81,18 @@ public function handle()

if ($input->withFormRequest) {
$requestName = $input->formRequestName;
$requestNameSpace = $this->getRequestsNamespace($requestName);
$requestNameSpace = $this->getRequestsNamespace($requestName, $input->formRequestDirectory);
$this->makeFormRequest($input);
}

$fields = $this->getFields($input->fields, $input->langFile, $input->fieldsFile);
$viewVariablesForIndex = $this->getCompactVariablesFor($fields, $this->getPluralVariable($input->modelName), 'index');
$viewVariablesForShow = $this->getCompactVariablesFor($fields, $this->getSingularVariable($input->modelName), 'show');
$viewVariablesForEdit = $this->getCompactVariablesFor($fields, $this->getSingularVariable($input->modelName), 'form');
$modelFullName = $this->getModelFullName($input->modelDirectory, $input->modelName);
$modelNamespace = $this->getModelNamespace($input->modelName, $input->modelDirectory);
$affirmMethod = $this->getAffirmMethod($input->withFormRequest, $fields, $requestNameSpace);
$classToExtendFullname = $this->getFullClassToExtend($input->extends);
$namespacesToUse = $this->getRequiredUseClasses($fields, [$modelFullName, $requestNameSpace, $classToExtendFullname]);
$namespacesToUse = $this->getRequiredUseClasses($fields, [$modelNamespace, $requestNameSpace, $classToExtendFullname]);
$dataMethod = $this->getDataMethod($fields, $requestNameSpace . '\\' . $requestName, $input->withFormRequest);
$stub = $this->getStubContent('controller');
$languages = array_keys(Helpers::getLanguageItems($fields));
Expand All @@ -112,7 +103,7 @@ public function handle()
->replaceGetDataMethod($stub, $dataMethod)
->replaceCallDataMethod($stub, $this->getCallDataMethod($input->withFormRequest))
->replaceModelName($stub, $input->modelName)
->replaceNamespace($stub, $this->getControllersNamespace())
->replaceNamespace($stub, $this->getControllersNamespace($input->controllerDirectory))
->replaceControllerExtends($stub, $this->getControllerExtends($classToExtendFullname))
->replaceUseCommandPlaceholder($stub, $namespacesToUse)
->replaceRouteNames($stub, $this->getModelName($input->modelName), $input->prefix)
Expand Down Expand Up @@ -611,37 +602,50 @@ protected function getUploadFileMethod(array $fields, $withFormRequest)
}

/**
* Gets the controller's fullname
* Gets the destenation file to be created.
*
* @param string $name
* @param string $path
*
* @return string
*/
protected function getDestenationFile($name)
protected function getDestenationFile($name, $path)
{
$path = app_path(Config::getControllersPath());

return Helpers::postFixWith($path, '/') . $name . '.php';
if (!empty($path)) {
$path = Helpers::getPathWithSlash(ucfirst($path));
}

return app_path(Config::getControllersPath($path. $name . '.php'));
}

/**
* Gets the Requests namespace
*
* @param string $name
* @param string $path
*
* @return string
*/
protected function getRequestsNamespace($name)
protected function getRequestsNamespace($name, $path)
{
$path = $this->getAppNamespace() . Config::getRequestsPath();
$path = str_finish($path, '\\');

$path = $this->getAppNamespace() . Config::getRequestsPath($path);

return Helpers::convertSlashToBackslash($path) . $name;
}

/**
* Gets the controllers namespace
*
* @param string $path
*
* @return string
*/
protected function getControllersNamespace()
protected function getControllersNamespace($path)
{
$path = $this->getAppNamespace() . Config::getControllersPath();
$path = $this->getAppNamespace() . Config::getControllersPath($path);

return rtrim(Helpers::convertSlashToBackslash($path), '\\');
}
Expand Down Expand Up @@ -673,31 +677,34 @@ protected function makeFormRequest($input)
{
$this->callSilent('create:form-request',
[
'model-name' => $input->modelName,
'--class-name' => $input->formRequestName,
'--fields' => $input->fields,
'--force' => $input->force,
'--with-auth' => $input->withAuth,
'--fields-file' => $input->fieldsFile,
'--template-name' => $input->template
'model-name' => $input->modelName,
'--class-name' => $input->formRequestName,
'--fields' => $input->fields,
'--force' => $input->force,
'--with-auth' => $input->withAuth,
'--fields-file' => $input->fieldsFile,
'--template-name' => $input->template,
'--form-request-directory' => $input->formRequestDirectory
]);

return $this;
}

/**
* Gets the full model name
* Gets the namespace of the model
*
* @param string $directory
* @param string $name
* @param string $modelName
* @param string $modelDirectory
*
* @return string
*/
protected function getModelFullName($directory, $name)
protected function getModelNamespace($modelName, $modelDirectory)
{
$final = !empty($directory) ? Config::getModelsPath() . Helpers::getPathWithSlash($directory) : Config::getModelsPath();
$modelDirectory = str_finish($modelDirectory, '\\');

$namespace = $this->getAppNamespace() . Config::getModelsPath($modelDirectory . $modelName);

return Helpers::convertSlashToBackslash($this->getAppNamespace() . $final . $name);
return rtrim(Helpers::convertSlashToBackslash($namespace), '\\');
}

/**
Expand All @@ -710,7 +717,6 @@ protected function getCommandInput()
$modelName = trim($this->argument('model-name'));
$cName = trim($this->option('controller-name'));
$controllerName = $cName ? str_finish($cName, 'Controller') : Helpers::makeControllerName($modelName);
;
$viewDirectory = $this->option('views-directory');
$prefix = $this->option('routes-prefix');
$perPage = intval($this->option('models-per-page'));
Expand All @@ -720,14 +726,16 @@ protected function getCommandInput()
$withFormRequest = $this->option('with-form-request');
$force = $this->option('force');
$modelDirectory = $this->option('model-directory');
$controllerDirectory = trim($this->option('controller-directory'));
$formRequestName = Helpers::makeFormRequestName($modelName);
$template = $this->getTemplateName();
$formRequestDirectory = trim($this->option('form-request-directory'));
$extends = $this->generatorOption('controller-extends');
$withAuth = $this->option('with-auth');

return (object) compact('viewDirectory', 'viewName', 'modelName', 'prefix', 'perPage', 'fileSnippet', 'modelDirectory',
return (object) compact('formRequestDirectory','viewDirectory', 'viewName', 'modelName', 'prefix', 'perPage', 'fileSnippet', 'modelDirectory',
'langFile', 'fields', 'withFormRequest', 'formRequestName', 'force', 'fieldsFile', 'template',
'controllerName', 'extends', 'withAuth');
'controllerName', 'extends', 'withAuth','controllerDirectory');
}

/**
Expand Down
Loading

0 comments on commit e238ca1

Please sign in to comment.