├── .gitignore ├── LICENSE.md ├── README.md ├── composer.json ├── src ├── ASTServiceProvider.php ├── MakeAction.php ├── MakeService.php ├── MakeTrait.php └── Stubs │ ├── action.stub │ ├── interface.stub │ ├── service.interface.stub │ ├── service.stub │ └── trait.stub └── tests ├── ActionTests.php ├── ServiceTests.php └── TraitTests.php /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PrevailExcel/laravel-action-service-trait/fc8ae14b6cfb7ae4e6b43a08d7ee7bd8581a27b5/.gitignore -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2023 Prevail Ejimadu 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-action-service-trait 2 | 3 | [![Latest Stable Version](http://poser.pugx.org/prevailexcel/laravel-action-service-trait/v)](https://packagist.org/packages/prevailexcel/laravel-action-service-trait) 4 | [![License](http://poser.pugx.org/prevailexcel/laravel-action-service-trait/license)](https://packagist.org/packages/prevailexcel/laravel-action-service-trait) 5 | 6 | 7 | > A Simple Package to create actions, traits and services using artisan commands in laravel. 8 | 9 | This package extends the `make:` commands to help you easily create traits, action classes and service classes in Laravel 5+. It also comes with the option of creating an interface for the service. 10 | 11 | # Install 12 | ```bash 13 | composer require prevailexcel/laravel-action-service-trait 14 | ``` 15 | 16 | Or add the following line to the require block of your `composer.json` file. 17 | 18 | ``` 19 | "prevailexcel/laravel-action-service-trait": "1.0.*" 20 | ``` 21 | 22 | You'll then need to run `composer install` or `composer update` to download it and have the autoloader updated. 23 | 24 | Once it is installed, you can use any of the commands in your terminal. 25 | 26 | # Usage 27 | ```bash 28 | php artisan make:action {name} 29 | ``` 30 | ```bash 31 | php artisan make:service {name} 32 | ``` 33 | ```bash 34 | php artisan make:service {name} --i 35 | ``` 36 | ```bash 37 | php artisan make:trait {name} 38 | ``` 39 | 40 | # Examples 41 | 42 | ## Create an action class 43 | ```bash 44 | $ php artisan make:action CreateUser 45 | ``` 46 | `/app/Actions/CreateUser.php` 47 | ```php 48 | (C) 11 | * 12 | * For the full copyright and license information, please view the LICENSE 13 | * file that was distributed with this source code. 14 | */ 15 | 16 | class ASTServiceProvider extends ServiceProvider 17 | { 18 | /** 19 | * Register the application services. 20 | * 21 | * @return void 22 | */ 23 | public function register(): void 24 | { 25 | $this->commands(MakeAction::class); 26 | $this->commands(MakeService::class); 27 | $this->commands(MakeTrait::class); 28 | } 29 | } -------------------------------------------------------------------------------- /src/MakeAction.php: -------------------------------------------------------------------------------- 1 | (C) 11 | * 12 | * For the full copyright and license information, please view the LICENSE 13 | * file that was distributed with this source code. 14 | */ 15 | 16 | class MakeAction extends GeneratorCommand 17 | { 18 | /** 19 | * STUB_PATH. 20 | */ 21 | const STUB_PATH = __DIR__ . '/Stubs/'; 22 | 23 | /** 24 | * The name and signature of the console command. 25 | * 26 | * @var string 27 | */ 28 | protected $signature = 'make:action {name : Create an action class}'; 29 | 30 | /** 31 | * The console command description. 32 | * 33 | * @var string 34 | */ 35 | protected $description = 'Create a new action class'; 36 | 37 | /** 38 | * The type of class being generated. 39 | * 40 | * @var string 41 | */ 42 | protected $type = 'Action'; 43 | 44 | 45 | /** 46 | * @return string 47 | */ 48 | protected function getStub() 49 | { 50 | return self::STUB_PATH . 'action.stub'; 51 | 52 | } 53 | 54 | /** 55 | * Execute the console command. 56 | * 57 | * @return bool|null 58 | * 59 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 60 | * @see \Illuminate\Console\GeneratorCommand 61 | * 62 | */ 63 | public function handle() 64 | { 65 | if ($this->isReservedName($this->getNameInput())) { 66 | $this->error('The name "' . $this->getNameInput() . '" is reserved by PHP.'); 67 | 68 | return false; 69 | } 70 | 71 | $name = $this->qualifyClass($this->getNameInput()); 72 | 73 | $path = $this->getPath($name); 74 | 75 | if ((! $this->hasOption('force') || 76 | ! $this->option('force')) && 77 | $this->alreadyExists($this->getNameInput())) { 78 | $this->error($this->type . ' already exists!'); 79 | 80 | return false; 81 | } 82 | 83 | $this->makeDirectory($path); 84 | 85 | $this->files->put( 86 | $path, 87 | $this->sortImports( 88 | $this->buildServiceClass($name) 89 | ) 90 | ); 91 | $message = $this->type; 92 | 93 | $this->info($message . ' created successfully.'); 94 | } 95 | 96 | /** 97 | * Build the class with the given name. 98 | * 99 | * @param string $name 100 | * @param $isInterface 101 | * @return string 102 | * 103 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 104 | */ 105 | protected function buildServiceClass(string $name): string 106 | { 107 | $stub = $this->files->get( 108 | $this->getStub() 109 | ); 110 | 111 | return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); 112 | } 113 | 114 | 115 | /** 116 | * @param $rootNamespace 117 | * @return string 118 | */ 119 | protected function getDefaultNamespace($rootNamespace): string 120 | { 121 | return $rootNamespace . '\Actions'; 122 | } 123 | } -------------------------------------------------------------------------------- /src/MakeService.php: -------------------------------------------------------------------------------- 1 | (C) 11 | * 12 | * For the full copyright and license information, please view the LICENSE 13 | * file that was distributed with this source code. 14 | */ 15 | 16 | class MakeService extends GeneratorCommand 17 | { 18 | /** 19 | * STUB_PATH. 20 | */ 21 | const STUB_PATH = __DIR__ . '/Stubs/'; 22 | 23 | /** 24 | * The name and signature of the console command. 25 | * 26 | * @var string 27 | */ 28 | protected $signature = 'make:service {name : Create a service class} {--i : Create a service interface}'; 29 | 30 | /** 31 | * The console command description. 32 | * 33 | * @var string 34 | */ 35 | protected $description = 'Create a new service class and optional interface'; 36 | 37 | /** 38 | * The type of class being generated. 39 | * 40 | * @var string 41 | */ 42 | protected $type = 'Service'; 43 | 44 | 45 | /** 46 | * @return string 47 | */ 48 | protected function getStub(): string 49 | { 50 | return self::STUB_PATH . 'service.stub'; 51 | } 52 | 53 | /** 54 | * @return string 55 | */ 56 | protected function getServiceInterfaceStub(): string 57 | { 58 | return self::STUB_PATH . 'service.interface.stub'; 59 | } 60 | 61 | /** 62 | * @return string 63 | */ 64 | protected function getInterfaceStub(): string 65 | { 66 | return self::STUB_PATH . 'interface.stub'; 67 | } 68 | 69 | /** 70 | * Execute the console command. 71 | * 72 | * @return bool|null 73 | * 74 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 75 | * @see \Illuminate\Console\GeneratorCommand 76 | * 77 | */ 78 | public function handle() 79 | { 80 | if ($this->isReservedName($this->getNameInput())) { 81 | $this->error('The name "' . $this->getNameInput() . '" is reserved by PHP.'); 82 | 83 | return false; 84 | } 85 | 86 | $name = $this->qualifyClass($this->getNameInput()); 87 | 88 | $path = $this->getPath($name); 89 | 90 | if ((! $this->hasOption('force') || 91 | ! $this->option('force')) && 92 | $this->alreadyExists($this->getNameInput())) { 93 | $this->error($this->type . ' already exists!'); 94 | 95 | return false; 96 | } 97 | 98 | $this->makeDirectory($path); 99 | $hasInterface = $this->option('i'); 100 | 101 | $this->files->put( 102 | $path, 103 | $this->sortImports( 104 | $this->buildServiceClass($name, $hasInterface) 105 | ) 106 | ); 107 | $message = $this->type; 108 | 109 | // If the option for interface exists 110 | if ($hasInterface) { 111 | $interfaceName = $this->getNameInput() . 'Interface.php'; 112 | $interfacePath = str_replace($this->getNameInput() . '.php', 'Interfaces/', $path); 113 | 114 | $this->makeDirectory($interfacePath . $interfaceName); 115 | 116 | $this->files->put( 117 | $interfacePath . $interfaceName, 118 | $this->sortImports( 119 | $this->buildServiceInterface($this->getNameInput()) 120 | ) 121 | ); 122 | 123 | $message .= ' and Interface'; 124 | } 125 | 126 | $this->info($message . ' created successfully.'); 127 | } 128 | 129 | /** 130 | * Build the class with the given name. 131 | * 132 | * @param string $name 133 | * @param $hasInterface 134 | * @return string 135 | * 136 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 137 | */ 138 | protected function buildServiceClass(string $name, $hasInterface): string 139 | { 140 | $stub = $this->files->get( 141 | $hasInterface ? $this->getServiceInterfaceStub() : $this->getStub() 142 | ); 143 | 144 | return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); 145 | } 146 | 147 | /** 148 | * Build the class with the given name. 149 | * 150 | * @param string $name 151 | * @return string 152 | * 153 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 154 | */ 155 | protected function buildServiceInterface(string $name): string 156 | { 157 | $stub = $this->files->get($this->getInterfaceStub()); 158 | 159 | return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); 160 | } 161 | 162 | /** 163 | * @param $rootNamespace 164 | * @return string 165 | */ 166 | protected function getDefaultNamespace($rootNamespace): string 167 | { 168 | return $rootNamespace . '\Services'; 169 | } 170 | } -------------------------------------------------------------------------------- /src/MakeTrait.php: -------------------------------------------------------------------------------- 1 | (C) 11 | * 12 | * For the full copyright and license information, please view the LICENSE 13 | * file that was distributed with this source code. 14 | */ 15 | 16 | class MakeTrait extends GeneratorCommand 17 | { 18 | /** 19 | * STUB_PATH. 20 | */ 21 | const STUB_PATH = __DIR__ . '/Stubs/'; 22 | 23 | /** 24 | * The name and signature of the console command. 25 | * 26 | * @var string 27 | */ 28 | protected $signature = 'make:trait {name : Create a php trait}'; 29 | 30 | /** 31 | * The console command description. 32 | * 33 | * @var string 34 | */ 35 | protected $description = 'Create a new Create a php trait'; 36 | 37 | /** 38 | * The type of class being generated. 39 | * 40 | * @var string 41 | */ 42 | protected $type = 'Trait'; 43 | 44 | 45 | /** 46 | * @return string 47 | */ 48 | protected function getStub() 49 | { 50 | return self::STUB_PATH . 'trait.stub'; 51 | 52 | } 53 | 54 | /** 55 | * Execute the console command. 56 | * 57 | * @return bool|null 58 | * 59 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 60 | * @see \Illuminate\Console\GeneratorCommand 61 | * 62 | */ 63 | public function handle() 64 | { 65 | if ($this->isReservedName($this->getNameInput())) { 66 | $this->error('The name "' . $this->getNameInput() . '" is reserved by PHP.'); 67 | 68 | return false; 69 | } 70 | 71 | $name = $this->qualifyClass($this->getNameInput()); 72 | 73 | $path = $this->getPath($name); 74 | 75 | if ((! $this->hasOption('force') || 76 | ! $this->option('force')) && 77 | $this->alreadyExists($this->getNameInput())) { 78 | $this->error($this->type . ' already exists!'); 79 | 80 | return false; 81 | } 82 | 83 | $this->makeDirectory($path); 84 | 85 | $this->files->put( 86 | $path, 87 | $this->sortImports( 88 | $this->buildServiceClass($name) 89 | ) 90 | ); 91 | $message = $this->type; 92 | 93 | $this->info($message . ' created successfully.'); 94 | } 95 | 96 | /** 97 | * Build the class with the given name. 98 | * 99 | * @param string $name 100 | * @param $isInterface 101 | * @return string 102 | * 103 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 104 | */ 105 | protected function buildServiceClass(string $name): string 106 | { 107 | $stub = $this->files->get( 108 | $this->getStub() 109 | ); 110 | 111 | return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); 112 | } 113 | 114 | 115 | /** 116 | * @param $rootNamespace 117 | * @return string 118 | */ 119 | protected function getDefaultNamespace($rootNamespace): string 120 | { 121 | return $rootNamespace . '\Traits'; 122 | } 123 | } -------------------------------------------------------------------------------- /src/Stubs/action.stub: -------------------------------------------------------------------------------- 1 | app = require __DIR__ . '/../../bootstrap/app.php'; 20 | } 21 | 22 | /** 23 | * @return void 24 | */ 25 | public function test_make_action_command(): void 26 | { 27 | $this->artisan('make:action TestAction')->assertSuccessful(); 28 | } 29 | 30 | /** 31 | * @return void 32 | */ 33 | public function test_action_stub_exist(): void 34 | { 35 | $this->assertFileExists(__DIR__ . '/../src/Stubs/action.stub'); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /tests/ServiceTests.php: -------------------------------------------------------------------------------- 1 | app = require __DIR__ . '/../../bootstrap/app.php'; 20 | } 21 | 22 | /** 23 | * @return void 24 | */ 25 | public function test_make_service_command(): void 26 | { 27 | $this->artisan('make:service TestService')->assertSuccessful(); 28 | } 29 | 30 | /** 31 | * @return void 32 | */ 33 | public function test_service_stub_exist(): void 34 | { 35 | $this->assertFileExists(__DIR__ . '/../src/Stubs/service.stub'); 36 | } 37 | 38 | /** 39 | * @return void 40 | */ 41 | public function test_interface_stub_exist(): void 42 | { 43 | $this->assertFileExists(__DIR__ . '/../src/Stubs/interface.stub'); 44 | } 45 | 46 | /** 47 | * @return void 48 | */ 49 | public function test_service_interface_stub_exist(): void 50 | { 51 | $this->assertFileExists(__DIR__ . '/../src/Stubs/service.interface.stub'); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /tests/TraitTests.php: -------------------------------------------------------------------------------- 1 | app = require __DIR__ . '/../../bootstrap/app.php'; 20 | } 21 | 22 | /** 23 | * @return void 24 | */ 25 | public function test_make_trait_command(): void 26 | { 27 | $this->artisan('make:trait TestTrait')->assertSuccessful(); 28 | } 29 | 30 | /** 31 | * @return void 32 | */ 33 | public function test_trait_stub_exist(): void 34 | { 35 | $this->assertFileExists(__DIR__ . '/../src/Stubs/trait.stub'); 36 | } 37 | 38 | } --------------------------------------------------------------------------------