├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── src ├── LaravelMakeServiceProvider.php ├── MakeService.php └── Stubs │ ├── interface.stub │ ├── service.interface.stub │ └── service.stub └── tests ├── GenerateTest.php └── StubExistTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .DS_Store 3 | composer.lock 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 getsolaris (https://github.com/getsolaris) 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | [](https://packagist.org/packages/getsolaris/laravel-make-service) [](https://packagist.org/packages/getsolaris/laravel-make-service) 4 | [](https://packagist.org/packages/getsolaris/laravel-make-service) 5 | [](https://packagist.org/packages/getsolaris/laravel-make-service) 6 | [](https://packagist.org/packages/getsolaris/laravel-make-service) 7 | 8 |
9 | 10 | # A MVCS pattern create a service command for Laravel 5+ 11 | Create a new service class and service interface 12 | 13 | # Install 14 | ```bash 15 | composer require getsolaris/laravel-make-service --dev 16 | ``` 17 | 18 | # Suggest 19 | getsolaris.kr@gmail.com 20 | 21 | 22 | # Usage 23 | ```bash 24 | $ php artisan make:service {name : Create a service class} {--i : Optional of create a service interface} 25 | ``` 26 | 27 | # Example 28 | 29 | ## Create a service class 30 | ```bash 31 | $ php artisan make:service UserService 32 | ``` 33 | 34 | ```php 35 | contract 54 | v1.1.x -> interface 55 | ``` 56 | 57 | ```bash 58 | $ php artisan make:service UserService --i 59 | ``` 60 | 61 | ```php 62 | commands(MakeService::class); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/MakeService.php: -------------------------------------------------------------------------------- 1 | isReservedName($this->getNameInput())) { 77 | $this->error('The name "' . $this->getNameInput() . '" is reserved by PHP.'); 78 | 79 | return false; 80 | } 81 | 82 | $name = $this->qualifyClass($this->getNameInput()); 83 | 84 | $path = $this->getPath($name); 85 | 86 | if ((! $this->hasOption('force') || 87 | ! $this->option('force')) && 88 | $this->alreadyExists($this->getNameInput())) { 89 | $this->error($this->type . ' already exists!'); 90 | 91 | return false; 92 | } 93 | 94 | $this->makeDirectory($path); 95 | $isInterface = $this->option('i'); 96 | 97 | $this->files->put( 98 | $path, 99 | $this->sortImports( 100 | $this->buildServiceClass($name, $isInterface) 101 | ) 102 | ); 103 | $message = $this->type; 104 | 105 | // Whether to create contract 106 | if ($isInterface) { 107 | $interfaceName = $this->getNameInput() . 'Interface.php'; 108 | $interfacePath = str_replace($this->getNameInput() . '.php', 'Interfaces/', $path); 109 | 110 | $this->makeDirectory($interfacePath . $interfaceName); 111 | 112 | $this->files->put( 113 | $interfacePath . $interfaceName, 114 | $this->sortImports( 115 | $this->buildServiceInterface($this->getNameInput()) 116 | ) 117 | ); 118 | 119 | $message .= ' and Interface'; 120 | } 121 | 122 | $this->info($message . ' created successfully.'); 123 | } 124 | 125 | /** 126 | * Build the class with the given name. 127 | * 128 | * @param string $name 129 | * @param $isInterface 130 | * @return string 131 | * 132 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 133 | */ 134 | protected function buildServiceClass(string $name, $isInterface): string 135 | { 136 | $stub = $this->files->get( 137 | $isInterface ? $this->getServiceInterfaceStub() : $this->getServiceStub() 138 | ); 139 | 140 | return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); 141 | } 142 | 143 | /** 144 | * Build the class with the given name. 145 | * 146 | * @param string $name 147 | * @return string 148 | * 149 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 150 | */ 151 | protected function buildServiceInterface(string $name): string 152 | { 153 | $stub = $this->files->get($this->getInterfaceStub()); 154 | 155 | return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); 156 | } 157 | 158 | /** 159 | * @param $rootNamespace 160 | * @return string 161 | */ 162 | protected function getDefaultNamespace($rootNamespace): string 163 | { 164 | return $rootNamespace . '\Services'; 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /src/Stubs/interface.stub: -------------------------------------------------------------------------------- 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_make_service_to_interface_command(): void 34 | { 35 | $this->artisan('make:service TestService --i')->assertSuccessful(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/StubExistTest.php: -------------------------------------------------------------------------------- 1 | assertFileExists(__DIR__ . '/../src/Stubs/service.stub'); 13 | } 14 | 15 | /** 16 | * @return void 17 | */ 18 | public function test_exist_interface_stub(): void 19 | { 20 | $this->assertFileExists(__DIR__ . '/../src/Stubs/interface.stub'); 21 | } 22 | 23 | /** 24 | * @return void 25 | */ 26 | public function test_exist_service_interface_stub(): void 27 | { 28 | $this->assertFileExists(__DIR__ . '/../src/Stubs/service.interface.stub'); 29 | } 30 | } 31 | --------------------------------------------------------------------------------