From 44d94fe93dce5bf8a5f3f386b14fe0643d2ef94a Mon Sep 17 00:00:00 2001 From: Vishal InfyOm Date: Sat, 5 Jun 2021 12:46:45 +0530 Subject: [PATCH] feat: #754 events support added --- src/Commands/API/APIGeneratorCommand.php | 3 ++ src/Commands/APIScaffoldGeneratorCommand.php | 3 ++ src/Commands/RollbackGeneratorCommand.php | 8 ++++- .../Scaffold/ScaffoldGeneratorCommand.php | 3 ++ src/Common/CommandData.php | 32 +++++++++++++++++++ src/Events/GeneratorFileCreated.php | 28 ++++++++++++++++ src/Events/GeneratorFileCreating.php | 28 ++++++++++++++++ src/Events/GeneratorFileDeleted.php | 28 ++++++++++++++++ src/Events/GeneratorFileDeleting.php | 28 ++++++++++++++++ src/Utils/FileUtil.php | 5 +++ 10 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 src/Events/GeneratorFileCreated.php create mode 100644 src/Events/GeneratorFileCreating.php create mode 100644 src/Events/GeneratorFileDeleted.php create mode 100644 src/Events/GeneratorFileDeleting.php diff --git a/src/Commands/API/APIGeneratorCommand.php b/src/Commands/API/APIGeneratorCommand.php index 2ab7f79d4..bc1adc75c 100755 --- a/src/Commands/API/APIGeneratorCommand.php +++ b/src/Commands/API/APIGeneratorCommand.php @@ -4,6 +4,7 @@ use InfyOm\Generator\Commands\BaseCommand; use InfyOm\Generator\Common\CommandData; +use InfyOm\Generator\Utils\FileUtil; class APIGeneratorCommand extends BaseCommand { @@ -39,12 +40,14 @@ public function __construct() public function handle() { parent::handle(); + $this->commandData->fireEvent('api', FileUtil::FILE_CREATING); $this->generateCommonItems(); $this->generateAPIItems(); $this->performPostActionsWithMigration(); + $this->commandData->fireEvent('api', FileUtil::FILE_CREATED); } /** diff --git a/src/Commands/APIScaffoldGeneratorCommand.php b/src/Commands/APIScaffoldGeneratorCommand.php index 5a67318d3..c3e0fef28 100755 --- a/src/Commands/APIScaffoldGeneratorCommand.php +++ b/src/Commands/APIScaffoldGeneratorCommand.php @@ -3,6 +3,7 @@ namespace InfyOm\Generator\Commands; use InfyOm\Generator\Common\CommandData; +use InfyOm\Generator\Utils\FileUtil; class APIScaffoldGeneratorCommand extends BaseCommand { @@ -38,6 +39,7 @@ public function __construct() public function handle() { parent::handle(); + $this->commandData->fireEvent('api_scaffold', FileUtil::FILE_CREATING); $this->generateCommonItems(); @@ -46,6 +48,7 @@ public function handle() $this->generateScaffoldItems(); $this->performPostActionsWithMigration(); + $this->commandData->fireEvent('api_scaffold', FileUtil::FILE_CREATED); } /** diff --git a/src/Commands/RollbackGeneratorCommand.php b/src/Commands/RollbackGeneratorCommand.php index 11a462831..59bfe5140 100755 --- a/src/Commands/RollbackGeneratorCommand.php +++ b/src/Commands/RollbackGeneratorCommand.php @@ -18,6 +18,7 @@ use InfyOm\Generator\Generators\Scaffold\RequestGenerator; use InfyOm\Generator\Generators\Scaffold\RoutesGenerator; use InfyOm\Generator\Generators\Scaffold\ViewGenerator; +use InfyOm\Generator\Utils\FileUtil; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -64,7 +65,8 @@ public function __construct() */ public function handle() { - if (!in_array($this->argument('type'), [ + $type = $this->argument('type'); + if (!in_array($type, [ CommandData::$COMMAND_TYPE_API, CommandData::$COMMAND_TYPE_SCAFFOLD, CommandData::$COMMAND_TYPE_API_SCAFFOLD, @@ -73,6 +75,7 @@ public function handle() } $this->commandData = new CommandData($this, $this->argument('type')); + $this->commandData->fireEvent($type, FileUtil::FILE_DELETING); $this->commandData->config->mName = $this->commandData->modelName = $this->argument('model'); $this->commandData->config->init($this->commandData, ['tableName', 'prefix', 'plural', 'views']); @@ -85,6 +88,7 @@ public function handle() $this->info('Generating autoload files'); $this->composer->dumpOptimized(); + $this->commandData->fireEvent($type, FileUtil::FILE_DELETED); return; } @@ -137,6 +141,8 @@ public function handle() $this->info('Generating autoload files'); $this->composer->dumpOptimized(); + + $this->commandData->fireEvent($type, FileUtil::FILE_DELETED); } /** diff --git a/src/Commands/Scaffold/ScaffoldGeneratorCommand.php b/src/Commands/Scaffold/ScaffoldGeneratorCommand.php index 4d3c94cb8..9abb7b5a0 100755 --- a/src/Commands/Scaffold/ScaffoldGeneratorCommand.php +++ b/src/Commands/Scaffold/ScaffoldGeneratorCommand.php @@ -4,6 +4,7 @@ use InfyOm\Generator\Commands\BaseCommand; use InfyOm\Generator\Common\CommandData; +use InfyOm\Generator\Utils\FileUtil; class ScaffoldGeneratorCommand extends BaseCommand { @@ -41,11 +42,13 @@ public function handle() parent::handle(); if ($this->checkIsThereAnyDataToGenerate()) { + $this->commandData->fireEvent('scaffold', FileUtil::FILE_CREATING); $this->generateCommonItems(); $this->generateScaffoldItems(); $this->performPostActionsWithMigration(); + $this->commandData->fireEvent('scaffold', FileUtil::FILE_CREATED); } else { $this->commandData->commandInfo('There are not enough input fields for scaffold generation.'); } diff --git a/src/Common/CommandData.php b/src/Common/CommandData.php index c6cc9d4ed..f0292e0bf 100755 --- a/src/Common/CommandData.php +++ b/src/Common/CommandData.php @@ -5,6 +5,11 @@ use Exception; use Illuminate\Console\Command; use Illuminate\Support\Str; +use InfyOm\Generator\Events\GeneratorFileCreated; +use InfyOm\Generator\Events\GeneratorFileCreating; +use InfyOm\Generator\Events\GeneratorFileDeleted; +use InfyOm\Generator\Events\GeneratorFileDeleting; +use InfyOm\Generator\Utils\FileUtil; use InfyOm\Generator\Utils\GeneratorFieldsInputUtil; use InfyOm\Generator\Utils\TableFieldsGenerator; @@ -307,4 +312,31 @@ private function getInputFromTable() $this->fields = $tableFieldsGenerator->fields; $this->relations = $tableFieldsGenerator->relations; } + + public function prepareEventsData() + { + $data['modelName'] = $this->modelName; + $data['tableName'] = $this->config->tableName; + $data['nsModel'] = $this->config->nsModel; + + return $data; + } + + public function fireEvent($commandType, $eventType) + { + switch ($eventType) { + case FileUtil::FILE_CREATING: + event(new GeneratorFileCreating($commandType, $this->prepareEventsData())); + break; + case FileUtil::FILE_CREATED: + event(new GeneratorFileCreated($commandType, $this->prepareEventsData())); + break; + case FileUtil::FILE_DELETING: + event(new GeneratorFileDeleting($commandType, $this->prepareEventsData())); + break; + case FileUtil::FILE_DELETED: + event(new GeneratorFileDeleted($commandType, $this->prepareEventsData())); + break; + } + } } diff --git a/src/Events/GeneratorFileCreated.php b/src/Events/GeneratorFileCreated.php new file mode 100644 index 000000000..858f713c4 --- /dev/null +++ b/src/Events/GeneratorFileCreated.php @@ -0,0 +1,28 @@ +type = $type; + $this->data = $data; + } +} diff --git a/src/Events/GeneratorFileCreating.php b/src/Events/GeneratorFileCreating.php new file mode 100644 index 000000000..7d719757a --- /dev/null +++ b/src/Events/GeneratorFileCreating.php @@ -0,0 +1,28 @@ +type = $type; + $this->data = $data; + } +} diff --git a/src/Events/GeneratorFileDeleted.php b/src/Events/GeneratorFileDeleted.php new file mode 100644 index 000000000..ece701aea --- /dev/null +++ b/src/Events/GeneratorFileDeleted.php @@ -0,0 +1,28 @@ +type = $type; + $this->data = $data; + } +} diff --git a/src/Events/GeneratorFileDeleting.php b/src/Events/GeneratorFileDeleting.php new file mode 100644 index 000000000..ebe16ffed --- /dev/null +++ b/src/Events/GeneratorFileDeleting.php @@ -0,0 +1,28 @@ +type = $type; + $this->data = $data; + } +} diff --git a/src/Utils/FileUtil.php b/src/Utils/FileUtil.php index 5bb2db938..49cfdcdbc 100755 --- a/src/Utils/FileUtil.php +++ b/src/Utils/FileUtil.php @@ -4,6 +4,11 @@ class FileUtil { + const FILE_CREATING = 1; + const FILE_CREATED = 2; + const FILE_DELETING = 3; + const FILE_DELETED = 4; + public static function createFile($path, $fileName, $contents) { if (!file_exists($path)) {