├── .gitignore ├── README.md ├── composer.json ├── licence.md └── src ├── ApiGeneratorProvider.php ├── Commands └── GenerateApiCommand.php ├── Generators ├── ApiControllerGenerator.php ├── ApiRouteGenerator.php └── Template.php └── templates └── api-controller.stub /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | composer.lock 3 | vendor -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # API Generator for Laravel 5.4 and above 2 | 3 | Package to create API Controller and Route entry with one `Artisan` command. 4 | 5 | For now we're starting with only one simple command and will expand functionality as needed. Please submit your suggestions in `Issues` section. 6 | 7 | __Notice__: if you want to generate not only API, but full admin panel - check out our [QuickAdminPanel.com](https://quickadminpanel.com) 8 | 9 | # Installation and Usage 10 | 11 | 1. Install the package via `composer require laraveldaily/apigenerator` 12 | 13 | 2. Add `Laraveldaily\Apigenerator\ApiGeneratorProvider::class` to your `config\app.php` providers. 14 | 15 | 3. That's it: run `php artisan make:api --model=XXXXX` where XXXXX is your model name. 16 | 17 | This command will generate API Controller and new entry in `routes/api.php` file. 18 | 19 | __Notice__: Model should exist already, our package won't create it. 20 | 21 | 22 | __Example__ 23 | 24 | `php artisan make:api --model=Project` 25 | 26 | Will generate the file __app\Http\Controllers\Api\ProjectsController.php__: 27 | 28 | ``` 29 | all()); 47 | 48 | return $project; 49 | } 50 | 51 | public function show($id) 52 | { 53 | return Project::findOrFail($id); 54 | } 55 | 56 | public function update(Request $request, $id) 57 | { 58 | $project = Project::findOrFail($id); 59 | $project->update($request->all()); 60 | 61 | return $project; 62 | } 63 | 64 | public function destroy($id) 65 | { 66 | $project = Project::findOrFail($id); 67 | $project->delete(); 68 | 69 | return ''; 70 | } 71 | } 72 | 73 | ``` 74 | 75 | And this line will be added to `routes/api.php`: 76 | 77 | ``` 78 | Route::resource('projects', 'Api/ProjectsController', ['except' => ['create', 'edit']]); 79 | ``` 80 | 81 | # License 82 | 83 | The MIT License (MIT). Please see [License File](https://github.com/LaravelDaily/api-generator/blob/master/license.md) for more information. 84 | 85 | --- 86 | 87 | ## More from our LaravelDaily Team 88 | 89 | - Check out our adminpanel generator [QuickAdminPanel](https://quickadminpanel.com) 90 | - Read our [Blog with Laravel Tutorials](https://laraveldaily.com) 91 | - FREE E-book: [50 Laravel Quick Tips (and counting)](https://laraveldaily.com/free-e-book-40-laravel-quick-tips-and-counting/) 92 | - Subscribe to our [YouTube channel Laravel Business](https://www.youtube.com/channel/UCTuplgOBi6tJIlesIboymGA) 93 | - Enroll in our [Laravel Online Courses](https://laraveldaily.teachable.com/) 94 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laraveldaily/apigenerator", 3 | "description": "API Generator for Laravel 5", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "KarolisNarkevicius", 8 | "email": "narkevicius.karolis@gmail.com" 9 | }, 10 | { 11 | "name": "PovilasKorop", 12 | "email": "povilas@laraveldaily.com" 13 | } 14 | ], 15 | "version": "0.1.9", 16 | "require": { 17 | "laravel/framework": "5.4.*|5.5.*|5.6.*|5.7.*" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "Laraveldaily\\Apigenerator\\": "src/" 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /licence.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2015 LaravelDaily 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /src/ApiGeneratorProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 18 | $this->commands([ 19 | GenerateApiCommand::class, 20 | ]); 21 | } 22 | } 23 | 24 | /** 25 | * Register the application services. 26 | * 27 | * @return void 28 | */ 29 | public function register() 30 | { 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Commands/GenerateApiCommand.php: -------------------------------------------------------------------------------- 1 | ask('Models folder', 'app'), '/'); 28 | $model = ucfirst($this->option('model')); 29 | 30 | if (!file_exists($modelPath = base_path("$folder/$model.php"))) { 31 | $this->error('Model not found at ' . $modelPath); 32 | return null; 33 | } 34 | 35 | $controller = new ApiControllerGenerator($model); 36 | 37 | //check controller 38 | if ($controller->exists()) { 39 | //override? 40 | if ($this->confirm('Controller exists. Do you wish to override?')) { 41 | if ($controller->create()) { 42 | $this->info('Created API controller.'); 43 | } else { 44 | $this->error('Couldn\'t create the controller.'); 45 | } 46 | } 47 | } else { 48 | $controller->create(); 49 | $this->info('Created API controller.'); 50 | } 51 | 52 | $route = new ApiRouteGenerator(); 53 | if (!$route->resource(str_plural($model))) { 54 | $route->generate(); 55 | $this->info('Created API route.'); 56 | } else { 57 | $this->info('API route already exists.'); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Generators/ApiControllerGenerator.php: -------------------------------------------------------------------------------- 1 | name = $name; 14 | 15 | $variables = [ 16 | '$_MODEL_NAME_LOWER_$' => lcfirst($name), 17 | '$_MODEL_NAME_$' => $name, 18 | '$_MODEL_NAME_PLURAL_$' => str_plural($name), 19 | ]; 20 | 21 | $this->template = new Template(); 22 | $this->template->stub('api-controller')->variables($variables)->generate(); 23 | 24 | } 25 | 26 | public function exists() 27 | { 28 | return file_exists(base_path('app/Http/Controllers/Api/' . str_plural($this->name) . 'Controller.php')); 29 | } 30 | 31 | public function create() 32 | { 33 | //check if api path exists 34 | if (!file_exists(base_path('app/Http/Controllers/Api'))) { 35 | //if not create 36 | mkdir(base_path('app/Http/Controllers/Api'), 775); 37 | } 38 | 39 | file_put_contents(base_path('app/Http/Controllers/Api/' . str_plural($this->name) . 'Controller.php'), $this->template->string); 40 | 41 | if ($this->exists()) { 42 | return true; 43 | } 44 | return false; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Generators/ApiRouteGenerator.php: -------------------------------------------------------------------------------- 1 | blockStart = '#autogenerated'; 19 | $this->blockEnd = '#endautogenerated'; 20 | 21 | $this->routePath = base_path('routes/api.php'); 22 | 23 | $this->blockPattern = '/' . $this->blockStart . '(.+)' . $this->blockEnd . '/si'; 24 | 25 | $this->blockExists = false; 26 | 27 | if (!file_exists($this->routePath)) { 28 | file_put_contents('routePath); 29 | } 30 | 31 | $this->file = file_get_contents($this->routePath); 32 | 33 | // remove php ending tag 34 | // because we always append to the end 35 | $this->file = str_replace('?>', '', $this->file); 36 | 37 | $this->makeBlock(); 38 | 39 | } 40 | 41 | public function generate() 42 | { 43 | array_unshift($this->block, $this->blockStart); 44 | $this->block[] = $this->blockEnd; 45 | $string = implode("\r\n", $this->block); 46 | if ($this->blockExists) { 47 | $this->file = preg_replace($this->blockPattern, $string, $this->file); 48 | } else { 49 | $this->file .= $string; 50 | } 51 | file_put_contents($this->routePath, $this->file); 52 | } 53 | 54 | public function resource($name) 55 | { 56 | $line = "Route::apiResource('" . lcfirst($name) . "', 'Api\\" . $name . "Controller');"; 57 | if (!$this->blockHasResource($name)) { 58 | $this->block[] = $line; 59 | return false; 60 | } 61 | return true; 62 | } 63 | 64 | private function makeBlock() 65 | { 66 | $regex = $this->blockPattern; 67 | $this->blockExists = preg_match($regex, $this->file, $matches); 68 | if ($this->blockExists) { 69 | $matches = trim($matches[1]); 70 | $matches = preg_split('/$\R?^/m', $matches); 71 | foreach ($matches as $match) { 72 | $m = trim($match); 73 | if ($m != '') { 74 | $this->block[] = $m; 75 | } 76 | } 77 | if ($this->block == null) { 78 | $this->block = []; 79 | } 80 | } else { 81 | $this->block = []; 82 | } 83 | } 84 | 85 | private function blockHasResource($name) 86 | { 87 | $block = implode('\n', $this->block); 88 | if ($block == '') { 89 | return false; 90 | } 91 | if (preg_match(preg_quote("/Route::resource('" . lcfirst($name) . "', 'Api\\" . $name . "Controller'/"), $block)) { 92 | return true; 93 | } 94 | return false; 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /src/Generators/Template.php: -------------------------------------------------------------------------------- 1 | template = file_get_contents(__DIR__ . '/../templates/' . $name . '.stub'); 14 | return $this; 15 | } 16 | 17 | public function variables($variables) 18 | { 19 | $this->variables = $variables; 20 | return $this; 21 | } 22 | 23 | public function generate() 24 | { 25 | $this->string = str_replace(array_keys($this->variables), array_values($this->variables), $this->template); 26 | return $this; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/templates/api-controller.stub: -------------------------------------------------------------------------------- 1 | all()); 19 | 20 | return $$_MODEL_NAME_LOWER_$; 21 | } 22 | 23 | public function show($id) 24 | { 25 | return $_MODEL_NAME_$::findOrFail($id); 26 | } 27 | 28 | public function update(Request $request, $id) 29 | { 30 | $$_MODEL_NAME_LOWER_$ = $_MODEL_NAME_$::findOrFail($id); 31 | $$_MODEL_NAME_LOWER_$->update($request->all()); 32 | 33 | return $$_MODEL_NAME_LOWER_$; 34 | } 35 | 36 | public function destroy($id) 37 | { 38 | $$_MODEL_NAME_LOWER_$ = $_MODEL_NAME_$::findOrFail($id); 39 | $$_MODEL_NAME_LOWER_$->delete(); 40 | 41 | return ''; 42 | } 43 | } 44 | --------------------------------------------------------------------------------