From e1d0e06f330abb6e373cf010177112b1c2f9abf4 Mon Sep 17 00:00:00 2001 From: Yurun Date: Thu, 21 Mar 2024 08:25:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B9=B6=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=94=9F=E6=88=90=E8=A1=A8=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=20(#684)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/dev/generate/table.md | 33 +++++++++++++++---- .../Cli/Table/Event/GenerateModelEvents.php | 22 ------------- .../Table/Event/Param/AfterGenerateTable.php | 12 +++++++ .../Table/Event/Param/AfterGenerateTables.php | 11 +++++++ .../Table/Event/Param/BeforeGenerateTable.php | 12 +++++++ .../Event/Param/BeforeGenerateTables.php | 11 +++++++ .../Cli/Table/Event/Param/TGenerateTable.php | 32 ++++++++++++++++++ .../model/src/Cli/Table/TableGenerate.php | 17 +++++++--- 8 files changed, 118 insertions(+), 32 deletions(-) delete mode 100644 src/Components/model/src/Cli/Table/Event/GenerateModelEvents.php create mode 100644 src/Components/model/src/Cli/Table/Event/Param/AfterGenerateTable.php create mode 100644 src/Components/model/src/Cli/Table/Event/Param/AfterGenerateTables.php create mode 100644 src/Components/model/src/Cli/Table/Event/Param/BeforeGenerateTable.php create mode 100644 src/Components/model/src/Cli/Table/Event/Param/BeforeGenerateTables.php create mode 100644 src/Components/model/src/Cli/Table/Event/Param/TGenerateTable.php diff --git a/doc/dev/generate/table.md b/doc/dev/generate/table.md index 14869e7aa..29774f9a4 100644 --- a/doc/dev/generate/table.md +++ b/doc/dev/generate/table.md @@ -28,14 +28,35 @@ vendor/bin/imi-xxx generate/table ## 事件监听 -### imi.generate_model.before +在生成表时,会触发以下事件: -生成表工具的前置操作 +- `Imi\Model\Cli\Table\Event\Param\BeforeGenerateTables` - 生成所有表前置事件 +- `Imi\Model\Cli\Table\Event\Param\AfterGenerateTables` - 生成所有表前置事件 +- `Imi\Model\Cli\Table\Event\Param\BeforeGenerateTable` - 生成表前置事件 +- `Imi\Model\Cli\Table\Event\Param\AfterGenerateTable` - 生成表前置事件 -**常量:** `Imi\Model\Cli\Table\Event\GenerateModelEvents::BEFORE_GENERATE_MODEL` +> 事件参数类都是事件名本身 -### imi.generate_model.after +`BeforeGenerateTable`、`AfterGenerateTable` 事件参数: -生成表工具的后置操作 +```php +/** + * 模型类名. + */ +public string $className, -**常量:** `Imi\Model\Cli\Table\Event\GenerateModelEvents::AFTER_GENERATE_MODEL` +/** + * 表名. + */ +public string $tableName, + +/** + * 是否跳过. + */ +public bool $skip, + +/** + * DDL 语句. + */ +public string $ddl, +``` diff --git a/src/Components/model/src/Cli/Table/Event/GenerateModelEvents.php b/src/Components/model/src/Cli/Table/Event/GenerateModelEvents.php deleted file mode 100644 index a1f61c345..000000000 --- a/src/Components/model/src/Cli/Table/Event/GenerateModelEvents.php +++ /dev/null @@ -1,22 +0,0 @@ -output->writeln('Skip ' . $table); + Event::dispatch(new BeforeGenerateTable($class, $table, true, '')); + Event::dispatch(new AfterGenerateTable($class, $table, true, '')); continue; } } /** @var \Imi\Model\Annotation\DDL $ddlAnnotation */ $ddlAnnotation = $point->getAnnotation(); + $ddl = $ddlAnnotation->getRawSql() . ';'; + $event = new BeforeGenerateTable($class, $table, false, $ddl); + Event::dispatch($event); // 创建表 - Db::getInstance()->batchExec($ddlAnnotation->getRawSql() . ';'); + Db::getInstance()->batchExec($event->ddl); + Event::dispatch(new AfterGenerateTable($class, $table, false, $ddl)); $tables[] = $table; $this->output->writeln('Create ' . $table . ''); } - Event::dispatch(eventName: GenerateModelEvents::AFTER_GENERATE_MODEL); + Event::dispatch(eventName: AfterGenerateTables::class); } /**