├── .gitignore ├── LICENSE ├── README.md ├── Upgrade_Guide.md ├── composer.json ├── config └── generator.php ├── installation.md ├── samples └── fields.json ├── src ├── .gitignore └── Mitul │ ├── Controller │ └── AppBaseController.php │ └── Generator │ ├── CommandData.php │ ├── Commands │ ├── APIGeneratorCommand.php │ ├── BaseCommand.php │ ├── PublisherCommand.php │ ├── ScaffoldAPIGeneratorCommand.php │ └── ScaffoldGeneratorCommand.php │ ├── Errors.php │ ├── File │ └── FileHelper.php │ ├── FormFieldsGenerator.php │ ├── GeneratorServiceProvider.php │ ├── Generators │ ├── API │ │ └── APIControllerGenerator.php │ ├── Common │ │ ├── MigrationGenerator.php │ │ ├── ModelGenerator.php │ │ ├── RepositoryGenerator.php │ │ ├── RequestGenerator.php │ │ └── RoutesGenerator.php │ ├── GeneratorProvider.php │ └── Scaffold │ │ ├── ViewControllerGenerator.php │ │ └── ViewGenerator.php │ ├── SchemaGenerator.php │ ├── TemplatesHelper.php │ └── Utils │ ├── GeneratorUtils.php │ └── TableFieldsGenerator.php ├── templates ├── api │ └── Controller.stub ├── common │ ├── Migration.stub │ ├── Model.stub │ └── Repository.stub ├── controller │ └── AppBaseController.stub ├── routes │ ├── api_routes.stub │ ├── api_routes_group.stub │ ├── dingo_api_routes_group.stub │ └── scaffold_routes.stub └── scaffold │ ├── Controller.stub │ ├── requests │ ├── CreateRequest.stub │ └── UpdateRequest.stub │ └── views │ ├── create.blade.stub │ ├── edit.blade.stub │ ├── field.blade.stub │ ├── fields.blade.stub │ ├── index.blade.stub │ ├── paginate.blade.stub │ ├── show.blade.stub │ ├── show_field.blade.stub │ └── table.blade.stub └── views └── common ├── errors.blade.php └── paginate.blade.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.lock 3 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mitul Golakya 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Laravel API/Scaffold/CRUD Generator 2 | ======================= 3 | [![Latest Stable Version](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/v/stable)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) [![Total Downloads](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/downloads)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) [![Monthly Downloads](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/d/monthly)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) [![Daily Downloads](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/d/daily)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) [![Latest Unstable Version](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/v/unstable)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) [![License](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/license)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) 4 | 5 | # THIS PACKAGE IS DEPRECATED. USE NEW ONE FROM [HERE](https://github.com/InfyOmLabs/laravel-generator) 6 | 7 | # IF YOU STILL WANT TO USE THIS PACKAGE. [HERE](https://github.com/mitulgolakiya/laravel-api-generator/blob/master/installation.md) ARE THE INSTALLATION STEPS. 8 | -------------------------------------------------------------------------------- /Upgrade_Guide.md: -------------------------------------------------------------------------------- 1 | Laravel API/Scaffold/CRUD Generator Upgrade Guide (Laravel5) 2 | ======================= 3 | 4 | Upgrade Guide from 1.2 to 1.3 5 | ------------------------------------- 6 | 7 | We are no longer using our own ```APIExceptionsHandler``` to send API fail responses and using Laravel's own ```HttpResponseException``` to overcome ```App\Exceptions\Handler``` overwrite problem. 8 | 9 | So we removed all extra Exception files. so you need to remove those things from your API Controllers. 10 | 11 | 1. In all your API Controllers and find ```throw new RecordNotFoundException```. 12 | 13 | 2. Replace it with ```$this->throwRecordNotFoundException```. 14 | 15 | 3. Remove use statements 16 | 17 | use Mitul\Generator\Exceptions\AppValidationException; 18 | use Mitul\Generator\Exceptions\RecordNotFoundException; 19 | 20 | 4. Remove throw statement from PHPDoc Blocks of functions 21 | 22 | @throws AppValidationException 23 | @throws RecordNotFoundException 24 | 25 | 5. Enjoy Upgrade :) 26 | 27 | Upgrade Guide from 1.0 to 1.1 or 1.2 28 | ------------------------------------- 29 | 30 | 1. Take a backup of your ```config/generator.php``` 31 | 32 | 2. Delete your ```config/generator.php``` 33 | 34 | 3. Change version in composer.json 35 | 36 | "require": { 37 | "mitulgolakiya/laravel-api-generator": "1.2.*" 38 | } 39 | 40 | 4. Run composer update. 41 | 42 | 5. Run publish command again. 43 | 44 | php artisan vendor:publish --provider="Mitul\Generator\GeneratorServiceProvider" 45 | 46 | 6. Replace your custom paths again in ```config/generator.php```. 47 | 48 | 7. Enjoy Upgrade :) 49 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mitulgolakiya/laravel-api-generator", 3 | "description": "Laravel API/Scaffold/CRUD Generator from just one command with including Controller, Repository, Model, Migrations, routes.php update.", 4 | "keywords": [ 5 | "laravel", 6 | "api", 7 | "model", 8 | "migration", 9 | "scaffold", 10 | "CRUD", 11 | "generator" 12 | ], 13 | "license": "MIT", 14 | "authors": [ 15 | { 16 | "name": "Mitul Golakiya", 17 | "email": "me@mitul.me" 18 | } 19 | ], 20 | "require": { 21 | "php": ">=5.5.9", 22 | "illuminate/support": "5.2.*" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Mitul\\": "src/Mitul/" 27 | } 28 | }, 29 | "suggest": { 30 | "doctrine/dbal": "Required to use generator from existing table.", 31 | "infyomlabs/laravel-generator": "This package is deprecated. we recommend to use this package instead of current package." 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /config/generator.php: -------------------------------------------------------------------------------- 1 | 'Mitul\Controller\AppBaseController', 15 | 16 | /* 17 | |-------------------------------------------------------------------------- 18 | | Path for classes 19 | |-------------------------------------------------------------------------- 20 | | 21 | | All Classes will be created on these relevant path 22 | | 23 | */ 24 | 25 | 'path_migration' => base_path('database/migrations/'), 26 | 27 | 'path_model' => app_path('Models/'), 28 | 29 | 'path_repository' => app_path('Libraries/Repositories/'), 30 | 31 | 'path_controller' => app_path('Http/Controllers/'), 32 | 33 | 'path_api_controller' => app_path('Http/Controllers/API/'), 34 | 35 | 'path_views' => base_path('resources/views/'), 36 | 37 | 'path_request' => app_path('Http/Requests/'), 38 | 39 | 'path_routes' => app_path('Http/routes.php'), 40 | 41 | 'path_api_routes' => app_path('Http/api_routes.php'), 42 | 43 | /* 44 | |-------------------------------------------------------------------------- 45 | | Namespace for classes 46 | |-------------------------------------------------------------------------- 47 | | 48 | | All Classes will be created with these namespaces 49 | | 50 | */ 51 | 52 | 'namespace_model' => 'App\Models', 53 | 54 | 'namespace_repository' => 'App\Libraries\Repositories', 55 | 56 | 'namespace_controller' => 'App\Http\Controllers', 57 | 58 | 'namespace_api_controller' => 'App\Http\Controllers\API', 59 | 60 | 'namespace_request' => 'App\Http\Requests', 61 | 62 | /* 63 | |-------------------------------------------------------------------------- 64 | | Model extend 65 | |-------------------------------------------------------------------------- 66 | | 67 | | Model extend Configuration. 68 | | By default Eloquent model will be used. 69 | | If you want to extend your own custom model then you can specify "model_extend" => true and "model_extend_namespace" & "model_extend_class". 70 | | 71 | | e.g. 72 | | 'model_extend' => true, 73 | | 'model_extend_namespace' => 'App\Models\AppBaseModel as AppBaseModel', 74 | | 'model_extend_class' => 'AppBaseModel', 75 | | 76 | */ 77 | 78 | 'model_extend_class' => 'Illuminate\Database\Eloquent\Model', 79 | 80 | /* 81 | |-------------------------------------------------------------------------- 82 | | API routes prefix 83 | |-------------------------------------------------------------------------- 84 | | 85 | | By default "api" will be prefix 86 | | 87 | */ 88 | 89 | 'api_prefix' => 'api', 90 | 91 | 'api_version' => 'v1', 92 | 93 | /* 94 | |-------------------------------------------------------------------------- 95 | | dingo API integration 96 | |-------------------------------------------------------------------------- 97 | | 98 | | By default dingo API Integration will not be enabled. Dingo API is in beta. 99 | | 100 | */ 101 | 102 | 'use_dingo_api' => false, 103 | 104 | ]; 105 | -------------------------------------------------------------------------------- /installation.md: -------------------------------------------------------------------------------- 1 | Laravel API/Scaffold/CRUD Generator 2 | ======================= 3 | [![Latest Stable Version](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/v/stable)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) [![Total Downloads](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/downloads)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) [![Monthly Downloads](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/d/monthly)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) [![Daily Downloads](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/d/daily)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) [![Latest Unstable Version](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/v/unstable)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) [![License](https://poser.pugx.org/mitulgolakiya/laravel-api-generator/license)](https://packagist.org/packages/mitulgolakiya/laravel-api-generator) 4 | 5 | ### Version Compability 6 | 7 | Laravel | Branch 8 | :---------|:------------ 9 | 5.0 | [1.3](https://github.com/mitulgolakiya/laravel-api-generator/tree/1.3) 10 | 5.1.* | [1.4](https://github.com/mitulgolakiya/laravel-api-generator/tree/1.4) 11 | 5.2.* | [master](https://github.com/mitulgolakiya/laravel-api-generator) 12 | 13 | I enjoy creating API's and I have worked on many projects that required them. But the problem I always faced was setting up all the boilerplate code. For example each end point needs a migration, model, controller, repository, and on and on. I wanted a way to streamline this process and that is how this package was born. 14 | 15 | This API generator allows you to use artisan commands to automatically generate all these files saving you time. Not only does it auto generate the files but it will set the namespaces. 16 | 17 | The artisan command can generate the following items: 18 | * Migration File 19 | * Model 20 | * Repository 21 | * Controller 22 | * View 23 | * index.blade.php 24 | * table.blade.php 25 | * show.blade.php 26 | * show_fields.blade.php 27 | * create.blade.php 28 | * edit.blade.php 29 | * fields.blade.php 30 | * adjusts routes.php 31 | 32 | And your simple CRUD and APIs are ready in mere seconds. 33 | 34 | Here is the full documentation. 35 | 36 | [Upgrade Guide](https://github.com/mitulgolakiya/laravel-api-generator/blob/master/Upgrade_Guide.md). 37 | 38 | # Documentation is in process... 39 | 40 | Documentation 41 | -------------- 42 | 43 | 1. [Installation](#installation) 44 | 2. [Configuration](#configuration) 45 | 3. [Publish & Initialization](#publish--initialization) 46 | 4. [Generator](#generator) 47 | 5. [Supported Field Types](#supported-field-types) 48 | 5. [Customization](#customization) 49 | 1. [Base Controller](#base-controller) 50 | 2. [Customize Templates](#customize-templates) 51 | 3. [Dingo API Integration](#dingo-api-integration) 52 | 6. [Options](#options) 53 | 1. [Paginate Records](#paginate-records) 54 | 2. [Model Soft Deletes](#model-soft-deletes) 55 | 3. [Fields From File](#fields-from-file) 56 | 4. [Custom Table Name](#custom-table-name) 57 | 5. [Skip Migration](#skip-migration) 58 | 6. [Remember Token](#remember-token) 59 | 7. [Generator from existing tables](#generator-from-existing-tables) 60 | 61 | ## Installation 62 | 63 | 1. Add this package to your composer.json: 64 | 65 | "require": { 66 | "laracasts/flash": "~1.3", 67 | "laravelcollective/html": "5.2.*", 68 | "bosnadev/repositories": "dev-master", 69 | "mitulgolakiya/laravel-api-generator": "dev-master" 70 | } 71 | 72 | 2. Run composer update 73 | 74 | composer update 75 | 76 | 3. Add the ServiceProviders to the providers array in ```config/app.php```.
77 | As we are using these two packages [laravelcollective/html](https://github.com/LaravelCollective/html) & [laracasts/flash](https://github.com/laracasts/flash) as a dependency.
78 | so we need to add those ServiceProviders as well. 79 | 80 | Collective\Html\HtmlServiceProvider::class, 81 | Laracasts\Flash\FlashServiceProvider::class, 82 | Mitul\Generator\GeneratorServiceProvider::class, 83 | 84 | Also for convenience, add these facades in alias array in ```config/app.php```. 85 | 86 | 'Form' => Collective\Html\FormFacade::class, 87 | 'Html' => Collective\Html\HtmlFacade::class, 88 | 'Flash' => Laracasts\Flash\Flash::class 89 | 90 | ## Configuration 91 | 92 | Publish Configuration file ```generator.php```. 93 | 94 | php artisan vendor:publish --provider="Mitul\Generator\GeneratorServiceProvider" 95 | 96 | Config file (```config/generator.php```) contains path for all generated files 97 | 98 | ```base_controller``` - Base Controller for all Controllers
99 | 100 | ```path_migration``` - Path where Migration file to be generated
101 | ```path_model``` - Path where Model file to be generated
102 | ```path_repository``` - Path where Repository file to be generated
103 | ```path_controller``` - Path where Controller file to be generated
104 | ```path_api_controller``` - Path where API Controller file to be generated
105 | ```path_views``` - Path where views will be created
106 | ```path_request``` - Path where request file will be created
107 | ```path_routes``` - Path of routes.php (if you are using any custom routes file)
108 | ```path_api_routes``` - Path of api_routes.php (this file will contain all api routes)
109 | 110 | ```namespace_model``` - Namespace of Model
111 | ```namespace_repository``` - Namespace of Repository
112 | ```namespace_controller``` - Namespace of Controller
113 | ```namespace_api_controller``` - Namespace of API Controller
114 | ```namespace_request``` - Namespace for Request
115 | 116 | ```model_extend_class``` - Extend class of Models
117 | 118 | ```api_prefix``` - API Prefix 119 | ```api_version``` - API Version 120 | 121 | ```use_dingo_api``` - Integrate APIs with dingo/api package 122 | 123 | ## Publish & Initialization 124 | 125 | Mainly, we need to do three basic things to get started. 126 | 1. Publish some common views like ```errors.blade.php``` & ```paginate.blade.php```. 127 | 2. Publish ```api_routes.php``` which will contain all our api routes. 128 | 3. Init ```routes.php``` for api routes. We need to include ```api_routes.php``` into main ```routes.php```. 129 | 130 | php artisan mitul.generator:publish 131 | 132 | ## Generator 133 | 134 | Fire artisan command to generate API, Scaffold with CRUD views or both API as well as CRUD views. 135 | 136 | Generate API: 137 | 138 | php artisan mitul.generator:api ModelName 139 | 140 | Generate CRUD Scaffold: 141 | 142 | php artisan mitul.generator:scaffold ModelName 143 | 144 | Generate CRUD Scaffold with API: 145 | 146 | php artisan mitul.generator:scaffold_api ModelName 147 | 148 | e.g. 149 | 150 | php artisan mitul.generator:api Project 151 | php artisan mitul.generator:api Post 152 | 153 | php artisan mitul.generator:scaffold Project 154 | php artisan mitul.generator:scaffold Post 155 | 156 | php artisan mitul.generator:scaffold_api Project 157 | php artisan mitul.generator:scaffold_api Post 158 | 159 | Here is the sample [fields input json](https://github.com/mitulgolakiya/laravel-api-generator/blob/master/samples/fields.json) 160 | 161 | ## Supported HTML Field Types 162 | 163 | Here is the list of supported field types with options: 164 | * text 165 | * textarea 166 | * password 167 | * email 168 | * file 169 | * checkbox 170 | * radio:male,female,option3,option4 171 | * number 172 | * date 173 | * select:India,USA 174 | 175 | ## Customization 176 | 177 | ### Base Controller 178 | 179 | If you want to use your own base controller or want to extend/modify default AppBaseController then you can have following options: 180 | 181 | 1. If you want to use another controller (recommended to extends AppBaseController with new controller) as base controller then modify ```base_controller``` value in ```config/generator.php``` 182 | 183 | 2. If you want to modify AppBaseController then, 184 | 185 | 1. Publish AppBaseController in your controllers path 186 | 187 | php artisan mitul.generator:publish --baseController 188 | 189 | 2. Modify the content of ```AppBaseController.php``` and set it as a ```base_controller``` in ```config/generator.php``` 190 | 191 | ### Customize Templates 192 | 193 | To use your own custom templates, 194 | 195 | 1. Publish templates to ```/resources/api-generator-templates``` 196 | 197 | php artisan mitul.generator:publish --templates 198 | 199 | 2. Leave only those templates that you want to change. Remove the templates that do not plan to change. 200 | 201 | ## Options 202 | 203 | ### Paginate Records 204 | 205 | To paginate records, you can specify paginate option, 206 | e.g. 207 | 208 | php artisan mitul.generator:api Post --paginate=10 209 | 210 | ### Model Soft Deletes 211 | 212 | To use SoftDelete, use softDelete option, 213 | 214 | php artisan mitul.generator:api Post --softDelete 215 | 216 | ### Fields From File 217 | 218 | If you want to pass fields from file then you can create fields json file and pass it via command line. Here is the sample [fields.json](https://github.com/mitulgolakiya/laravel-api-generator/blob/master/samples/fields.json) 219 | 220 | You have to pass option ```--fieldsFile=absolute_file_path_or_path_from_base_directory``` with command. e.g. 221 | 222 | php artisan mitul.generator:scaffold_api Post --fieldsFile="/Users/Mitul/laravel-api-generator/fields.json" 223 | php artisan mitul.generator:scaffold_api Post --fieldsFile="fields.json" 224 | 225 | ### Custom Table Name 226 | 227 | You can also specify your own custom table name by, 228 | 229 | php artisan mitul.generator:api Post --tableName=custom_table_name 230 | 231 | ### Skip Migration 232 | 233 | You can also skip migration generation, 234 | 235 | php artisan mitul.generator:api Post --skipMigration 236 | 237 | ### Remember Token 238 | 239 | To generate rememberToken field in migration file, 240 | 241 | php artisan mitul.generator:api Post --rememberToken 242 | 243 | ## Generator from existing tables 244 | 245 | To use generator with existing table, you can specify ```--fromTable``` option. ```--tableName``` option is required and you need to specify table name. 246 | 247 | Just make sure, you have installed ```doctrine/dbal``` package. 248 | 249 | **Limitation:** As of now it is not fully working (work is in progress). It will not create migration file. You need to tweak some of the things in your generated files like timestamps, primary key etc. 250 | 251 | php artisan mitul.generator:api Post --fromTable --tableName=posts 252 | 253 | Credits 254 | -------- 255 | 256 | This API Generator is created by [Mitul Golakiya](https://github.com/mitulgolakiya). 257 | 258 | **Bugs & Forks are welcomed :)** 259 | -------------------------------------------------------------------------------- /samples/fields.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "field":"title:string", 4 | "type":"text", 5 | "validations": "required" 6 | }, 7 | { 8 | "field":"body:text", 9 | "type":"textarea", 10 | "validations": "" 11 | }, 12 | { 13 | "field":"password:string", 14 | "type":"password", 15 | "validations": "" 16 | }, 17 | { 18 | "field":"email:string", 19 | "type":"email", 20 | "validations": "" 21 | }, 22 | { 23 | "field":"profile:string", 24 | "type":"file", 25 | "validations": "" 26 | }, 27 | { 28 | "field":"remember:boolean", 29 | "type":"checkbox", 30 | "validations": "" 31 | }, 32 | { 33 | "field":"gender:integer", 34 | "type":"radio:male,female", 35 | "validations": "" 36 | }, 37 | { 38 | "field":"order:integer", 39 | "type":"number", 40 | "validations": "" 41 | }, 42 | { 43 | "field":"birth_date:timestamp", 44 | "type":"date", 45 | "validations": "" 46 | }, 47 | { 48 | "field":"location:string", 49 | "type":"select:Surat,Mumbai", 50 | "validations": "" 51 | } 52 | ] -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) -------------------------------------------------------------------------------- /src/Mitul/Controller/AppBaseController.php: -------------------------------------------------------------------------------- 1 | getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes); 23 | 24 | if ($validator->fails()) { 25 | throw new HttpException(400, json_encode($validator->errors()->getMessages())); 26 | } 27 | } 28 | 29 | public function makeResponse($result, $message) 30 | { 31 | return [ 32 | 'data' => $result, 33 | 'message' => $message, 34 | ]; 35 | } 36 | 37 | public function sendResponse($result, $message) 38 | { 39 | return Response::json($this->makeResponse($result, $message)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Mitul/Generator/CommandData.php: -------------------------------------------------------------------------------- 1 | commandObj = $commandObj; 58 | $this->commandType = $commandType; 59 | $this->fileHelper = new FileHelper(); 60 | $this->templatesHelper = new TemplatesHelper(); 61 | } 62 | 63 | public function initVariables() 64 | { 65 | $this->modelNamePlural = Str::plural($this->modelName); 66 | $this->modelNameCamel = Str::camel($this->modelName); 67 | $this->modelNamePluralCamel = Str::camel($this->modelNamePlural); 68 | $this->initDynamicVariables(); 69 | } 70 | 71 | public function getInputFields() 72 | { 73 | $fields = []; 74 | 75 | $this->commandObj->info('Specify fields for the model (skip id & timestamp fields, will be added automatically)'); 76 | $this->commandObj->info('Enter exit to finish'); 77 | 78 | while (true) { 79 | $fieldInputStr = $this->commandObj->ask('Field: (field_name:field_database_type)', ''); 80 | 81 | if (empty($fieldInputStr) || $fieldInputStr == false || $fieldInputStr == 'exit') { 82 | break; 83 | } 84 | 85 | if (!GeneratorUtils::validateFieldInput($fieldInputStr)) { 86 | $this->commandObj->error('Invalid Input. Try again'); 87 | continue; 88 | } 89 | 90 | $type = $this->commandObj->ask('Enter field html input type (text): ', 'text'); 91 | 92 | $validations = $this->commandObj->ask('Enter validations: ', false); 93 | 94 | $validations = ($validations == false) ? '' : $validations; 95 | 96 | $fields[] = GeneratorUtils::processFieldInput($fieldInputStr, $type, $validations); 97 | } 98 | 99 | return $fields; 100 | } 101 | 102 | public function initDynamicVariables() 103 | { 104 | $this->dynamicVars = self::getConfigDynamicVariables(); 105 | 106 | $this->dynamicVars = array_merge($this->dynamicVars, [ 107 | '$MODEL_NAME$' => $this->modelName, 108 | 109 | '$MODEL_NAME_CAMEL$' => $this->modelNameCamel, 110 | 111 | '$MODEL_NAME_PLURAL$' => $this->modelNamePlural, 112 | 113 | '$MODEL_NAME_PLURAL_CAMEL$' => $this->modelNamePluralCamel, 114 | ]); 115 | 116 | if ($this->tableName) { 117 | $this->dynamicVars['$TABLE_NAME$'] = $this->tableName; 118 | } else { 119 | $this->dynamicVars['$TABLE_NAME$'] = $this->modelNamePluralCamel; 120 | } 121 | } 122 | 123 | public function addDynamicVariable($name, $val) 124 | { 125 | $this->dynamicVars[$name] = $val; 126 | } 127 | 128 | public static function getConfigDynamicVariables() 129 | { 130 | return [ 131 | 132 | '$BASE_CONTROLLER$' => Config::get('generator.base_controller', 'Mitul\Controller\AppBaseController'), 133 | 134 | '$NAMESPACE_CONTROLLER$' => Config::get('generator.namespace_controller', 'App\Http\Controllers'), 135 | 136 | '$NAMESPACE_API_CONTROLLER$' => Config::get('generator.namespace_api_controller', 'App\Http\Controllers\API'), 137 | 138 | '$NAMESPACE_REQUEST$' => Config::get('generator.namespace_request', 'App\Http\Requests'), 139 | 140 | '$NAMESPACE_REPOSITORY$' => Config::get('generator.namespace_repository', 'App\Libraries\Repositories'), 141 | 142 | '$NAMESPACE_MODEL$' => Config::get('generator.namespace_model', 'App\Models'), 143 | 144 | '$NAMESPACE_MODEL_EXTEND$' => Config::get('generator.model_extend_class', 'Illuminate\Database\Eloquent\Model'), 145 | 146 | '$SOFT_DELETE_DATES$' => "\n\tprotected \$dates = ['deleted_at'];\n", 147 | 148 | '$SOFT_DELETE$' => "use SoftDeletes;\n", 149 | 150 | '$SOFT_DELETE_IMPORT$' => "use Illuminate\\Database\\Eloquent\\SoftDeletes;\n", 151 | 152 | '$API_PREFIX$' => Config::get('generator.api_prefix', 'api'), 153 | 154 | '$API_VERSION$' => Config::get('generator.api_version', 'v1'), 155 | 156 | '$PRIMARY_KEY$' => 'id', 157 | ]; 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Commands/APIGeneratorCommand.php: -------------------------------------------------------------------------------- 1 | commandData = new CommandData($this, CommandData::$COMMAND_TYPE_API); 36 | } 37 | 38 | /** 39 | * Execute the command. 40 | * 41 | * @return void 42 | */ 43 | public function handle() 44 | { 45 | parent::handle(); 46 | 47 | if (!$this->commandData->skipMigration) { 48 | $migrationGenerator = new MigrationGenerator($this->commandData); 49 | $migrationGenerator->generate(); 50 | } 51 | 52 | $modelGenerator = new ModelGenerator($this->commandData); 53 | $modelGenerator->generate(); 54 | 55 | $repositoryGenerator = new RepositoryGenerator($this->commandData); 56 | $repositoryGenerator->generate(); 57 | 58 | $repoControllerGenerator = new APIControllerGenerator($this->commandData); 59 | $repoControllerGenerator->generate(); 60 | 61 | $routeGenerator = new RoutesGenerator($this->commandData); 62 | $routeGenerator->generate(); 63 | 64 | if ($this->confirm("\nDo you want to migrate database? [y|N]", false)) { 65 | $this->call('migrate'); 66 | } 67 | } 68 | 69 | /** 70 | * Get the console command arguments. 71 | * 72 | * @return array 73 | */ 74 | protected function getArguments() 75 | { 76 | return array_merge(parent::getArguments(), []); 77 | } 78 | 79 | /** 80 | * Get the console command options. 81 | * 82 | * @return array 83 | */ 84 | public function getOptions() 85 | { 86 | return array_merge(parent::getOptions(), []); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Commands/BaseCommand.php: -------------------------------------------------------------------------------- 1 | commandData->modelName = $this->argument('model'); 26 | $this->commandData->useSoftDelete = $this->option('softDelete'); 27 | $this->commandData->fieldsFile = $this->option('fieldsFile'); 28 | $this->commandData->paginate = $this->option('paginate'); 29 | $this->commandData->tableName = $this->option('tableName'); 30 | $this->commandData->skipMigration = $this->option('skipMigration'); 31 | $this->commandData->fromTable = $this->option('fromTable'); 32 | $this->commandData->rememberToken = $this->option('rememberToken'); 33 | 34 | if ($this->commandData->fromTable) { 35 | if (!$this->commandData->tableName) { 36 | $this->error('tableName required with fromTable option.'); 37 | exit; 38 | } 39 | } 40 | 41 | if ($this->commandData->paginate <= 0) { 42 | $this->commandData->paginate = 10; 43 | } 44 | 45 | $this->commandData->initVariables(); 46 | $this->commandData->addDynamicVariable('$NAMESPACE_APP$', $this->getLaravel()->getNamespace()); 47 | 48 | if ($this->commandData->fieldsFile) { 49 | $fileHelper = new FileHelper(); 50 | try { 51 | if (file_exists($this->commandData->fieldsFile)) { 52 | $filePath = $this->commandData->fieldsFile; 53 | } else { 54 | $filePath = base_path($this->commandData->fieldsFile); 55 | } 56 | 57 | if (!file_exists($filePath)) { 58 | $this->commandData->commandObj->error('Fields file not found'); 59 | exit; 60 | } 61 | 62 | $fileContents = $fileHelper->getFileContents($filePath); 63 | $fields = json_decode($fileContents, true); 64 | 65 | $this->commandData->inputFields = GeneratorUtils::validateFieldsFile($fields); 66 | } catch (Exception $e) { 67 | $this->commandData->commandObj->error($e->getMessage()); 68 | exit; 69 | } 70 | } elseif ($this->commandData->fromTable) { 71 | $tableFieldsGenerator = new TableFieldsGenerator($this->commandData->tableName); 72 | $this->commandData->inputFields = $tableFieldsGenerator->generateFieldsFromTable(); 73 | } else { 74 | $this->commandData->inputFields = $this->commandData->getInputFields(); 75 | } 76 | } 77 | 78 | /** 79 | * Get the console command arguments. 80 | * 81 | * @return array 82 | */ 83 | protected function getArguments() 84 | { 85 | return [ 86 | ['model', InputArgument::REQUIRED, 'Singular Model name'], 87 | ]; 88 | } 89 | 90 | /** 91 | * Get the console command options. 92 | * 93 | * @return array 94 | */ 95 | public function getOptions() 96 | { 97 | return [ 98 | ['softDelete', null, InputOption::VALUE_NONE, 'Use Soft Delete trait'], 99 | ['fieldsFile', null, InputOption::VALUE_REQUIRED, 'Fields input as json file'], 100 | ['paginate', null, InputOption::VALUE_OPTIONAL, 'Pagination for index.blade.php', 10], 101 | ['tableName', null, InputOption::VALUE_REQUIRED, 'Table Name'], 102 | ['skipMigration', null, InputOption::VALUE_NONE, 'Skip Migration generation'], 103 | ['fromTable', null, InputOption::VALUE_NONE, 'Generate from table'], 104 | ['rememberToken', null, InputOption::VALUE_NONE, 'Generate rememberToken field in migration'], 105 | ]; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Commands/PublisherCommand.php: -------------------------------------------------------------------------------- 1 | option('all')) { 38 | $this->publishCommonViews(); 39 | $this->publishAPIRoutes(); 40 | $this->initAPIRoutes(); 41 | $this->publishTemplates(); 42 | $this->publishAppBaseController(); 43 | } elseif ($this->option('templates')) { 44 | $this->publishTemplates(); 45 | } elseif ($this->option('baseController')) { 46 | $this->publishAppBaseController(); 47 | } else { 48 | $this->publishCommonViews(); 49 | $this->publishAPIRoutes(); 50 | $this->initAPIRoutes(); 51 | } 52 | } 53 | 54 | /** 55 | * Get the console command arguments. 56 | * 57 | * @return array 58 | */ 59 | protected function getArguments() 60 | { 61 | return []; 62 | } 63 | 64 | /** 65 | * Get the console command options. 66 | * 67 | * @return array 68 | */ 69 | public function getOptions() 70 | { 71 | return [ 72 | ['templates', null, InputOption::VALUE_NONE, 'Publish templates'], 73 | ['baseController', null, InputOption::VALUE_NONE, 'Publish base controller'], 74 | ['all', null, InputOption::VALUE_NONE, 'Publish all options'], 75 | ]; 76 | } 77 | 78 | /** 79 | * Publishes templates. 80 | */ 81 | public function publishTemplates() 82 | { 83 | $templatesPath = __DIR__.'/../../../../templates'; 84 | 85 | $templatesCopyPath = base_path('resources/api-generator-templates'); 86 | 87 | $this->publishDirectory($templatesPath, $templatesCopyPath, 'templates'); 88 | } 89 | 90 | /** 91 | * Publishes common views. 92 | */ 93 | public function publishCommonViews() 94 | { 95 | $viewsPath = __DIR__.'/../../../../views/common'; 96 | 97 | $viewsCopyPath = base_path('resources/views/common'); 98 | 99 | $this->publishDirectory($viewsPath, $viewsCopyPath, 'common views'); 100 | } 101 | 102 | /** 103 | * Publishes base controller. 104 | */ 105 | private function publishAppBaseController() 106 | { 107 | $templateHelper = new TemplatesHelper(); 108 | $templateData = $templateHelper->getTemplate('AppBaseController', 'controller'); 109 | 110 | $templateData = GeneratorUtils::fillTemplate(CommandData::getConfigDynamicVariables(), $templateData); 111 | 112 | $fileName = 'AppBaseController.php'; 113 | 114 | $filePath = Config::get('generator.path_controller', app_path('Http/Controllers/')); 115 | 116 | $fileHelper = new FileHelper(); 117 | $fileHelper->writeFile($filePath.$fileName, $templateData); 118 | $this->comment('AppBaseController generated'); 119 | $this->info($fileName); 120 | } 121 | 122 | /** 123 | * Publishes api_routes.php. 124 | */ 125 | public function publishAPIRoutes() 126 | { 127 | $routesPath = __DIR__.'/../../../../templates/routes/api_routes.stub'; 128 | 129 | $apiRoutesPath = Config::get('generator.path_api_routes', app_path('Http/api_routes.php')); 130 | 131 | $this->publishFile($routesPath, $apiRoutesPath, 'api_routes.php'); 132 | } 133 | 134 | public function publishFile($sourceFile, $destinationFile, $fileName) 135 | { 136 | if (file_exists($destinationFile)) { 137 | $answer = $this->ask('Do you want to overwrite '.$fileName.'? (y|N) :', false); 138 | 139 | if (strtolower($answer) != 'y' and strtolower($answer) != 'yes') { 140 | return; 141 | } 142 | } 143 | 144 | copy($sourceFile, $destinationFile); 145 | 146 | $this->comment($fileName.' generated'); 147 | $this->info($destinationFile); 148 | } 149 | 150 | public function publishDirectory($sourceDir, $destinationDir, $dirName) 151 | { 152 | if (file_exists($destinationDir)) { 153 | $answer = $this->ask('Do you want to overwrite '.$dirName.'? (y|N) :', false); 154 | 155 | if (strtolower($answer) != 'y' and strtolower($answer) != 'yes') { 156 | return; 157 | } 158 | } else { 159 | File::makeDirectory($destinationDir); 160 | } 161 | 162 | File::copyDirectory($sourceDir, $destinationDir); 163 | 164 | $this->comment($dirName.' published'); 165 | $this->info($destinationDir); 166 | } 167 | 168 | /** 169 | * Initialize routes group based on route integration. 170 | */ 171 | private function initAPIRoutes() 172 | { 173 | $path = Config::get('generator.path_routes', app_path('Http/routes.php')); 174 | 175 | $fileHelper = new FileHelper(); 176 | $routeContents = $fileHelper->getFileContents($path); 177 | 178 | $useDingo = Config::get('generator.use_dingo_api', false); 179 | 180 | if ($useDingo) { 181 | $template = 'dingo_api_routes_group'; 182 | } else { 183 | $template = 'api_routes_group'; 184 | } 185 | 186 | $templateHelper = new TemplatesHelper(); 187 | $templateData = $templateHelper->getTemplate($template, 'routes'); 188 | 189 | $templateData = $this->fillTemplate($templateData); 190 | 191 | $fileHelper->writeFile($path, $routeContents."\n\n".$templateData); 192 | $this->comment("\nAPI group added to routes.php"); 193 | } 194 | 195 | /** 196 | * Replaces dynamic variables of template. 197 | * 198 | * @param string $templateData 199 | * 200 | * @return string 201 | */ 202 | private function fillTemplate($templateData) 203 | { 204 | $apiVersion = Config::get('generator.api_version'); 205 | $apiPrefix = Config::get('generator.api_prefix'); 206 | $apiNamespace = Config::get('generator.namespace_api_controller'); 207 | 208 | $templateData = str_replace('$API_VERSION$', $apiVersion, $templateData); 209 | $templateData = str_replace('$NAMESPACE_API_CONTROLLER$', $apiNamespace, $templateData); 210 | $templateData = str_replace('$API_PREFIX$', $apiPrefix, $templateData); 211 | 212 | return $templateData; 213 | } 214 | } 215 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Commands/ScaffoldAPIGeneratorCommand.php: -------------------------------------------------------------------------------- 1 | commandData = new CommandData($this, CommandData::$COMMAND_TYPE_SCAFFOLD_API); 37 | } 38 | 39 | /** 40 | * Execute the command. 41 | * 42 | * @return void 43 | */ 44 | public function handle() 45 | { 46 | parent::handle(); 47 | 48 | if (!$this->commandData->skipMigration) { 49 | $migrationGenerator = new MigrationGenerator($this->commandData); 50 | $migrationGenerator->generate(); 51 | } 52 | 53 | $modelGenerator = new ModelGenerator($this->commandData); 54 | $modelGenerator->generate(); 55 | 56 | $requestGenerator = new RequestGenerator($this->commandData); 57 | $requestGenerator->generate(); 58 | 59 | $repositoryGenerator = new RepositoryGenerator($this->commandData); 60 | $repositoryGenerator->generate(); 61 | 62 | $repoControllerGenerator = new APIControllerGenerator($this->commandData); 63 | $repoControllerGenerator->generate(); 64 | 65 | $viewsGenerator = new ViewGenerator($this->commandData); 66 | $viewsGenerator->generate(); 67 | 68 | $repoControllerGenerator = new ViewControllerGenerator($this->commandData); 69 | $repoControllerGenerator->generate(); 70 | 71 | $routeGenerator = new RoutesGenerator($this->commandData); 72 | $routeGenerator->generate(); 73 | 74 | if ($this->confirm("\nDo you want to migrate database? [y|N]", false)) { 75 | $this->call('migrate'); 76 | } 77 | } 78 | 79 | /** 80 | * Get the console command arguments. 81 | * 82 | * @return array 83 | */ 84 | protected function getArguments() 85 | { 86 | return array_merge(parent::getArguments(), []); 87 | } 88 | 89 | /** 90 | * Get the console command options. 91 | * 92 | * @return array 93 | */ 94 | public function getOptions() 95 | { 96 | return array_merge(parent::getOptions(), []); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Commands/ScaffoldGeneratorCommand.php: -------------------------------------------------------------------------------- 1 | commandData = new CommandData($this, CommandData::$COMMAND_TYPE_SCAFFOLD); 38 | } 39 | 40 | /** 41 | * Execute the command. 42 | * 43 | * @return void 44 | */ 45 | public function handle() 46 | { 47 | parent::handle(); 48 | 49 | if (!$this->commandData->skipMigration and !$this->commandData->fromTable) { 50 | $migrationGenerator = new MigrationGenerator($this->commandData); 51 | $migrationGenerator->generate(); 52 | } 53 | 54 | $modelGenerator = new ModelGenerator($this->commandData); 55 | $modelGenerator->generate(); 56 | 57 | $requestGenerator = new RequestGenerator($this->commandData); 58 | $requestGenerator->generate(); 59 | 60 | $repositoryGenerator = new RepositoryGenerator($this->commandData); 61 | $repositoryGenerator->generate(); 62 | 63 | $repoControllerGenerator = new ViewControllerGenerator($this->commandData); 64 | $repoControllerGenerator->generate(); 65 | 66 | $viewsGenerator = new ViewGenerator($this->commandData); 67 | $viewsGenerator->generate(); 68 | 69 | $routeGenerator = new RoutesGenerator($this->commandData); 70 | $routeGenerator->generate(); 71 | 72 | if ($this->confirm("\nDo you want to migrate database? [y|N]", false)) { 73 | $this->call('migrate'); 74 | } 75 | } 76 | 77 | /** 78 | * Get the console command arguments. 79 | * 80 | * @return array 81 | */ 82 | protected function getArguments() 83 | { 84 | return array_merge(parent::getArguments()); 85 | } 86 | 87 | /** 88 | * Get the console command options. 89 | * 90 | * @return array 91 | */ 92 | public function getOptions() 93 | { 94 | return array_merge(parent::getOptions(), []); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Errors.php: -------------------------------------------------------------------------------- 1 | [ 20 | 'message' => 'Somewhere something happened and someone already working to fix that!', 21 | 'system_message' => 'Unknown error', 22 | 'error_code' => self::UNKNOWN_ERROR, 23 | 'exception' => 'ServiceUnavailableHttpException', 24 | ], 25 | 26 | self::VALIDATION_ERROR => [ 27 | 'message' => 'Oops, some fields looks like incorrect!', 28 | 'system_message' => 'Validation error', 29 | 'error_code' => self::VALIDATION_ERROR, 30 | 'exception' => 'BadRequestHttpException', 31 | ], 32 | 33 | self::NOT_FOUND => [ 34 | 'message' => 'Sorry, we could not find requested resource', 35 | 'system_message' => 'Record not found', 36 | 'error_code' => self::NOT_FOUND, 37 | 'exception' => 'NotFoundHttpException', 38 | ], 39 | 40 | self::CREATION_FORM_NOT_EXISTS => [ 41 | 'message' => 'Sorry, we have nothing on this address', 42 | 'system_message' => 'Form of creating is not supported in API', 43 | 'error_code' => self::CREATION_FORM_NOT_EXISTS, 44 | 'exception' => 'NotFoundHttpException', 45 | ], 46 | 47 | self::EDITION_FORM_NOT_EXISTS => [ 48 | 'message' => 'Sorry, we have nothing on this address', 49 | 'system_message' => 'Form of editing is not supported in API', 50 | 'error_code' => self::EDITION_FORM_NOT_EXISTS, 51 | 'exception' => 'NotFoundHttpException', 52 | ], 53 | ]; 54 | 55 | /** 56 | * Get (with help links) errors with some codes / get unknown error or all errors. 57 | * 58 | * @param array $codes filter errors by single code or array of codes 59 | * @param array $payload error description [error code => useful info] 60 | * @param bool $get_all return all errors, if code not specified 61 | * 62 | * @return array 63 | */ 64 | public static function getErrors($codes = [], array $payload = [], $get_all = false) 65 | { 66 | $codes = (array) $codes; 67 | if (empty($codes) && $get_all) { 68 | $codes = array_keys(static::$errors); 69 | } 70 | $return = []; 71 | foreach ($codes as $code) { 72 | if (isset(static::$errors[$code])) { 73 | $return[$code] = static::$errors[$code]; 74 | unset($return[$code]['exception']); 75 | $return[$code]['help'] = '/errors/'.$code; 76 | $return[$code]['payload'] = isset($payload[$code]) 77 | ? $payload[$code] 78 | : []; 79 | } 80 | } 81 | if (empty($return)) { 82 | if (!empty($codes)) { 83 | $payload = [self::UNKNOWN_ERROR => 'Unknown error codes: '.implode(',', $codes)]; 84 | } 85 | $return = static::getErrors([self::UNKNOWN_ERROR], $payload); 86 | } 87 | 88 | return $return; 89 | } 90 | 91 | /** 92 | * throw HttpException error, that is not registered in Errors::$errors array and mix data inside. 93 | * 94 | * @param Exception\HttpException $exception 95 | * @param $help_link 96 | * @param string $system_message 97 | * @param array $payload 98 | * @param array $hateoas Send here static::getHATEOAS(['%id' => $id, '%placeholder' => 'value']) from generated controller 99 | * @param array $replacements additional info inside response 100 | */ 101 | public static function throwHttpException(Exception\HttpException $exception, $help_link, $system_message = '', $payload = [], $hateoas = [], $replacements = []) 102 | { 103 | $replacements = self::getReplacements($system_message, $payload, $help_link, $replacements); 104 | app('api.exception')->setReplacements($replacements); 105 | throw $exception; 106 | } 107 | 108 | /** 109 | * Throw HttpException with specified or unknown error. 110 | * 111 | * @param mixed $error_code send exception for error with code 112 | * @param [] $payload error description [error code => useful info] 113 | * @param array $hateoas Send here static::getHATEOAS(['%id' => $id, '%placeholder' => 'value']) from generated controller 114 | * @param array $replacements additional info inside response 115 | */ 116 | public static function throwHttpExceptionWithCode($error_code = null, $payload = [], $hateoas = [], $replacements = []) 117 | { 118 | $errors = static::getErrors([$error_code], [$error_code => $payload]); 119 | $error = current($errors); 120 | $replacements = self::getReplacements($error['system_message'], $error['payload'], $error['help'], $hateoas, $replacements); 121 | app('api.exception')->setReplacements($replacements); 122 | $exception = self::getExceptionObject(static::$errors[$error['error_code']]['exception'], $error); 123 | throw $exception; 124 | } 125 | 126 | /** 127 | * @param string $exception_class 128 | * @param array $error item from self::$errors 129 | * 130 | * @return mixed 131 | * 132 | * @internal param $exception 133 | */ 134 | protected static function getExceptionObject($exception_class, $error) 135 | { 136 | $exception = static::$exception_namespace.$exception_class; 137 | switch ($exception_class) { 138 | case 'MethodNotAllowedHttpException': 139 | return new $exception([], $error['message'], null, [], $error['error_code']); 140 | case 'HttpException': 141 | return new $exception(400, $error['message'], null, [], $error['error_code']); 142 | case 'ServiceUnavailableHttpException': 143 | case 'TooManyRequestsHttpException': 144 | return new $exception(3600, $error['message'], null, $error['error_code']); 145 | case 'UnauthorizedHttpException': 146 | return new $exception('', $error['message'], null, $error['error_code']); 147 | default: 148 | return new $exception($error['message'], null, $error['error_code']); 149 | } 150 | } 151 | 152 | /** 153 | * Get array of replacements for mixing to Exception. 154 | * 155 | * 156 | * setReplacements(Errors::getReplacements('Not found', ['id' => 1111], 'help/article_not_found')); 158 | * throw new HttpException(404, 'Sorry, article is not found'); 159 | * ?> 160 | * 161 | * 162 | * @param $system_message 163 | * @param $payload 164 | * @param $help_link 165 | * @param array $hateoas Send here static::getHATEOAS(['%id' => $id, '%placeholder' => 'value']) from generated controller 166 | * @param array $replacements 167 | * 168 | * @return array 169 | */ 170 | public static function getReplacements($system_message = '', $payload = [], $help_link = '', $hateoas = [], $replacements = []) 171 | { 172 | if ($system_message) { 173 | $replacements[':system_message'] = $system_message; 174 | } 175 | if (!empty($payload)) { 176 | $replacements[':payload'] = $payload; 177 | } 178 | if ($help_link) { 179 | $replacements[':help'] = $help_link; 180 | } 181 | if (!empty($hateoas)) { 182 | $replacements[':links'] = $hateoas; 183 | } 184 | 185 | return $replacements; 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src/Mitul/Generator/File/FileHelper.php: -------------------------------------------------------------------------------- 1 | 'form-control-label $FIELD_NAME_TITLE$']) !!}"; 14 | $template = str_replace('$FIELD_NAME_TITLE$', $label, $template); 15 | $template = str_replace('$FIELD_NAME$', $field['fieldName'], $template); 16 | return $template; 17 | } 18 | 19 | private static function replaceFieldVars($textField, $field) 20 | { 21 | $label = Str::title(str_replace('_', ' ', $field['fieldName'])); 22 | 23 | $textField = str_replace('$FIELD_NAME$', $field['fieldName'], $textField); 24 | $textField = str_replace('$FIELD_NAME_TITLE$', $label, $textField); 25 | $textField = str_replace('$FIELD_INPUT$', $textField, $textField); 26 | 27 | return $textField; 28 | } 29 | 30 | public static function text($templateData, $field) 31 | { 32 | $textField = self::generateLabel($field); 33 | 34 | $textField .= "\n\t{!! Form::text('\$FIELD_NAME\$', null, ['class' => 'form-control']) !!}"; 35 | 36 | $templateData = str_replace('$FIELD_INPUT$', $textField, $templateData); 37 | 38 | $templateData = self::replaceFieldVars($templateData, $field); 39 | 40 | return $templateData; 41 | } 42 | 43 | public static function textarea($templateData, $field) 44 | { 45 | $textareaField = self::generateLabel($field); 46 | 47 | $textareaField .= "\n\t{!! Form::textarea('\$FIELD_NAME\$', null, ['class' => 'form-control']) !!}"; 48 | 49 | $templateData = str_replace('$FIELD_INPUT$', $textareaField, $templateData); 50 | 51 | $templateData = self::replaceFieldVars($templateData, $field); 52 | 53 | return $templateData; 54 | } 55 | 56 | public static function password($templateData, $field) 57 | { 58 | $textField = self::generateLabel($field); 59 | 60 | $textField .= "\n\t{!! Form::password('\$FIELD_NAME\$', ['class' => 'form-control']) !!}"; 61 | 62 | $templateData = str_replace('$FIELD_INPUT$', $textField, $templateData); 63 | 64 | $templateData = self::replaceFieldVars($templateData, $field); 65 | 66 | return $templateData; 67 | } 68 | 69 | public static function email($templateData, $field) 70 | { 71 | $textField = self::generateLabel($field); 72 | 73 | $textField .= "\n\t{!! Form::email('\$FIELD_NAME\$', null, ['class' => 'form-control']) !!}"; 74 | $templateData = str_replace('$FIELD_INPUT$', $textField, $templateData); 75 | 76 | $templateData = self::replaceFieldVars($templateData, $field); 77 | 78 | return $templateData; 79 | } 80 | 81 | public static function file($templateData, $field) 82 | { 83 | $textField = self::generateLabel($field); 84 | 85 | $textField .= "\n\t{!! Form::file('\$FIELD_NAME\$') !!}"; 86 | 87 | $templateData = str_replace('$FIELD_INPUT$', $textField, $templateData); 88 | 89 | $templateData = self::replaceFieldVars($templateData, $field); 90 | 91 | return $templateData; 92 | } 93 | 94 | public static function checkbox($templateData, $field) 95 | { 96 | $textField = "
\n"; 97 | $textField .= "\t\t'; 103 | $textField .= "\n\t
"; 104 | 105 | $templateData = str_replace('$FIELD_INPUT$', $textField, $templateData); 106 | 107 | $templateData = self::replaceFieldVars($templateData, $field); 108 | 109 | return $templateData; 110 | } 111 | 112 | public static function radio($templateData, $field) 113 | { 114 | $textField = self::generateLabel($field); 115 | 116 | if (count($field['typeOptions']) > 0) { 117 | $arr = explode(',', $field['typeOptions']); 118 | 119 | foreach ($arr as $item) { 120 | $label = Str::title(str_replace('_', ' ', $item)); 121 | 122 | $textField .= "\n\t
"; 123 | $textField .= "\n\t\t"; 128 | $textField .= "\n\t
"; 129 | } 130 | } 131 | 132 | $templateData = str_replace('$FIELD_INPUT$', $textField, $templateData); 133 | 134 | $templateData = self::replaceFieldVars($templateData, $field); 135 | 136 | return $templateData; 137 | } 138 | 139 | public static function number($templateData, $field) 140 | { 141 | $textField = self::generateLabel($field); 142 | 143 | $textField .= "\n\t{!! Form::number('\$FIELD_NAME\$', null, ['class' => 'form-control']) !!}"; 144 | $templateData = str_replace('$FIELD_INPUT$', $textField, $templateData); 145 | 146 | $templateData = self::replaceFieldVars($templateData, $field); 147 | 148 | return $templateData; 149 | } 150 | 151 | public static function date($templateData, $field) 152 | { 153 | $textField = self::generateLabel($field); 154 | 155 | $textField .= "\n\t{!! Form::date('\$FIELD_NAME\$', null, ['class' => 'form-control']) !!}"; 156 | $templateData = str_replace('$FIELD_INPUT$', $textField, $templateData); 157 | 158 | $templateData = self::replaceFieldVars($templateData, $field); 159 | 160 | return $templateData; 161 | } 162 | 163 | public static function select($templateData, $field) 164 | { 165 | $textField = self::generateLabel($field); 166 | 167 | $textField .= "\n\t{!! Form::select('\$FIELD_NAME\$', \$INPUT_ARR\$, null, ['class' => 'form-control']) !!}"; 168 | $textField = str_replace('$FIELD_NAME$', $field['fieldName'], $textField); 169 | 170 | if (count($field['typeOptions']) > 0) { 171 | $arr = explode(',', $field['typeOptions']); 172 | $inputArr = '['; 173 | foreach ($arr as $item) { 174 | $inputArr .= " '$item' => '$item',"; 175 | } 176 | 177 | $inputArr = substr($inputArr, 0, strlen($inputArr) - 1); 178 | 179 | $inputArr .= ' ]'; 180 | 181 | $textField = str_replace('$INPUT_ARR$', $inputArr, $textField); 182 | } else { 183 | $textField = str_replace('$INPUT_ARR$', '[]', $textField); 184 | } 185 | 186 | $templateData = str_replace('$FIELD_INPUT$', $textField, $templateData); 187 | 188 | $templateData = self::replaceFieldVars($templateData, $field); 189 | 190 | return $templateData; 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /src/Mitul/Generator/GeneratorServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 23 | $configPath => config_path('generator.php'), 24 | ]); 25 | } 26 | 27 | /** 28 | * Register the application services. 29 | * 30 | * @return void 31 | */ 32 | public function register() 33 | { 34 | $this->app->singleton('mitul.generator.publish', function ($app) { 35 | return new PublisherCommand(); 36 | }); 37 | 38 | $this->app->singleton('mitul.generator.api', function ($app) { 39 | return new APIGeneratorCommand(); 40 | }); 41 | 42 | $this->app->singleton('mitul.generator.scaffold', function ($app) { 43 | return new ScaffoldGeneratorCommand(); 44 | }); 45 | 46 | $this->app->singleton('mitul.generator.scaffold_api', function ($app) { 47 | return new ScaffoldAPIGeneratorCommand(); 48 | }); 49 | 50 | $this->commands([ 51 | 'mitul.generator.publish', 52 | 'mitul.generator.api', 53 | 'mitul.generator.scaffold', 54 | 'mitul.generator.scaffold_api', 55 | ]); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Generators/API/APIControllerGenerator.php: -------------------------------------------------------------------------------- 1 | commandData = $commandData; 21 | $this->path = Config::get('generator.path_api_controller', app_path('Http/Controllers/API/')); 22 | } 23 | 24 | public function generate() 25 | { 26 | $templateData = $this->commandData->templatesHelper->getTemplate('Controller', 'api'); 27 | 28 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 29 | 30 | $fileName = $this->commandData->modelName.'APIController.php'; 31 | 32 | if (!file_exists($this->path)) { 33 | mkdir($this->path, 0755, true); 34 | } 35 | 36 | $path = $this->path.$fileName; 37 | 38 | $this->commandData->fileHelper->writeFile($path, $templateData); 39 | $this->commandData->commandObj->comment("\nAPI Controller created: "); 40 | $this->commandData->commandObj->info($fileName); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Generators/Common/MigrationGenerator.php: -------------------------------------------------------------------------------- 1 | commandData = $commandData; 22 | $this->path = Config::get('generator.path_migration', base_path('database/migrations/')); 23 | } 24 | 25 | public function generate() 26 | { 27 | $templateData = $this->commandData->templatesHelper->getTemplate('Migration', 'common'); 28 | 29 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 30 | 31 | $templateData = str_replace('$FIELDS$', $this->generateFieldsStr(), $templateData); 32 | 33 | $fileName = date('Y_m_d_His').'_'.'create_'.$this->commandData->modelNamePluralCamel.'_table.php'; 34 | 35 | $path = $this->path.$fileName; 36 | 37 | $this->commandData->fileHelper->writeFile($path, $templateData); 38 | $this->commandData->commandObj->comment("\nMigration created: "); 39 | $this->commandData->commandObj->info($fileName); 40 | } 41 | 42 | private function generateFieldsStr() 43 | { 44 | $fieldsStr = "\$table->increments('id');\n"; 45 | 46 | foreach ($this->commandData->inputFields as $field) { 47 | $fieldsStr .= SchemaGenerator::createField($field['fieldInput']); 48 | } 49 | 50 | if ($this->commandData->rememberToken) { 51 | $fieldsStr .= "\t\t\t\$table->rememberToken();\n"; 52 | } 53 | 54 | $fieldsStr .= "\t\t\t\$table->timestamps();"; 55 | 56 | if ($this->commandData->useSoftDelete) { 57 | $fieldsStr .= "\n\t\t\t\$table->softDeletes();"; 58 | } 59 | 60 | return $fieldsStr; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Generators/Common/ModelGenerator.php: -------------------------------------------------------------------------------- 1 | commandData = $commandData; 21 | $this->path = Config::get('generator.path_model', app_path('Models/')); 22 | } 23 | 24 | public function generate() 25 | { 26 | $templateName = 'Model'; 27 | 28 | $templateData = $this->commandData->templatesHelper->getTemplate($templateName, 'common'); 29 | 30 | $templateData = $this->fillTemplate($templateData); 31 | 32 | $fileName = $this->commandData->modelName.'.php'; 33 | 34 | if (!file_exists($this->path)) { 35 | mkdir($this->path, 0755, true); 36 | } 37 | 38 | $path = $this->path.$fileName; 39 | 40 | $this->commandData->fileHelper->writeFile($path, $templateData); 41 | $this->commandData->commandObj->comment("\nModel created: "); 42 | $this->commandData->commandObj->info($fileName); 43 | } 44 | 45 | private function fillTemplate($templateData) 46 | { 47 | if (!$this->commandData->useSoftDelete) { 48 | $templateData = str_replace('$SOFT_DELETE_IMPORT$', '', $templateData); 49 | $templateData = str_replace('$SOFT_DELETE$', '', $templateData); 50 | $templateData = str_replace('$SOFT_DELETE_DATES$', '', $templateData); 51 | } 52 | 53 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 54 | 55 | $fillables = []; 56 | 57 | foreach ($this->commandData->inputFields as $field) { 58 | $fillables[] = '"'.$field['fieldName'].'"'; 59 | } 60 | 61 | $templateData = str_replace('$FIELDS$', implode(",\n\t\t", $fillables), $templateData); 62 | 63 | $templateData = str_replace('$RULES$', implode(",\n\t\t", $this->generateRules()), $templateData); 64 | 65 | $templateData = str_replace('$CAST$', implode(",\n\t\t", $this->generateCasts()), $templateData); 66 | 67 | return $templateData; 68 | } 69 | 70 | private function generateRules() 71 | { 72 | $rules = []; 73 | 74 | foreach ($this->commandData->inputFields as $field) { 75 | if (!empty($field['validations'])) { 76 | $rule = '"'.$field['fieldName'].'" => "'.$field['validations'].'"'; 77 | $rules[] = $rule; 78 | } 79 | } 80 | 81 | return $rules; 82 | } 83 | 84 | public function generateCasts() 85 | { 86 | $casts = []; 87 | 88 | foreach ($this->commandData->inputFields as $field) { 89 | switch ($field['fieldType']) { 90 | case 'integer': 91 | $rule = '"'.$field['fieldName'].'" => "integer"'; 92 | break; 93 | case 'double': 94 | $rule = '"'.$field['fieldName'].'" => "double"'; 95 | break; 96 | case 'float': 97 | $rule = '"'.$field['fieldName'].'" => "float"'; 98 | break; 99 | case 'boolean': 100 | $rule = '"'.$field['fieldName'].'" => "boolean"'; 101 | break; 102 | case 'string': 103 | case 'char': 104 | case 'text': 105 | $rule = '"'.$field['fieldName'].'" => "string"'; 106 | break; 107 | default: 108 | $rule = ''; 109 | break; 110 | } 111 | 112 | if (!empty($rule)) { 113 | $casts[] = $rule; 114 | } 115 | } 116 | 117 | return $casts; 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Generators/Common/RepositoryGenerator.php: -------------------------------------------------------------------------------- 1 | commandData = $commandData; 21 | $this->path = Config::get('generator.path_repository', app_path('/Libraries/Repositories/')); 22 | } 23 | 24 | public function generate() 25 | { 26 | $templateData = $this->commandData->templatesHelper->getTemplate('Repository', 'common'); 27 | 28 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 29 | 30 | $fileName = $this->commandData->modelName.'Repository.php'; 31 | 32 | if (!file_exists($this->path)) { 33 | mkdir($this->path, 0755, true); 34 | } 35 | 36 | $path = $this->path.$fileName; 37 | 38 | $this->commandData->fileHelper->writeFile($path, $templateData); 39 | $this->commandData->commandObj->comment("\nRepository created: "); 40 | $this->commandData->commandObj->info($fileName); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Generators/Common/RequestGenerator.php: -------------------------------------------------------------------------------- 1 | commandData = $commandData; 21 | $this->path = Config::get('generator.path_request', app_path('Http/Requests/')); 22 | } 23 | 24 | public function generate() 25 | { 26 | $this->generateCreateRequest(); 27 | $this->generateUpdateRequest(); 28 | } 29 | 30 | private function generateCreateRequest() 31 | { 32 | $templateData = $this->commandData->templatesHelper->getTemplate('CreateRequest', 'scaffold/requests'); 33 | 34 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 35 | 36 | $fileName = 'Create'.$this->commandData->modelName.'Request.php'; 37 | 38 | $path = $this->path.$fileName; 39 | 40 | $this->commandData->fileHelper->writeFile($path, $templateData); 41 | $this->commandData->commandObj->comment("\nCreate Request created: "); 42 | $this->commandData->commandObj->info($fileName); 43 | } 44 | 45 | private function generateUpdateRequest() 46 | { 47 | $templateData = $this->commandData->templatesHelper->getTemplate('UpdateRequest', 'scaffold/requests'); 48 | 49 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 50 | 51 | $fileName = 'Update'.$this->commandData->modelName.'Request.php'; 52 | 53 | $path = $this->path.$fileName; 54 | 55 | $this->commandData->fileHelper->writeFile($path, $templateData); 56 | $this->commandData->commandObj->comment("\nUpdate Request created: "); 57 | $this->commandData->commandObj->info($fileName); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Generators/Common/RoutesGenerator.php: -------------------------------------------------------------------------------- 1 | commandData = $commandData; 27 | $this->path = Config::get('generator.path_routes', app_path('Http/routes.php')); 28 | $this->apiPath = Config::get('generator.path_api_routes', app_path('Http/api_routes.php')); 29 | $this->useDingo = Config::get('generator.use_dingo_api', false); 30 | } 31 | 32 | public function generate() 33 | { 34 | if ($this->commandData->commandType == CommandData::$COMMAND_TYPE_API) { 35 | $this->generateAPIRoutes(); 36 | } elseif ($this->commandData->commandType == CommandData::$COMMAND_TYPE_SCAFFOLD) { 37 | $this->generateScaffoldRoutes(); 38 | } elseif ($this->commandData->commandType == CommandData::$COMMAND_TYPE_SCAFFOLD_API) { 39 | $this->generateAPIRoutes(); 40 | $this->generateScaffoldRoutes(); 41 | } 42 | } 43 | 44 | private function generateAPIRoutes() 45 | { 46 | $routeContents = $this->commandData->fileHelper->getFileContents($this->apiPath); 47 | 48 | if ($this->useDingo) { 49 | $routeContents .= "\n\n".'$api->resource("'.$this->commandData->modelNamePluralCamel.'", "'.$this->commandData->modelName.'APIController");'; 50 | } else { 51 | $routeContents .= "\n\n".'Route::resource("'.$this->commandData->modelNamePluralCamel.'", "'.$this->commandData->modelName.'APIController");'; 52 | } 53 | 54 | $this->commandData->fileHelper->writeFile($this->apiPath, $routeContents); 55 | $this->commandData->commandObj->comment("\napi_routes.php modified:"); 56 | $this->commandData->commandObj->info('"'.$this->commandData->modelNamePluralCamel.'" route added.'); 57 | } 58 | 59 | private function generateScaffoldRoutes() 60 | { 61 | $routeContents = $this->commandData->fileHelper->getFileContents($this->path); 62 | 63 | $templateData = $this->commandData->templatesHelper->getTemplate('scaffold_routes', 'routes'); 64 | 65 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 66 | 67 | $routeContents .= "\n\n".$templateData; 68 | 69 | $this->commandData->fileHelper->writeFile($this->path, $routeContents); 70 | $this->commandData->commandObj->comment("\nroutes.php modified:"); 71 | $this->commandData->commandObj->info('"'.$this->commandData->modelNamePluralCamel.'" route added.'); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Generators/GeneratorProvider.php: -------------------------------------------------------------------------------- 1 | commandData = $commandData; 21 | $this->path = Config::get('generator.path_controller', app_path('Http/Controllers/')); 22 | } 23 | 24 | public function generate() 25 | { 26 | $templateData = $this->commandData->templatesHelper->getTemplate('Controller', 'scaffold'); 27 | 28 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 29 | 30 | if ($this->commandData->paginate) { 31 | $templateData = str_replace('$RENDER_TYPE$', 'paginate('.$this->commandData->paginate.')', $templateData); 32 | } else { 33 | $templateData = str_replace('$RENDER_TYPE$', 'all()', $templateData); 34 | } 35 | 36 | $fileName = $this->commandData->modelName.'Controller.php'; 37 | 38 | $path = $this->path.$fileName; 39 | 40 | $this->commandData->fileHelper->writeFile($path, $templateData); 41 | $this->commandData->commandObj->comment("\nController created: "); 42 | $this->commandData->commandObj->info($fileName); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Generators/Scaffold/ViewGenerator.php: -------------------------------------------------------------------------------- 1 | commandData = $commandData; 26 | $this->path = Config::get('generator.path_views', base_path('resources/views')).'/'.$this->commandData->modelNamePluralCamel.'/'; 27 | $this->viewsPath = 'scaffold/views'; 28 | } 29 | 30 | public function generate() 31 | { 32 | if (!file_exists($this->path)) { 33 | mkdir($this->path, 0755, true); 34 | } 35 | 36 | $this->commandData->commandObj->comment("\nViews created: "); 37 | $this->generateFields(); 38 | $this->generateShowFields(); 39 | $this->generateTable(); 40 | $this->generateIndex(); 41 | $this->generateShow(); 42 | $this->generateCreate(); 43 | $this->generateEdit(); 44 | } 45 | 46 | private function generateFields() 47 | { 48 | $fieldTemplate = $this->commandData->templatesHelper->getTemplate('field.blade', $this->viewsPath); 49 | 50 | $fieldsStr = ''; 51 | 52 | foreach ($this->commandData->inputFields as $field) { 53 | switch ($field['type']) { 54 | case 'text': 55 | $fieldsStr .= FormFieldsGenerator::text($fieldTemplate, $field)."\n\n"; 56 | break; 57 | case 'textarea': 58 | $fieldsStr .= FormFieldsGenerator::textarea($fieldTemplate, $field)."\n\n"; 59 | break; 60 | case 'password': 61 | $fieldsStr .= FormFieldsGenerator::password($fieldTemplate, $field)."\n\n"; 62 | break; 63 | case 'email': 64 | $fieldsStr .= FormFieldsGenerator::email($fieldTemplate, $field)."\n\n"; 65 | break; 66 | case 'file': 67 | $fieldsStr .= FormFieldsGenerator::file($fieldTemplate, $field)."\n\n"; 68 | break; 69 | case 'checkbox': 70 | $fieldsStr .= FormFieldsGenerator::checkbox($fieldTemplate, $field)."\n\n"; 71 | break; 72 | case 'radio': 73 | $fieldsStr .= FormFieldsGenerator::radio($fieldTemplate, $field)."\n\n"; 74 | break; 75 | case 'number': 76 | $fieldsStr .= FormFieldsGenerator::number($fieldTemplate, $field)."\n\n"; 77 | break; 78 | case 'date': 79 | $fieldsStr .= FormFieldsGenerator::date($fieldTemplate, $field)."\n\n"; 80 | break; 81 | case 'select': 82 | $fieldsStr .= FormFieldsGenerator::select($fieldTemplate, $field)."\n\n"; 83 | break; 84 | } 85 | } 86 | 87 | $templateData = $this->commandData->templatesHelper->getTemplate('fields.blade', $this->viewsPath); 88 | 89 | $templateData = str_replace('$FIELDS$', $fieldsStr, $templateData); 90 | 91 | $fileName = 'fields.blade.php'; 92 | 93 | $path = $this->path.$fileName; 94 | 95 | $this->commandData->fileHelper->writeFile($path, $templateData); 96 | $this->commandData->commandObj->info('field.blade.php created'); 97 | } 98 | 99 | private function generateShowFields() 100 | { 101 | $fieldTemplate = $this->commandData->templatesHelper->getTemplate('show_field.blade', $this->viewsPath); 102 | 103 | $fieldsStr = ''; 104 | 105 | foreach ($this->commandData->inputFields as $field) { 106 | $singleFieldStr = str_replace('$FIELD_NAME_TITLE$', Str::title(str_replace('_', ' ', $field['fieldName'])), $fieldTemplate); 107 | $singleFieldStr = str_replace('$FIELD_NAME$', $field['fieldName'], $singleFieldStr); 108 | $singleFieldStr = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $singleFieldStr); 109 | 110 | $fieldsStr .= $singleFieldStr."\n\n"; 111 | } 112 | 113 | $fileName = 'show_fields.blade.php'; 114 | 115 | $path = $this->path.$fileName; 116 | 117 | $this->commandData->fileHelper->writeFile($path, $fieldsStr); 118 | $this->commandData->commandObj->info('show-field.blade.php created'); 119 | } 120 | 121 | private function generateIndex() 122 | { 123 | $templateData = $this->commandData->templatesHelper->getTemplate('index.blade', $this->viewsPath); 124 | 125 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 126 | 127 | if ($this->commandData->paginate) { 128 | $paginateTemplate = $this->commandData->templatesHelper->getTemplate('paginate.blade', 'scaffold/views'); 129 | 130 | $paginateTemplate = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $paginateTemplate); 131 | 132 | $templateData = str_replace('$PAGINATE$', $paginateTemplate, $templateData); 133 | } else { 134 | $templateData = str_replace('$PAGINATE$', '', $templateData); 135 | } 136 | 137 | $fileName = 'index.blade.php'; 138 | 139 | $path = $this->path.$fileName; 140 | 141 | $this->commandData->fileHelper->writeFile($path, $templateData); 142 | $this->commandData->commandObj->info('index.blade.php created'); 143 | } 144 | 145 | private function generateTable() 146 | { 147 | $templateData = $this->commandData->templatesHelper->getTemplate('table.blade', $this->viewsPath); 148 | 149 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 150 | 151 | $fileName = 'table.blade.php'; 152 | 153 | $headerFields = ''; 154 | 155 | foreach ($this->commandData->inputFields as $field) { 156 | $headerFields .= ''.Str::title(str_replace('_', ' ', $field['fieldName']))."\n\t\t\t"; 157 | } 158 | 159 | $headerFields = trim($headerFields); 160 | 161 | $templateData = str_replace('$FIELD_HEADERS$', $headerFields, $templateData); 162 | 163 | $tableBodyFields = ''; 164 | 165 | foreach ($this->commandData->inputFields as $field) { 166 | $tableBodyFields .= '{!! $'.$this->commandData->modelNameCamel.'->'.$field['fieldName']." !!}\n\t\t\t"; 167 | } 168 | 169 | $tableBodyFields = trim($tableBodyFields); 170 | 171 | $templateData = str_replace('$FIELD_BODY$', $tableBodyFields, $templateData); 172 | 173 | $path = $this->path.$fileName; 174 | 175 | $this->commandData->fileHelper->writeFile($path, $templateData); 176 | $this->commandData->commandObj->info('table.blade.php created'); 177 | } 178 | 179 | private function generateShow() 180 | { 181 | $templateData = $this->commandData->templatesHelper->getTemplate('show.blade', $this->viewsPath); 182 | 183 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 184 | 185 | $fileName = 'show.blade.php'; 186 | 187 | $path = $this->path.$fileName; 188 | 189 | $this->commandData->fileHelper->writeFile($path, $templateData); 190 | $this->commandData->commandObj->info('show.blade.php created'); 191 | } 192 | 193 | private function generateCreate() 194 | { 195 | $templateData = $this->commandData->templatesHelper->getTemplate('create.blade', $this->viewsPath); 196 | 197 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 198 | 199 | $fileName = 'create.blade.php'; 200 | 201 | $path = $this->path.$fileName; 202 | 203 | $this->commandData->fileHelper->writeFile($path, $templateData); 204 | $this->commandData->commandObj->info('create.blade.php created'); 205 | } 206 | 207 | private function generateEdit() 208 | { 209 | $templateData = $this->commandData->templatesHelper->getTemplate('edit.blade', $this->viewsPath); 210 | 211 | $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); 212 | 213 | $fileName = 'edit.blade.php'; 214 | 215 | $path = $this->path.$fileName; 216 | 217 | $this->commandData->fileHelper->writeFile($path, $templateData); 218 | $this->commandData->commandObj->info('edit.blade.php created'); 219 | } 220 | } 221 | -------------------------------------------------------------------------------- /src/Mitul/Generator/SchemaGenerator.php: -------------------------------------------------------------------------------- 1 | ".$fieldType."('".$fieldName."'"; 20 | 21 | if (count($fieldTypeInputs) > 0) { 22 | foreach ($fieldTypeInputs as $param) { 23 | $fieldStr .= ', '.$param; 24 | } 25 | } 26 | 27 | $fieldStr .= ')'; 28 | 29 | if (count($fieldInputs) > 0) { 30 | foreach ($fieldInputs as $input) { 31 | $input = explode(',', $input); 32 | 33 | $option = array_shift($input); 34 | 35 | $fieldStr .= '->'.$option.'('; 36 | 37 | if (count($input) > 0) { 38 | foreach ($input as $param) { 39 | $fieldStr .= "'".$param."', "; 40 | } 41 | 42 | $fieldStr = substr($fieldStr, 0, strlen($fieldStr) - 2); 43 | } 44 | 45 | $fieldStr .= ')'; 46 | } 47 | } 48 | 49 | if (!empty($fieldStr)) { 50 | $fieldStr .= ";\n"; 51 | } 52 | 53 | return $fieldStr; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/Mitul/Generator/TemplatesHelper.php: -------------------------------------------------------------------------------- 1 | 1) { 28 | for ($i = 1; $i < count($fieldTypeOptions); $i++) { 29 | $fieldTypeParams[] = $fieldTypeOptions[$i]; 30 | } 31 | } 32 | 33 | $fieldOptions = []; 34 | if (count($fieldInputs) > 2) { 35 | $fieldOptions[] = $fieldInputs[2]; 36 | } 37 | 38 | $typeOptions = explode(':', $type); 39 | $type = $typeOptions[0]; 40 | if (count($typeOptions) > 1) { 41 | $typeOptions = $typeOptions[1]; 42 | } else { 43 | $typeOptions = []; 44 | } 45 | 46 | return [ 47 | 'fieldName' => $fieldName, 48 | 'type' => $type, 49 | 'typeOptions' => $typeOptions, 50 | 'fieldInput' => $fieldInputStr, 51 | 'fieldType' => $fieldType, 52 | 'fieldTypeParams' => $fieldTypeParams, 53 | 'fieldOptions' => $fieldOptions, 54 | 'validations' => $validations, 55 | ]; 56 | } 57 | 58 | public static function validateFieldsFile($fields) 59 | { 60 | $fieldsArr = []; 61 | 62 | foreach ($fields as $field) { 63 | if (!self::validateFieldInput($field['field'])) { 64 | throw new \RuntimeException('Invalid Input '.$field['field']); 65 | } 66 | 67 | if (isset($field['type'])) { 68 | $type = $field['type']; 69 | } else { 70 | $type = 'text'; 71 | } 72 | 73 | if (isset($field['validations'])) { 74 | $validations = $field['validations']; 75 | } else { 76 | $validations = []; 77 | } 78 | 79 | $fieldsArr[] = self::processFieldInput($field['field'], $type, $validations); 80 | } 81 | 82 | return $fieldsArr; 83 | } 84 | 85 | public static function fillTemplate($variables, $template) 86 | { 87 | foreach ($variables as $variable => $value) { 88 | $template = str_replace($variable, $value, $template); 89 | } 90 | 91 | return $template; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/Mitul/Generator/Utils/TableFieldsGenerator.php: -------------------------------------------------------------------------------- 1 | tableName = $tableName; 27 | $this->schema = DB::getDoctrineSchemaManager($tableName); 28 | // $this->table = $this->schema->listTableDetails($tableName); 29 | 30 | // $this->analyzeIndexes(); 31 | } 32 | 33 | // private function analyzeIndexes() 34 | // { 35 | // $indexes = $this->table->getIndexes(); 36 | // 37 | // $this->uniqueFields = []; 38 | // 39 | // foreach($indexes as $index) 40 | // { 41 | // if($index->isPrimary()) 42 | // { 43 | // $columns = $index->getColumns(); 44 | // 45 | // if(sizeof($columns) == 1) 46 | // { 47 | // $this->primaryKey = $columns[0]; 48 | // } 49 | // } 50 | // 51 | // if($index->isUnique()) 52 | // { 53 | // $columns = $index->getColumns(); 54 | // 55 | // if(sizeof($columns) == 1) 56 | // { 57 | // $column = $columns[0]; 58 | // if($column != $this->primaryKey) 59 | // { 60 | // $this->uniqueFields[] = $column; 61 | // } 62 | // } 63 | // } 64 | // } 65 | // } 66 | 67 | public function generateFieldsFromTable() 68 | { 69 | $this->schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); 70 | $columns = $this->schema->listTableColumns($this->tableName); 71 | 72 | $fields = []; 73 | 74 | foreach ($columns as $column) { 75 | switch ($column->getType()->getName()) { 76 | case 'integer': 77 | $fieldInput = $this->generateIntFieldInput($column->getName(), 'integer', $column); 78 | $type = 'number'; 79 | break; 80 | case 'smallint': 81 | $fieldInput = $this->generateIntFieldInput($column->getName(), 'smallInteger', $column); 82 | $type = 'number'; 83 | break; 84 | case 'bigint': 85 | $fieldInput = $this->generateIntFieldInput($column->getName(), 'bigInteger', $column); 86 | $type = 'number'; 87 | break; 88 | case 'boolean': 89 | $fieldInput = $this->generateSingleFieldInput($column->getName(), 'boolean'); 90 | $type = 'text'; 91 | break; 92 | case 'datetime': 93 | $fieldInput = $this->generateSingleFieldInput($column->getName(), 'dateTime'); 94 | $type = 'date'; 95 | break; 96 | case 'datetimetz': 97 | $fieldInput = $this->generateSingleFieldInput($column->getName(), 'dateTimeTz'); 98 | $type = 'date'; 99 | break; 100 | case 'date': 101 | $fieldInput = $this->generateSingleFieldInput($column->getName(), 'date'); 102 | $type = 'date'; 103 | break; 104 | case 'time': 105 | $fieldInput = $this->generateSingleFieldInput($column->getName(), 'time'); 106 | $type = 'text'; 107 | break; 108 | case 'decimal': 109 | $fieldInput = $this->generateDecimalInput($column, 'decimal'); 110 | $type = 'number'; 111 | break; 112 | case 'float': 113 | $fieldInput = $this->generateFloatInput($column); 114 | $type = 'number'; 115 | break; 116 | case 'string': 117 | $fieldInput = $this->generateStringInput($column); 118 | $type = 'text'; 119 | break; 120 | case 'text': 121 | $fieldInput = $this->generateTextInput($column); 122 | $type = 'textarea'; 123 | break; 124 | default: 125 | $fieldInput = $this->generateTextInput($column); 126 | $type = 'text'; 127 | } 128 | 129 | if (strtolower($column->getName()) == 'password') { 130 | $type = 'password'; 131 | } elseif (strtolower($column->getName()) == 'email') { 132 | $type = 'email'; 133 | } 134 | 135 | if (!empty($fieldInput)) { 136 | // $fieldInput .= $this->checkForDefault($column); 137 | // $fieldInput .= $this->checkForNullable($column); 138 | // $fieldInput .= $this->checkForUnique($column); 139 | $fields [] = GeneratorUtils::processFieldInput($fieldInput, $type, ''); 140 | } 141 | } 142 | 143 | return $fields; 144 | } 145 | 146 | /** 147 | * @param string $name 148 | * @param string $type 149 | * @param \Doctrine\DBAL\Schema\Column $column 150 | * 151 | * @return string 152 | */ 153 | private function generateIntFieldInput($name, $type, $column) 154 | { 155 | $fieldInput = "$name:$type"; 156 | 157 | if ($column->getAutoincrement()) { 158 | $fieldInput .= ',true'; 159 | } 160 | 161 | if ($column->getUnsigned()) { 162 | $fieldInput .= ',true'; 163 | } 164 | 165 | return $fieldInput; 166 | } 167 | 168 | private function generateSingleFieldInput($name, $type) 169 | { 170 | $fieldInput = "$name:$type"; 171 | 172 | return $fieldInput; 173 | } 174 | 175 | /** 176 | * @param \Doctrine\DBAL\Schema\Column $column 177 | * 178 | * @return string 179 | */ 180 | private function generateDecimalInput($column) 181 | { 182 | $fieldInput = $column->getName().':decimal,'.$column->getPrecision().','.$column->getScale(); 183 | 184 | return $fieldInput; 185 | } 186 | 187 | /** 188 | * @param \Doctrine\DBAL\Schema\Column $column 189 | * 190 | * @return string 191 | */ 192 | private function generateFloatInput($column) 193 | { 194 | $fieldInput = $column->getName().':float,'.$column->getPrecision().','.$column->getScale(); 195 | 196 | return $fieldInput; 197 | } 198 | 199 | /** 200 | * @param \Doctrine\DBAL\Schema\Column $column 201 | * @param int $length 202 | * 203 | * @return string 204 | */ 205 | private function generateStringInput($column, $length = 255) 206 | { 207 | $fieldInput = $column->getName().':string,'.$length; 208 | 209 | return $fieldInput; 210 | } 211 | 212 | /** 213 | * @param \Doctrine\DBAL\Schema\Column $column 214 | * 215 | * @return string 216 | */ 217 | private function generateTextInput($column) 218 | { 219 | $fieldInput = $column->getName().':text'; 220 | 221 | return $fieldInput; 222 | } 223 | 224 | // /** 225 | // * @param \Doctrine\DBAL\Schema\Column $column 226 | // * 227 | // * @return string 228 | // */ 229 | // private function checkForNullable($column) 230 | // { 231 | // if(!$column->getNotnull()) 232 | // return ":nullable"; 233 | // 234 | // return ''; 235 | // } 236 | // 237 | // /** 238 | // * @param \Doctrine\DBAL\Schema\Column $column 239 | // * 240 | // * @return string 241 | // */ 242 | // private function checkForDefault($column) 243 | // { 244 | // if($column->getDefault()) 245 | // return ":default," . $column->getDefault(); 246 | // 247 | // return ''; 248 | // } 249 | // 250 | // /** 251 | // * @param \Doctrine\DBAL\Schema\Column $column 252 | // * 253 | // * @return string 254 | // */ 255 | // private function checkForUnique($column) 256 | // { 257 | // if(in_array($column->getName(), $this->uniqueFields)) 258 | // return ":unique"; 259 | // 260 | // return ''; 261 | // } 262 | } 263 | -------------------------------------------------------------------------------- /templates/api/Controller.stub: -------------------------------------------------------------------------------- 1 | $MODEL_NAME_CAMEL$Repository = $$MODEL_NAME_CAMEL$Repo; 18 | } 19 | 20 | /** 21 | * Display a listing of the $MODEL_NAME$. 22 | * GET|HEAD /$MODEL_NAME_PLURAL_CAMEL$ 23 | * 24 | * @return Response 25 | */ 26 | public function index() 27 | { 28 | $$MODEL_NAME_PLURAL_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->all(); 29 | 30 | return $this->sendResponse($$MODEL_NAME_PLURAL_CAMEL$->toArray(), "$MODEL_NAME_PLURAL$ retrieved successfully"); 31 | } 32 | 33 | /** 34 | * Show the form for creating a new $MODEL_NAME$. 35 | * GET|HEAD /$MODEL_NAME_PLURAL_CAMEL$/create 36 | * 37 | * @return Response 38 | */ 39 | public function create() 40 | { 41 | } 42 | 43 | /** 44 | * Store a newly created $MODEL_NAME$ in storage. 45 | * POST /$MODEL_NAME_PLURAL_CAMEL$ 46 | * 47 | * @param Request $request 48 | * 49 | * @return Response 50 | */ 51 | public function store(Request $request) 52 | { 53 | if(sizeof($MODEL_NAME$::$rules) > 0) 54 | $this->validateRequestOrFail($request, $MODEL_NAME$::$rules); 55 | 56 | $input = $request->all(); 57 | 58 | $$MODEL_NAME_PLURAL_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->create($input); 59 | 60 | return $this->sendResponse($$MODEL_NAME_PLURAL_CAMEL$->toArray(), "$MODEL_NAME$ saved successfully"); 61 | } 62 | 63 | /** 64 | * Display the specified $MODEL_NAME$. 65 | * GET|HEAD /$MODEL_NAME_PLURAL_CAMEL$/{id} 66 | * 67 | * @param int $id 68 | * 69 | * @return Response 70 | */ 71 | public function show($id) 72 | { 73 | $$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->apiFindOrFail($id); 74 | 75 | return $this->sendResponse($$MODEL_NAME_CAMEL$->toArray(), "$MODEL_NAME$ retrieved successfully"); 76 | } 77 | 78 | /** 79 | * Show the form for editing the specified $MODEL_NAME$. 80 | * GET|HEAD /$MODEL_NAME_PLURAL_CAMEL$/{id}/edit 81 | * 82 | * @param int $id 83 | * 84 | * @return Response 85 | */ 86 | public function edit($id) 87 | { 88 | // maybe, you can return a template for JS 89 | // Errors::throwHttpExceptionWithCode(Errors::EDITION_FORM_NOT_EXISTS, ['id' => $id], static::getHATEOAS(['%id' => $id])); 90 | } 91 | 92 | /** 93 | * Update the specified $MODEL_NAME$ in storage. 94 | * PUT/PATCH /$MODEL_NAME_PLURAL_CAMEL$/{id} 95 | * 96 | * @param int $id 97 | * @param Request $request 98 | * 99 | * @return Response 100 | */ 101 | public function update($id, Request $request) 102 | { 103 | $input = $request->all(); 104 | 105 | /** @var $MODEL_NAME$ $$MODEL_NAME_CAMEL$ */ 106 | $$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->apiFindOrFail($id); 107 | 108 | $result = $this->$MODEL_NAME_CAMEL$Repository->updateRich($input, $id); 109 | 110 | $$MODEL_NAME_CAMEL$ = $$MODEL_NAME_CAMEL$->fresh(); 111 | 112 | return $this->sendResponse($$MODEL_NAME_CAMEL$->toArray(), "$MODEL_NAME$ updated successfully"); 113 | } 114 | 115 | /** 116 | * Remove the specified $MODEL_NAME$ from storage. 117 | * DELETE /$MODEL_NAME_PLURAL_CAMEL$/{id} 118 | * 119 | * @param int $id 120 | * 121 | * @return Response 122 | */ 123 | public function destroy($id) 124 | { 125 | $this->$MODEL_NAME_CAMEL$Repository->apiDeleteOrFail($id); 126 | 127 | return $this->sendResponse($id, "$MODEL_NAME$ deleted successfully"); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /templates/common/Migration.stub: -------------------------------------------------------------------------------- 1 | where($attribute, $input[$attribute]); 32 | $attributes[$attribute] = $input[$attribute]; 33 | } 34 | else 35 | { 36 | $attributes[$attribute] = null; 37 | } 38 | } 39 | 40 | return [$query->get(), $attributes]; 41 | } 42 | 43 | public function apiFindOrFail($id) 44 | { 45 | $model = $this->find($id); 46 | 47 | if(empty($model)) 48 | { 49 | throw new HttpException(1001, "$MODEL_NAME$ not found"); 50 | } 51 | 52 | return $model; 53 | } 54 | 55 | public function apiDeleteOrFail($id) 56 | { 57 | $model = $this->find($id); 58 | 59 | if(empty($model)) 60 | { 61 | throw new HttpException(1001, "$MODEL_NAME$ not found"); 62 | } 63 | 64 | return $model->delete(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /templates/controller/AppBaseController.stub: -------------------------------------------------------------------------------- 1 | getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes); 21 | 22 | if ($validator->fails()) 23 | { 24 | throw new HttpException(400, $validator->errors()->getMessages()); 25 | } 26 | } 27 | 28 | public function makeResponse($result, $message) 29 | { 30 | return [ 31 | 'data' => $result, 32 | 'message' => $message 33 | ]; 34 | } 35 | 36 | public function sendResponse($result, $message) 37 | { 38 | return Response::json($this->makeResponse($result, $message)); 39 | } 40 | } -------------------------------------------------------------------------------- /templates/routes/api_routes.stub: -------------------------------------------------------------------------------- 1 | '$API_PREFIX$', 'namespace' => 'API'], function () 8 | { 9 | Route::group(['prefix' => '$API_VERSION$'], function () 10 | { 11 | require Config::get('generator.path_api_routes'); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /templates/routes/dingo_api_routes_group.stub: -------------------------------------------------------------------------------- 1 | /* 2 | |-------------------------------------------------------------------------- 3 | | dingo/api and mitulgolakiya/laravel-api-generator api routes 4 | |-------------------------------------------------------------------------- 5 | */ 6 | 7 | $api = app('api.router'); 8 | 9 | $api->group([ 10 | 'version' => '$API_VERSION$', 11 | 'prefix' => '$API_PREFIX$', 12 | 'namespace' => '$API_CONTROLLER_NAMESPACE$', 13 | ], function ($api) 14 | { 15 | include_once __DIR__ . '/api_routes.php'; 16 | 17 | $api->get('errors/{id}', function ($id) 18 | { 19 | return \Mitul\Generator\Errors::getErrors([$id]); 20 | }); 21 | 22 | $api->get('errors', function () 23 | { 24 | return \Mitul\Generator\Errors::getErrors([], [], true); 25 | }); 26 | 27 | $api->get('/', function () 28 | { 29 | $links = []; 30 | 31 | return ['links' => $links]; 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /templates/routes/scaffold_routes.stub: -------------------------------------------------------------------------------- 1 | Route::resource('$MODEL_NAME_PLURAL_CAMEL$', '$MODEL_NAME$Controller'); 2 | 3 | Route::get('$MODEL_NAME_PLURAL_CAMEL$/{id}/delete', [ 4 | 'as' => '$MODEL_NAME_PLURAL_CAMEL$.delete', 5 | 'uses' => '$MODEL_NAME$Controller@destroy', 6 | ]); 7 | -------------------------------------------------------------------------------- /templates/scaffold/Controller.stub: -------------------------------------------------------------------------------- 1 | $MODEL_NAME_CAMEL$Repository = $$MODEL_NAME_CAMEL$Repo; 20 | } 21 | 22 | /** 23 | * Display a listing of the $MODEL_NAME$. 24 | * 25 | * @return Response 26 | */ 27 | public function index() 28 | { 29 | $$MODEL_NAME_PLURAL_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->$RENDER_TYPE$; 30 | 31 | return view('$MODEL_NAME_PLURAL_CAMEL$.index') 32 | ->with('$MODEL_NAME_PLURAL_CAMEL$', $$MODEL_NAME_PLURAL_CAMEL$); 33 | } 34 | 35 | /** 36 | * Show the form for creating a new $MODEL_NAME$. 37 | * 38 | * @return Response 39 | */ 40 | public function create() 41 | { 42 | return view('$MODEL_NAME_PLURAL_CAMEL$.create'); 43 | } 44 | 45 | /** 46 | * Store a newly created $MODEL_NAME$ in storage. 47 | * 48 | * @param Create$MODEL_NAME$Request $request 49 | * 50 | * @return Response 51 | */ 52 | public function store(Create$MODEL_NAME$Request $request) 53 | { 54 | $input = $request->all(); 55 | 56 | $$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->create($input); 57 | 58 | Flash::success('$MODEL_NAME$ saved successfully.'); 59 | 60 | return redirect(route('$MODEL_NAME_PLURAL_CAMEL$.index')); 61 | } 62 | 63 | /** 64 | * Display the specified $MODEL_NAME$. 65 | * 66 | * @param int $id 67 | * 68 | * @return Response 69 | */ 70 | public function show($id) 71 | { 72 | $$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->find($id); 73 | 74 | if(empty($$MODEL_NAME_CAMEL$)) 75 | { 76 | Flash::error('$MODEL_NAME$ not found'); 77 | 78 | return redirect(route('$MODEL_NAME_PLURAL_CAMEL$.index')); 79 | } 80 | 81 | return view('$MODEL_NAME_PLURAL_CAMEL$.show')->with('$MODEL_NAME_CAMEL$', $$MODEL_NAME_CAMEL$); 82 | } 83 | 84 | /** 85 | * Show the form for editing the specified $MODEL_NAME$. 86 | * 87 | * @param int $id 88 | * 89 | * @return Response 90 | */ 91 | public function edit($id) 92 | { 93 | $$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->find($id); 94 | 95 | if(empty($$MODEL_NAME_CAMEL$)) 96 | { 97 | Flash::error('$MODEL_NAME$ not found'); 98 | 99 | return redirect(route('$MODEL_NAME_PLURAL_CAMEL$.index')); 100 | } 101 | 102 | return view('$MODEL_NAME_PLURAL_CAMEL$.edit')->with('$MODEL_NAME_CAMEL$', $$MODEL_NAME_CAMEL$); 103 | } 104 | 105 | /** 106 | * Update the specified $MODEL_NAME$ in storage. 107 | * 108 | * @param int $id 109 | * @param Update$MODEL_NAME$Request $request 110 | * 111 | * @return Response 112 | */ 113 | public function update($id, Update$MODEL_NAME$Request $request) 114 | { 115 | $$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->find($id); 116 | 117 | if(empty($$MODEL_NAME_CAMEL$)) 118 | { 119 | Flash::error('$MODEL_NAME$ not found'); 120 | 121 | return redirect(route('$MODEL_NAME_PLURAL_CAMEL$.index')); 122 | } 123 | 124 | $this->$MODEL_NAME_CAMEL$Repository->updateRich($request->all(), $id); 125 | 126 | Flash::success('$MODEL_NAME$ updated successfully.'); 127 | 128 | return redirect(route('$MODEL_NAME_PLURAL_CAMEL$.index')); 129 | } 130 | 131 | /** 132 | * Remove the specified $MODEL_NAME$ from storage. 133 | * 134 | * @param int $id 135 | * 136 | * @return Response 137 | */ 138 | public function destroy($id) 139 | { 140 | $$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->find($id); 141 | 142 | if(empty($$MODEL_NAME_CAMEL$)) 143 | { 144 | Flash::error('$MODEL_NAME$ not found'); 145 | 146 | return redirect(route('$MODEL_NAME_PLURAL_CAMEL$.index')); 147 | } 148 | 149 | $this->$MODEL_NAME_CAMEL$Repository->delete($id); 150 | 151 | Flash::success('$MODEL_NAME$ deleted successfully.'); 152 | 153 | return redirect(route('$MODEL_NAME_PLURAL_CAMEL$.index')); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /templates/scaffold/requests/CreateRequest.stub: -------------------------------------------------------------------------------- 1 | 5 | 6 | @include('common.errors') 7 | 8 | {!! Form::open(['route' => '$MODEL_NAME_PLURAL_CAMEL$.store']) !!} 9 | 10 | @include('$MODEL_NAME_PLURAL_CAMEL$.fields') 11 | 12 | {!! Form::close() !!} 13 | 14 | @endsection 15 | -------------------------------------------------------------------------------- /templates/scaffold/views/edit.blade.stub: -------------------------------------------------------------------------------- 1 | @extends('layouts.app') 2 | 3 | @section('content') 4 |
5 | 6 | @include('common.errors') 7 | 8 | {!! Form::model($$MODEL_NAME_CAMEL$, ['route' => ['$MODEL_NAME_PLURAL_CAMEL$.update', $$MODEL_NAME_CAMEL$->id], 'method' => 'patch']) !!} 9 | 10 | @include('$MODEL_NAME_PLURAL_CAMEL$.fields') 11 | 12 | {!! Form::close() !!} 13 |
14 | @endsection 15 | -------------------------------------------------------------------------------- /templates/scaffold/views/field.blade.stub: -------------------------------------------------------------------------------- 1 | 2 |
3 | $FIELD_INPUT$ 4 |
-------------------------------------------------------------------------------- /templates/scaffold/views/fields.blade.stub: -------------------------------------------------------------------------------- 1 | $FIELDS$ 2 | 3 |
4 | {!! Form::submit('Save', ['class' => 'btn btn-primary']) !!} 5 |
6 | -------------------------------------------------------------------------------- /templates/scaffold/views/index.blade.stub: -------------------------------------------------------------------------------- 1 | @extends('layouts.app') 2 | 3 | @section('content') 4 | 5 |
6 | 7 | @include('flash::message') 8 | 9 |
10 |

$MODEL_NAME_PLURAL$

11 | Add New 12 |
13 | 14 |
15 | @if($$MODEL_NAME_PLURAL_CAMEL$->isEmpty()) 16 |
No $MODEL_NAME_PLURAL$ found.
17 | @else 18 | @include('$MODEL_NAME_PLURAL_CAMEL$.table') 19 | @endif 20 |
21 | 22 | $PAGINATE$ 23 | 24 |
25 | @endsection 26 | -------------------------------------------------------------------------------- /templates/scaffold/views/paginate.blade.stub: -------------------------------------------------------------------------------- 1 | @include('common.paginate', ['records' => $$MODEL_NAME_PLURAL_CAMEL$]) 2 | -------------------------------------------------------------------------------- /templates/scaffold/views/show.blade.stub: -------------------------------------------------------------------------------- 1 | @extends('layouts.app') 2 | 3 | @section('content') 4 |
5 | @include('$MODEL_NAME_PLURAL_CAMEL$.show_fields') 6 |
7 | @endsection 8 | -------------------------------------------------------------------------------- /templates/scaffold/views/show_field.blade.stub: -------------------------------------------------------------------------------- 1 | 2 |
3 | {!! Form::label('$FIELD_NAME$', '$FIELD_NAME_TITLE$:') !!} 4 |

{!! $$MODEL_NAME_CAMEL$->$FIELD_NAME$ !!}

5 |
-------------------------------------------------------------------------------- /templates/scaffold/views/table.blade.stub: -------------------------------------------------------------------------------- 1 | 2 | 3 | $FIELD_HEADERS$ 4 | 5 | 6 | 7 | @foreach($$MODEL_NAME_PLURAL_CAMEL$ as $$MODEL_NAME_CAMEL$) 8 | 9 | $FIELD_BODY$ 10 | 14 | 15 | @endforeach 16 | 17 |
Action
11 | 12 | 13 |
18 | -------------------------------------------------------------------------------- /views/common/errors.blade.php: -------------------------------------------------------------------------------- 1 | @if(isset($errors) and $errors->any()) 2 | 7 | @endif 8 | -------------------------------------------------------------------------------- /views/common/paginate.blade.php: -------------------------------------------------------------------------------- 1 |
2 | {!! $records->render() !!} 3 |
4 | --------------------------------------------------------------------------------