-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1
- Loading branch information
Showing
11 changed files
with
230 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea/ | ||
.idea/ | ||
.DS_Store | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,37 @@ | ||
{ | ||
"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": { | ||
"php": "^7.1|^8.0", | ||
"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": [ | ||
{ | ||
"name": "Mingeun Kim", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Getsolaris\\LaravelMakeService\\LaravelMakeServiceProvider" | ||
] | ||
} | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"getsolaris\\LaravelCreateService\\": "src/" | ||
} | ||
"Getsolaris\\LaravelMakeService\\": "src/" | ||
}, | ||
"classmap": ["src/"] | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Getsolaris\LaravelMakeService; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class LaravelMakeServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register(): void | ||
{ | ||
$this->commands(MakeService::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
namespace Getsolaris\LaravelMakeService; | ||
|
||
use Illuminate\Console\GeneratorCommand; | ||
|
||
/** | ||
* Class MakeService | ||
* @package Getsolaris\LaravelMakeService | ||
* @author getsolaris (https://github.com/getsolaris) | ||
*/ | ||
class MakeService extends GeneratorCommand | ||
{ | ||
/** | ||
* STUB_PATH | ||
*/ | ||
const STUB_PATH = __DIR__ . ' /Stubs/'; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'make:service {name : Create a service class} {--c : Create a service contract}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create a new service class and contract'; | ||
|
||
/** | ||
* The type of class being generated. | ||
* | ||
* @var string | ||
*/ | ||
protected $type = 'Service'; | ||
|
||
protected function getStub() { } | ||
|
||
/** | ||
* @param bool $isContract | ||
* @return string | ||
*/ | ||
protected function getServiceStub(bool $isContract): string | ||
{ | ||
return self::STUB_PATH . | ||
$isContract ? 'service.origin.stub' : 'service.stub'; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getServiceContractStub(): string | ||
{ | ||
return self::STUB_PATH . 'service.contract.stub'; | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return bool|null | ||
* | ||
* @see \Illuminate\Console\GeneratorCommand | ||
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException | ||
*/ | ||
public function handle() | ||
{ | ||
if ($this->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'; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Services\Contracts; | ||
|
||
/** | ||
* Interface {{ class }}Contract | ||
* @package App\Services\Contracts | ||
*/ | ||
interface {{ class }}Contract | ||
{ | ||
|
||
} |
Oops, something went wrong.