diff --git a/src/Commands/ControllerMakeCommand.php b/src/Commands/ControllerMakeCommand.php index 87a958c..cce9872 100644 --- a/src/Commands/ControllerMakeCommand.php +++ b/src/Commands/ControllerMakeCommand.php @@ -60,6 +60,7 @@ public function handle() parent::handle(); $this->createSwagger(); $this->createTest(); + $this->createUsecase(); } /** * Create a swagger file controller. @@ -83,11 +84,17 @@ protected function createSwagger() */ protected function createTest() { - $className = class_basename($this->argument('name')); + $dirPath = "Controllers"; + $temp = explode("/", $this->argument('name')); + if (count($temp) > 1) { + array_pop($temp); + $dirPath .= "/" . implode("/", $temp); + } + $className = class_basename($this->argument('name')) . 'Controller'; $this->call( 'make:test', [ - 'name' => $className + 'name' => $dirPath . "/" . $className ] ); } @@ -113,10 +120,27 @@ protected function getPath($name) protected function replaceClass($stub, $name) { $stub = parent::replaceClass($stub, $name); - $stub = str_replace('DummyUsecase', Str::ucfirst($this->argument('name')) . 'Usecase', $stub); - $stub = str_replace('dummyUsecase', Str::lower($this->argument('name')) . 'Usecase', $stub); + $temp = explode("/", $this->argument('name')); + $usecaseClass = Str::ucfirst($temp[count($temp) - 1]) . 'Usecase'; + $stub = str_replace('DummyUsecase', $usecaseClass, $stub); + $stub = str_replace('dummyUsecase', Str::lower($usecaseClass), $stub); return $stub; } + /** + * Create a usecase file. + * + * @return void + */ + protected function createUsecase() + { + $className = class_basename($this->argument('name')); + $this->call( + 'make:usecase', + [ + 'name' => $className + ] + ); + } /** * Get the stub file for the generator. * diff --git a/src/Commands/ServiceMakeCommand.php b/src/Commands/ServiceMakeCommand.php new file mode 100644 index 0000000..4d91b12 --- /dev/null +++ b/src/Commands/ServiceMakeCommand.php @@ -0,0 +1,97 @@ + + * @license https://mit-license.org/ MIT License + * @version GIT: 0.0.9 + * @link https://github.com/spotlibs + */ + +declare(strict_types=1); + +namespace Spotlibs\PhpLib\Commands; + +use Illuminate\Console\GeneratorCommand; +use Symfony\Component\Console\Input\InputOption; + +/** + * ServiceMakeCommand + * + * Custom command + * + * @category Console + * @package Commands + * @author Made Mas Adi Winata + * @license https://mit-license.org/ MIT License + * @link https://github.com/ + */ +class ServiceMakeCommand extends GeneratorCommand +{ + /** + * The console command name. + * + * @var string + */ + protected $name = 'make:service'; + /** + * The console command description. + * + * @var string + */ + protected $description = 'Create a new service for model class'; + /** + * The type of class being generated. + * + * @var string + */ + protected $type = 'Service'; + /** + * Get the destination class path. + * + * @param string $name name of the type + * + * @return string + */ + protected function getPath($name) + { + return parent::getPath($name . 'Service'); + } + /** + * Get the stub file for the generator. + * + * @return string + */ + protected function getStub() + { + if ($this->option('resource')) { + return __DIR__ . '/stubs/service.stub'; + } + return __DIR__ . '/stubs/service.plain.stub'; + } + /** + * Get the default namespace for the class. + * + * @param string $rootNamespace root namespace (generally App) + * + * @return string + */ + protected function getDefaultNamespace($rootNamespace) + { + return $rootNamespace . '\Services'; + } + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return [ + ['resource', null, InputOption::VALUE_NONE, 'Generate a resource service class.'], + ]; + } +} diff --git a/src/Commands/TestMakeCommand.php b/src/Commands/TestMakeCommand.php index 49cdc00..bff2d07 100644 --- a/src/Commands/TestMakeCommand.php +++ b/src/Commands/TestMakeCommand.php @@ -60,7 +60,10 @@ class TestMakeCommand extends GeneratorCommand protected function getPath($name) { $name = Str::replaceFirst($this->laravel->getNamespace(), '', $name); - return $this->laravel->basePath() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $name . 'Test') . '.php'; + $temp = explode("\\", $name); + $name = implode(DIRECTORY_SEPARATOR, $temp); + $testPath = $this->laravel->basePath() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . $name . 'Test.php'; + return $testPath; } /** * Get the stub file for the generator. diff --git a/src/Commands/UsecaseMakeCommand.php b/src/Commands/UsecaseMakeCommand.php index 1d87703..45b1bc7 100644 --- a/src/Commands/UsecaseMakeCommand.php +++ b/src/Commands/UsecaseMakeCommand.php @@ -49,6 +49,16 @@ class UsecaseMakeCommand extends GeneratorCommand * @var string */ protected $type = 'Usecase'; + /** + * Execute the console command. + * + * @return void + */ + public function handle() + { + parent::handle(); + $this->createTest(); + } /** * Get the destination class path. * @@ -60,6 +70,22 @@ protected function getPath($name) { return parent::getPath($name . 'Usecase'); } + /** + * Create a unit test file. + * + * @return void + */ + protected function createTest() + { + $dirPath = "Usecases"; + $className = class_basename($this->argument('name')) . 'Usecase'; + $this->call( + 'make:test', + [ + 'name' => $dirPath . "/" . $className + ] + ); + } /** * Get the stub file for the generator. * diff --git a/src/Commands/stubs/service.plain.stub b/src/Commands/stubs/service.plain.stub new file mode 100644 index 0000000..8d55654 --- /dev/null +++ b/src/Commands/stubs/service.plain.stub @@ -0,0 +1,40 @@ +rest_client = $restClient; + } +}