From acec563064d2b98e6b18e0aa1d2823a8ea1850aa Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Sat, 17 May 2025 16:26:39 +0300 Subject: [PATCH 1/2] refactor: remove useless --- composer.json | 5 +- src/Commands/MakeEntityCommand.php | 81 +++++++----------------------- src/helpers.php | 11 ---- 3 files changed, 19 insertions(+), 78 deletions(-) delete mode 100644 src/helpers.php diff --git a/composer.json b/composer.json index 1c8cb636..88e89949 100644 --- a/composer.json +++ b/composer.json @@ -29,10 +29,7 @@ "autoload": { "psr-4": { "RonasIT\\Support\\": "src/" - }, - "files": [ - "src/helpers.php" - ] + } }, "autoload-dev": { "psr-4": { diff --git a/src/Commands/MakeEntityCommand.php b/src/Commands/MakeEntityCommand.php index 6ec5f173..98739a02 100644 --- a/src/Commands/MakeEntityCommand.php +++ b/src/Commands/MakeEntityCommand.php @@ -9,7 +9,7 @@ use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Events\WarningEvent; use RonasIT\Support\Exceptions\ClassNotExistsException; -use RonasIT\Support\Exceptions\EntityCreateException; +use Exception; use RonasIT\Support\Generators\ControllerGenerator; use RonasIT\Support\Generators\EntityGenerator; use RonasIT\Support\Generators\FactoryGenerator; @@ -27,22 +27,6 @@ use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; use UnexpectedValueException; -/** - * @property ControllerGenerator $controllerGenerator - * @property MigrationGenerator $migrationGenerator - * @property ModelGenerator $modelGenerator - * @property RepositoryGenerator $repositoryGenerator - * @property RequestsGenerator $requestsGenerator - * @property ServiceGenerator $serviceGenerator - * @property FactoryGenerator $factoryGenerator - * @property TestsGenerator $testGenerator - * @property TranslationsGenerator $translationsGenerator - * @property SeederGenerator $seederGenerator - * @property ResourceGenerator $resourceGenerator - * @property NovaResourceGenerator $novaResourceGenerator - * @property NovaTestGenerator $novaTestGenerator - * @property EventDispatcher $eventDispatcher - */ class MakeEntityCommand extends Command { const CRUD_OPTIONS = [ @@ -97,21 +81,6 @@ class MakeEntityCommand extends Command */ protected $description = 'Make entity with Model, Repository, Service, Migration, Controller, Resource and Nova Resource.'; - protected $controllerGenerator; - protected $migrationGenerator; - protected $modelGenerator; - protected $repositoryGenerator; - protected $requestsGenerator; - protected $serviceGenerator; - protected $factoryGenerator; - protected $testGenerator; - protected $translationsGenerator; - protected $seederGenerator; - protected $resourceGenerator; - protected $novaResourceGenerator; - protected $novaTestGenerator; - protected $eventDispatcher; - protected $rules = [ 'only' => [ 'only-api' => [ResourceGenerator::class, ControllerGenerator::class, RequestsGenerator::class, TestsGenerator::class], @@ -138,26 +107,6 @@ class MakeEntityCommand extends Command NovaTestGenerator::class ]; - public function __construct() - { - parent::__construct(); - - $this->controllerGenerator = app(ControllerGenerator::class); - $this->migrationGenerator = app(MigrationGenerator::class); - $this->modelGenerator = app(ModelGenerator::class); - $this->repositoryGenerator = app(RepositoryGenerator::class); - $this->requestsGenerator = app(RequestsGenerator::class); - $this->serviceGenerator = app(ServiceGenerator::class); - $this->factoryGenerator = app(FactoryGenerator::class); - $this->testGenerator = app(TestsGenerator::class); - $this->translationsGenerator = app(TranslationsGenerator::class); - $this->seederGenerator = app(SeederGenerator::class); - $this->resourceGenerator = app(ResourceGenerator::class); - $this->novaResourceGenerator = app(NovaResourceGenerator::class); - $this->novaTestGenerator = app(NovaTestGenerator::class); - $this->eventDispatcher = app(EventDispatcher::class); - } - /** * Execute the console command. * @@ -167,20 +116,11 @@ public function handle(): void { $this->validateInput(); $this->checkConfigs(); - - $this->eventDispatcher->listen( - events: SuccessCreateMessage::class, - listener: fn (SuccessCreateMessage $event) => $this->info($event->message), - ); - - $this->eventDispatcher->listen( - events: WarningEvent::class, - listener: fn (WarningEvent $event) => $this->warn($event->message), - ); + $this->listenEvents(); try { $this->generate(); - } catch (EntityCreateException $e) { + } catch (Exception $e) { $this->error($e->getMessage()); } } @@ -320,4 +260,19 @@ protected function validateOnlyApiOption() } } } + + protected function listenEvents(): void + { + $eventDispatcher = app(EventDispatcher::class); + + $eventDispatcher->listen( + events: SuccessCreateMessage::class, + listener: fn (SuccessCreateMessage $event) => $this->info($event->message), + ); + + $eventDispatcher->listen( + events: WarningEvent::class, + listener: fn (WarningEvent $event) => $this->warn($event->message), + ); + } } diff --git a/src/helpers.php b/src/helpers.php deleted file mode 100644 index 4ab804d1..00000000 --- a/src/helpers.php +++ /dev/null @@ -1,11 +0,0 @@ - Date: Tue, 20 May 2025 14:12:20 +0300 Subject: [PATCH 2/2] refactor: use Event facade --- src/Commands/MakeEntityCommand.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Commands/MakeEntityCommand.php b/src/Commands/MakeEntityCommand.php index 98739a02..60c53797 100644 --- a/src/Commands/MakeEntityCommand.php +++ b/src/Commands/MakeEntityCommand.php @@ -5,6 +5,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Str; use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Events\WarningEvent; @@ -24,7 +25,6 @@ use RonasIT\Support\Generators\TestsGenerator; use RonasIT\Support\Generators\TranslationsGenerator; use RonasIT\Support\Generators\SeederGenerator; -use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; use UnexpectedValueException; class MakeEntityCommand extends Command @@ -263,14 +263,12 @@ protected function validateOnlyApiOption() protected function listenEvents(): void { - $eventDispatcher = app(EventDispatcher::class); - - $eventDispatcher->listen( + Event::listen( events: SuccessCreateMessage::class, listener: fn (SuccessCreateMessage $event) => $this->info($event->message), ); - $eventDispatcher->listen( + Event::listen( events: WarningEvent::class, listener: fn (WarningEvent $event) => $this->warn($event->message), );