diff --git a/src/Commands/ObserverMakeCommand.php b/src/Commands/ObserverMakeCommand.php index c9289a9a4..68f8196a0 100644 --- a/src/Commands/ObserverMakeCommand.php +++ b/src/Commands/ObserverMakeCommand.php @@ -7,7 +7,6 @@ use Nwidart\Modules\Support\Stub; use Nwidart\Modules\Traits\ModuleCommandTrait; use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; class ObserverMakeCommand extends GeneratorCommand { @@ -55,19 +54,53 @@ protected function getTemplateContents() { $module = $this->laravel['modules']->findOrFail($this->getModuleName()); return (new Stub('/observer.stub', [ - 'NAMESPACE' => $this->getClassNamespace($module).'\Observers', - 'CLASS' => $this->getClass(), -// 'NAMESPACEMODEL' => $this->getClass() - ]))->render(); + 'NAMESPACE' => $this->getClassNamespace($module) . '\Observers', + 'NAME' => $this->getModelName(), + 'MODEL_NAMESPACE' => $this->getModelNamespace(), + 'NAME_VARIABLE' => $this->getModelVariable(), + ]))->render(); } + + /** + * Get model namespace. + * + * @return string + */ + public function getModelNamespace(): string + { + $path = $this->laravel['modules']->config('paths.generator.model.path', 'Entities'); + + $path = str_replace('/', '\\', $path); + + return $this->laravel['modules']->config('namespace') . '\\' . $this->laravel['modules']->findOrFail($this->getModuleName()) . '\\' . $path; + } + + /** + * @return mixed|string + */ + private function getModelName() + { + return Str::studly($this->argument('name')); + } + + /** + * @return mixed|string + */ + private function getModelVariable() : string + { + return '$'.Str::lower($this->argument('name')); + } + /** * @return mixed */ protected function getDestinationFilePath() { $path = $this->laravel['modules']->getModulePath($this->getModuleName()); + $observerPath = GenerateConfigReader::read('observer'); - return $path . $observerPath->getPath() . '/' . $this->getFileName() . '.php'; + + return $path . $observerPath->getPath() . '/' . $this->getFileName(); } /** @@ -75,7 +108,7 @@ protected function getDestinationFilePath() */ private function getFileName() { - return Str::studly($this->argument('name')); + return Str::studly($this->argument('name')) . 'Observer.php'; } @@ -87,5 +120,4 @@ public function handle(): int return 0; } - } diff --git a/src/Commands/stubs/observer.stub b/src/Commands/stubs/observer.stub index c0aa0b931..ca498635f 100644 --- a/src/Commands/stubs/observer.stub +++ b/src/Commands/stubs/observer.stub @@ -2,7 +2,47 @@ namespace $NAMESPACE$; -class $CLASS$ +use $MODEL_NAMESPACE$\$NAME$; + +class $NAME$Observer { + /** + * Handle the $NAME$ "created" event. + */ + public function created($NAME$ $NAME_VARIABLE$): void + { + // + } + + /** + * Handle the $NAME$ "updated" event. + */ + public function updated($NAME$ $NAME_VARIABLE$): void + { + // + } + + /** + * Handle the $NAME$ "deleted" event. + */ + public function deleted($NAME$ $NAME_VARIABLE$): void + { + // + } + + /** + * Handle the $NAME$ "restored" event. + */ + public function restored($NAME$ $NAME_VARIABLE$): void + { + // + } + /** + * Handle the $NAME$ "force deleted" event. + */ + public function forceDeleted($NAME$ $NAME_VARIABLE$): void + { + // + } } diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 465bd846c..58f075e9f 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -61,6 +61,7 @@ protected function getEnvironmentSetUp($app) 'migration' => ['path' => 'Database/Migrations', 'generate' => true], 'factory' => ['path' => 'Database/factories', 'generate' => true], 'model' => ['path' => 'Entities', 'generate' => true], + 'observer' => ['path' => 'Observers', 'generate' => true], 'repository' => ['path' => 'Repositories', 'generate' => true], 'seeder' => ['path' => 'Database/Seeders', 'generate' => true], 'controller' => ['path' => 'Http/Controllers', 'generate' => true], diff --git a/tests/Commands/ObserverMakeCommandTest.php b/tests/Commands/ObserverMakeCommandTest.php new file mode 100644 index 000000000..ab0a78f67 --- /dev/null +++ b/tests/Commands/ObserverMakeCommandTest.php @@ -0,0 +1,47 @@ +modulePath = base_path('modules/Blog'); + $this->finder = $this->app['files']; + $this->artisan('module:make', ['name' => ['Blog']]); + } + + public function tearDown(): void + { + $this->app[RepositoryInterface::class]->delete('Blog'); + parent::tearDown(); + } + + /** @test */ + public function it_makes_observer() + { + $code = $this->artisan('module:make-observer', ['name' => 'Post', 'module' => 'Blog']); + + $observerFile = $this->modulePath . '/Observers/PostObserver.php'; + // dd($observerFile); + + $this->assertTrue(is_file($observerFile), 'Observer file was not created.'); + $this->assertMatchesSnapshot($this->finder->get($observerFile)); + $this->assertSame(0, $code); + } +} diff --git a/tests/Commands/__snapshots__/ObserverMakeCommandTest__it_makes_observer__1.php b/tests/Commands/__snapshots__/ObserverMakeCommandTest__it_makes_observer__1.php new file mode 100644 index 000000000..8d0d30ea3 --- /dev/null +++ b/tests/Commands/__snapshots__/ObserverMakeCommandTest__it_makes_observer__1.php @@ -0,0 +1,50 @@ +