├── .styleci.yml ├── src ├── Console │ └── Commands │ │ ├── TestCommand.php │ │ └── ResourceMakeCommand.php └── ApiToolkitServiceProvider.php ├── .scrutinizer.yml ├── LICENSE.md ├── .php_cs.cache ├── composer.json ├── config └── api-toolkit.php └── README.md /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: psr2 2 | -------------------------------------------------------------------------------- /src/Console/Commands/TestCommand.php: -------------------------------------------------------------------------------- 1 | comment('API Toolkit working.'); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | build: 2 | nodes: 3 | analysis: 4 | project_setup: 5 | override: true 6 | tests: 7 | override: [php-scrutinizer-run] 8 | 9 | filter: 10 | excluded_paths: [tests/*] 11 | 12 | checks: 13 | php: 14 | remove_extra_empty_lines: true 15 | remove_php_closing_tag: true 16 | remove_trailing_whitespace: true 17 | fix_use_statements: 18 | remove_unused: true 19 | preserve_multiple: false 20 | preserve_blanklines: true 21 | order_alphabetically: true 22 | fix_php_opening_tag: true 23 | fix_linefeed: true 24 | fix_line_ending: true 25 | fix_identation_4spaces: true 26 | fix_doc_comments: true 27 | 28 | tools: 29 | external_code_coverage: 30 | timeout: 600 31 | runs: 3 32 | -------------------------------------------------------------------------------- /src/ApiToolkitServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 17 | $this->publishes([ 18 | __DIR__ . '/../config/api-toolkit.php' => config_path('api-toolkit.php'), 19 | ], 'config'); 20 | 21 | // publish any resources 22 | 23 | $this->commands([ 24 | \JustSteveKing\Laravel\ApiToolkit\Console\Commands\TestCommand::class, 25 | \JustSteveKing\Laravel\ApiToolkit\Console\Commands\ResourceMakeCommand::class, 26 | ]); 27 | } 28 | } 29 | 30 | public function register() 31 | { 32 | $this->mergeConfigFrom( 33 | __DIR__ . '/../config/api-toolkit.php', 34 | 'api-toolkit' 35 | ); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Steve McDougall 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /.php_cs.cache: -------------------------------------------------------------------------------- 1 | {"php":"7.4.13","version":"2.17.1","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sortAlgorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline_array":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":["method"]},"single_trait_insert_per_statement":true},"hashes":{"src\/ApiToolkitServiceProvider.php":4269201547,"src\/Console\/Commands\/ResourceMakeCommand.php":537875139,"src\/Console\/Commands\/TestCommand.php":3298154856,"tests\/TestCase.php":1998369022,"tests\/Feature\/Console\/Commands\/ResourceMakeCommandTest.php":1046345983,"tests\/Feature\/Console\/Commands\/TestCommandTest.php":3984122945}} -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "juststeveking/laravel-api-toolkit", 3 | "description": "A toolkit for creating APIs in Laravel", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "role": "developer", 9 | "name": "Steve McDougall", 10 | "email": "juststevemcd@gmail.com", 11 | "homepage": "https://www.juststeveking.uk/" 12 | } 13 | ], 14 | "require": { 15 | "php": "^7.4|^8.0", 16 | "illuminate/support": "^8.19|^9.0" 17 | }, 18 | "require-dev": { 19 | "friendsofphp/php-cs-fixer": "^2.17", 20 | "orchestra/testbench": "^6.7|^7.0", 21 | "phpunit/phpunit": "^9.5", 22 | "vimeo/psalm": "^4.3" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "JustSteveKing\\Laravel\\ApiToolkit\\": "src/" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "JustSteveKing\\Laravel\\ApiToolkit\\Tests\\": "tests/" 32 | } 33 | }, 34 | "scripts": { 35 | "test": "vendor/bin/phpunit --testdox", 36 | "test-coverage": "@test --coverage-html coverage", 37 | "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes", 38 | "analyse": "vendor/bin/psalm" 39 | }, 40 | "config": { 41 | "sort-packages": true, 42 | "allow-plugins": { 43 | "composer/package-versions-deprecated": true 44 | } 45 | }, 46 | "extra": { 47 | "laravel": { 48 | "providers": [ 49 | "JustSteveKing\\Laravel\\ApiToolkit\\ApiToolkitServiceProvider" 50 | ] 51 | } 52 | }, 53 | "minimum-stability": "dev", 54 | "prefer-stable": true 55 | } 56 | -------------------------------------------------------------------------------- /config/api-toolkit.php: -------------------------------------------------------------------------------- 1 | '%sResource', 9 | 10 | /** 11 | * The pattern to use for Policy Naming. 12 | * This will be used with sprintf(). 13 | */ 14 | 'policy_name' => '%sPolicy', 15 | 16 | /** 17 | * The pattern to use for Seeder Naming. 18 | * This will be used with sprintf(). 19 | */ 20 | 'seeder_name' => '%sSeeder', 21 | 22 | /** 23 | * The names of the Controllers to generate for each API Resource, 24 | * Please include any options you may want to include. 25 | * By default they are invokable controllers. 26 | */ 27 | 'controllers' => [ 28 | [ 29 | 'name' => 'IndexController', 30 | 'options' => [ 31 | '--invokable', 32 | ] 33 | ], 34 | [ 35 | 'name' => 'CreateController', 36 | 'options' => [ 37 | '--invokable', 38 | ] 39 | ], 40 | [ 41 | 'name' => 'ShowController', 42 | 'options' => [ 43 | '--invokable', 44 | ] 45 | ], 46 | [ 47 | 'name' => 'UpdateController', 48 | 'options' => [ 49 | '--invokable', 50 | ] 51 | ], 52 | [ 53 | 'name' => 'DeleteController', 54 | 'options' => [ 55 | '--invokable', 56 | ] 57 | ], 58 | ], 59 | 60 | /** 61 | * The names of the Form Requests to generate for each API Resource. 62 | */ 63 | 'form_requests' => [ 64 | 'CreateRequest', 65 | 'UpdateRequest', 66 | ], 67 | ]; 68 | -------------------------------------------------------------------------------- /src/Console/Commands/ResourceMakeCommand.php: -------------------------------------------------------------------------------- 1 | argument('name'))) { 17 | return; 18 | } 19 | 20 | $name = Str::studly($this->argument('name')); 21 | $this->comment("Generating boilerplate for {$name}"); 22 | 23 | $this->makeModel($name); 24 | $this->makeFactory($name); 25 | $this->makeSeeder($name); 26 | $this->makePolicy($name); 27 | $this->makeFormRequests($name); 28 | $this->makeApiResource($name); 29 | $this->makeControllers($name); 30 | } 31 | 32 | protected function makeModel(string $name): void 33 | { 34 | $this->comment("Creating Model and Migration for {$name}"); 35 | $this->call("make:model", [ 36 | 'name' => $name, 37 | '-m', 38 | ]); 39 | } 40 | 41 | protected function makeFactory(string $name): void 42 | { 43 | $this->comment("Creating Factory for {$name}"); 44 | $this->call("make:factory", [ 45 | 'name' => "{$name}Factory", 46 | '--model' => $name, 47 | ]); 48 | } 49 | 50 | protected function makeSeeder(string $name): void 51 | { 52 | $this->comment("Creating Seeder for {$name}"); 53 | $this->call("make:seeder", [ 54 | 'name' => sprintf(config('api-toolkit.seeder_name'), $name), 55 | ]); 56 | } 57 | 58 | protected function makePolicy(string $name): void 59 | { 60 | $this->comment("Creating Model Policy for {$name}"); 61 | $this->call('make:policy', [ 62 | 'name' => sprintf(config('api-toolkit.policy_name'), $name), 63 | '--model' => $name, 64 | ]); 65 | } 66 | 67 | protected function makeFormRequests(string $name): void 68 | { 69 | foreach (config('api-toolkit.form_requests') as $request) { 70 | $this->comment("Creating {$request} for model {$name}"); 71 | $this->call('make:request', [ 72 | 'name' => "Api\\{$name}\\{$request}", 73 | ]); 74 | } 75 | } 76 | 77 | protected function makeApiResource(string $name): void 78 | { 79 | $this->comment("Creating API Resource for {$name}"); 80 | $this->call('make:resource', [ 81 | 'name' => sprintf(config('api-toolkit.resource_name'), $name), 82 | ]); 83 | } 84 | 85 | protected function makeControllers(string $name): void 86 | { 87 | foreach (config('api-toolkit.controllers') as $controller) { 88 | $this->comment("Creating {$controller['name']} for model {$name}"); 89 | $this->call('make:controller', [ 90 | 'name' => "{$name}\\{$controller['name']}", 91 | implode(' ', $controller['options']), 92 | ]); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A toolkit for creating APIs in Laravel 2 | 3 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 4 | [![PHP Version](https://img.shields.io/packagist/php-v/juststeveking/php-sdk.svg?style=flat-square)](https://php.net) 5 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/juststeveking/laravel-api-toolkit.svg?style=flat-square)](https://packagist.org/packages/juststeveking/laravel-api-toolkit) 6 | ![Tests](https://github.com/JustSteveKing/laravel-api-toolkit/workflows/Tests/badge.svg) 7 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/JustSteveKing/laravel-api-toolkit/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/JustSteveKing/laravel-api-toolkit/?branch=main) 8 | [![Total Downloads](https://img.shields.io/packagist/dt/juststeveking/laravel-api-toolkit.svg?style=flat-square)](https://packagist.org/packages/juststeveking/laravel-api-toolkit) 9 | 10 | A toolkit for creating APIs in Laravel. 11 | 12 | ## Installation 13 | 14 | You can install the package via composer: 15 | 16 | ```bash 17 | composer require juststeveking/laravel-api-toolkit 18 | ``` 19 | 20 | You can publish the config file with: 21 | 22 | ```bash 23 | php artisan vendor:publish --provider="JustSteveKing\Laravel\ApiToolkit\ApiToolkitServiceProvider" --tag="config" 24 | ``` 25 | 26 | This is the contents of the published config file: 27 | 28 | ```php 29 | return [ 30 | 'resource_name' => '%sResource', 31 | 'policy_name' => '%sPolicy', 32 | 'seeder_name' => '%sSeeder', 33 | 'controllers' => [ 34 | [ 35 | 'name' => 'IndexController', 36 | 'options' => [ 37 | '--invokable', 38 | ] 39 | ], 40 | [ 41 | 'name' => 'CreateController', 42 | 'options' => [ 43 | '--invokable', 44 | ] 45 | ], 46 | [ 47 | 'name' => 'ShowController', 48 | 'options' => [ 49 | '--invokable', 50 | ] 51 | ], 52 | [ 53 | 'name' => 'UpdateController', 54 | 'options' => [ 55 | '--invokable', 56 | ] 57 | ], 58 | [ 59 | 'name' => 'DeleteController', 60 | 'options' => [ 61 | '--invokable', 62 | ] 63 | ], 64 | ], 65 | 'form_requests' => [ 66 | 'CreateRequest', 67 | 'UpdateRequest', 68 | ], 69 | ]; 70 | ``` 71 | 72 | ## Usage 73 | 74 | To get starteed using this, there is only one command you really need to worry about: `api-toolkit:resource`. 75 | 76 | The only option you need to pass it is the eloquent Model you wish to create and create the APi blueprint for. 77 | 78 | ```bash 79 | php artisan api-toolkit:resource Post 80 | ``` 81 | 82 | The above command will generate: 83 | 84 | - `app/Models/Post.php` 85 | - `app/Policies/PostPolicy.php` - Class name can be altered in config 86 | - `app/Http/Resources/PostResource.php` - Class name can be altered in config 87 | - `app/Http/Requests/Api/Post/CreateRequest.php` - Class name can be altered in config 88 | - `app/Http/Requests/Api/Post/UpdateRequest.php` - Class name can be altered in config 89 | - `app/Http/Controllers/Post/IndexController.php` - Class name can be altered in config 90 | - `app/Http/Controllers/Post/CreateController.php` - Class name can be altered in config 91 | - `app/Http/Controllers/Post/ShowController.php` - Class name can be altered in config 92 | - `app/Http/Controllers/Post/UpdateController.php` - Class name can be altered in config 93 | - `app/Http/Controllers/Post/DeleteController.php` - Class name can be altered in config 94 | - `database/seeds/PostSeeder.php` - Class name can be altered in config 95 | - `database/factories/PostFactory.php` 96 | - `database/migrations/xxxx_xx_xx_xxxxxx_create_posts_table.php` 97 | 98 | ## Testing 99 | ```bash 100 | composer test 101 | ``` 102 | 103 | ## Changelog 104 | 105 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 106 | 107 | ## Contributing 108 | 109 | Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. 110 | 111 | ## Security Vulnerabilities 112 | 113 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 114 | 115 | ## Credits 116 | 117 | - [Steve McDougall](https://github.com/JustSteveKing) 118 | - [All Contributors](../../contributors) 119 | 120 | ## License 121 | 122 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 123 | --------------------------------------------------------------------------------