diff --git a/.gitignore b/.gitignore index 62c8935..15b594e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.idea/ \ No newline at end of file +.idea/ +.DS_Store +composer.lock diff --git a/README.md b/README.md index 754e3a7..7ec1e1c 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ License

-# Introduction -Laravel Make Service Class +# A MVCS pattern create a service command for Laravel 5+ +Create a new service class and service contract # Install ```bash @@ -13,13 +13,6 @@ composer require getsolaris/laravel-make-service ``` # Usage -```php -// config/app.php -'providers' => [ - getsolaris\LaravelCreateService\CreateServiceProvider::class, -]; -``` - ```bash -php artisan make:service {name} +php artisan make:service {name : Create a service class} {--c : Create a service contract} ``` diff --git a/composer.json b/composer.json index 16c6660..39f9c59 100644 --- a/composer.json +++ b/composer.json @@ -1,10 +1,12 @@ { "name": "getsolaris/laravel-make-service", - "description": "Laravel MVCS Pattern Create a new Service class", + "description": "A MVCS pattern create a service command for Laravel 5+", "keywords": [ "laravel", "mvcs", - "service" + "service", + "contract", + "pattern" ], "type": "library", "require": { @@ -12,9 +14,6 @@ "illuminate/support": "~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0|^8.0", "illuminate/console": "~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0|^8.0" }, - "require-dev": { - "phpunit/phpunit": "^7.0|^8.0|^9.0" - }, "license": "MIT", "authors": [ { @@ -22,9 +21,17 @@ "email": "mingeun.k.k@gmail.com" } ], + "extra": { + "laravel": { + "providers": [ + "Getsolaris\\LaravelMakeService\\LaravelMakeServiceProvider" + ] + } + }, "autoload": { "psr-4": { - "getsolaris\\LaravelCreateService\\": "src/" - } + "Getsolaris\\LaravelMakeService\\": "src/" + }, + "classmap": ["src/"] } } diff --git a/src/Commands/MakeServices.php b/src/Commands/MakeServices.php deleted file mode 100644 index 14beb45..0000000 --- a/src/Commands/MakeServices.php +++ /dev/null @@ -1,82 +0,0 @@ -files = $files; - } - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $name = trim($this->argument('name')); - - if (! file_exists(app_path('Services'))) - mkdir(app_path('Services'), 0777); - - if (! $name or is_null($name) or empty($name)) { - $this->error('Not enough arguments (missing: "name").'); - return false; - } - - $this->createService($name); - } - - private function createService($name) { - $name = $this->checkServiceName($name); - - $file = $name . '.php'; - - if ($this->files->exists(app_path('Services/' . $file))) { - $this->error('Service already exists!'); - return false; - } - - $original = $this->files->get(__DIR__ . '/../Services/Service.php'); - - $original = str_replace('ServiceName', ucfirst($name), $original); - $this->files->put(app_path('Services/' . $name . '.php'), $original); - - $this->info('Service created successfully.'); - return true; - } - - private function checkServiceName($name) { - if (strpos($name, 'Service')) - return ucfirst($name); - else if (strpos($name, 'service')) - return ucfirst(str_replace('service', 'Service', $name)); - else - return ucfirst($name) . 'Service'; - } -} diff --git a/src/CreateServiceProvider.php b/src/CreateServiceProvider.php deleted file mode 100644 index 30d7e01..0000000 --- a/src/CreateServiceProvider.php +++ /dev/null @@ -1,30 +0,0 @@ -app->singleton('command.getsolaris.makeservice', function ($app) { - return $app['getsolaris\LaravelCreateService\Commands\MakeServices']; - }); - $this->commands('command.getsolaris.makeservice'); - } -} \ No newline at end of file diff --git a/src/LaravelMakeServiceProvider.php b/src/LaravelMakeServiceProvider.php new file mode 100644 index 0000000..a023916 --- /dev/null +++ b/src/LaravelMakeServiceProvider.php @@ -0,0 +1,18 @@ +commands(MakeService::class); + } +} diff --git a/src/MakeService.php b/src/MakeService.php new file mode 100644 index 0000000..ccd8943 --- /dev/null +++ b/src/MakeService.php @@ -0,0 +1,154 @@ +isReservedName($this->getNameInput())) { + $this->error('The name "'.$this->getNameInput().'" is reserved by PHP.'); + + return false; + } + + $name = $this->qualifyClass($this->getNameInput()); + + $path = $this->getPath($name); + + if ((! $this->hasOption('force') || + ! $this->option('force')) && + $this->alreadyExists($this->getNameInput())) { + $this->error($this->type.' already exists!'); + + return false; + } + + $this->makeDirectory($path); + $isContract = $this->option('c'); + + $this->files->put($path, $this->sortImports( + $this->buildServiceClass($name, $isContract) + )); + $message = $this->type; + + // Whether to create contract + if ($isContract) { + $contractName = $this->getNameInput() . 'Contract.php'; + $contractPath = str_replace($this->getNameInput() . '.php', 'Contracts/', $path); + + $this->makeDirectory($contractPath . $contractName); + + $this->files->put($contractPath . $contractName, + $this->sortImports( + $this->buildServiceContractInterface($this->getNameInput()) + ) + ); + + $message .= ' and Contract'; + } + + $this->info($message . ' created successfully.'); + } + + /** + * Build the class with the given name. + * + * @param string $name + * @param $isContract + * @return string + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + protected function buildServiceClass($name, $isContract): string + { + $stub = $this->files->get($this->getServiceStub($isContract)); + + return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); + } + + /** + * Build the class with the given name. + * + * @param string $name + * @return string + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + protected function buildServiceContractInterface($name): string + { + $stub = $this->files->get($this->getServiceContractStub()); + + return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); + } + + /** + * @param $rootNamespace + * @return string + */ + protected function getDefaultNamespace($rootNamespace): string + { + return $rootNamespace . '\Services'; + } +} diff --git a/src/Services/Service.php b/src/Services/Service.php deleted file mode 100644 index 30143a3..0000000 --- a/src/Services/Service.php +++ /dev/null @@ -1,7 +0,0 @@ -