Skip to content

Commit

Permalink
增加并完善模型生成表事件
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Mar 20, 2024
1 parent 061bfb2 commit 5b0d4b4
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 32 deletions.
33 changes: 27 additions & 6 deletions doc/dev/generate/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
```
22 changes: 0 additions & 22 deletions src/Components/model/src/Cli/Table/Event/GenerateModelEvents.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Imi\Model\Cli\Table\Event\Param;

use Imi\Event\CommonEvent;

class AfterGenerateTable extends CommonEvent
{
use TGenerateTable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Imi\Model\Cli\Table\Event\Param;

use Imi\Event\CommonEvent;

class AfterGenerateTables extends CommonEvent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Imi\Model\Cli\Table\Event\Param;

use Imi\Event\CommonEvent;

class BeforeGenerateTable extends CommonEvent
{
use TGenerateTable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Imi\Model\Cli\Table\Event\Param;

use Imi\Event\CommonEvent;

class BeforeGenerateTables extends CommonEvent
{
}
32 changes: 32 additions & 0 deletions src/Components/model/src/Cli/Table/Event/Param/TGenerateTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Imi\Model\Cli\Table\Event\Param;

trait TGenerateTable
{
public function __construct(

Check warning on line 9 in src/Components/model/src/Cli/Table/Event/Param/TGenerateTable.php

View check run for this annotation

Codecov / codecov/patch

src/Components/model/src/Cli/Table/Event/Param/TGenerateTable.php#L9

Added line #L9 was not covered by tests
/**
* 模型类名.
*/
public string $className,

/**
* 表名.
*/
public string $tableName,

/**
* 是否跳过.
*/
public bool $skip,

/**
* DDL 语句.
*/
public string $ddl,
) {
parent::__construct(static::class);

Check warning on line 30 in src/Components/model/src/Cli/Table/Event/Param/TGenerateTable.php

View check run for this annotation

Codecov / codecov/patch

src/Components/model/src/Cli/Table/Event/Param/TGenerateTable.php#L30

Added line #L30 was not covered by tests
}
}
17 changes: 13 additions & 4 deletions src/Components/model/src/Cli/Table/TableGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
use Imi\Db\Db;
use Imi\Event\Event;
use Imi\Model\Annotation\DDL;
use Imi\Model\Cli\Table\Event\GenerateModelEvents;
use Imi\Model\Cli\Table\Event\Param\AfterGenerateTable;
use Imi\Model\Cli\Table\Event\Param\AfterGenerateTables;
use Imi\Model\Cli\Table\Event\Param\BeforeGenerateTable;
use Imi\Model\Cli\Table\Event\Param\BeforeGenerateTables;
use Imi\Util\ClassObject;

#[Command(name: 'generate')]
Expand All @@ -31,7 +34,7 @@ class TableGenerate extends BaseCommand
#[Option(name: 'override', type: \Imi\Cli\ArgType::STRING, default: false, comments: '是否覆盖已存在的表,请慎重!true-全覆盖;false-不覆盖;默认缺省状态为false')]
public function generate(?string $namespace, ?string $database, ?string $poolName, array $include, array $exclude, string|bool $override): void
{
Event::dispatch(eventName: GenerateModelEvents::BEFORE_GENERATE_MODEL);
Event::dispatch(eventName: BeforeGenerateTables::class);

Check warning on line 37 in src/Components/model/src/Cli/Table/TableGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Components/model/src/Cli/Table/TableGenerate.php#L37

Added line #L37 was not covered by tests
$override = (bool) json_decode((string) $override, false);
// 数据库
if (null === $database)
Expand Down Expand Up @@ -85,17 +88,23 @@ public function generate(?string $namespace, ?string $database, ?string $poolNam
$tables[] = $table;
// 表存在跳过
$this->output->writeln('Skip ' . $table);
Event::dispatch(new BeforeGenerateTable($class, $table, true, ''));
Event::dispatch(new AfterGenerateTable($class, $table, true, ''));

Check warning on line 92 in src/Components/model/src/Cli/Table/TableGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Components/model/src/Cli/Table/TableGenerate.php#L91-L92

Added lines #L91 - L92 were not covered by tests
continue;
}
}
/** @var \Imi\Model\Annotation\DDL $ddlAnnotation */
$ddlAnnotation = $point->getAnnotation();
$ddl = $ddlAnnotation->getRawSql() . ';';
$event = new BeforeGenerateTable($class, $table, false, $ddl);
Event::dispatch($event);

Check warning on line 100 in src/Components/model/src/Cli/Table/TableGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Components/model/src/Cli/Table/TableGenerate.php#L98-L100

Added lines #L98 - L100 were not covered by tests
// 创建表
Db::getInstance()->batchExec($ddlAnnotation->getRawSql() . ';');
Db::getInstance()->batchExec($event->ddl);
Event::dispatch(new AfterGenerateTable($class, $table, false, $ddl));

Check warning on line 103 in src/Components/model/src/Cli/Table/TableGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Components/model/src/Cli/Table/TableGenerate.php#L102-L103

Added lines #L102 - L103 were not covered by tests
$tables[] = $table;
$this->output->writeln('Create <info>' . $table . '</info>');
}
Event::dispatch(eventName: GenerateModelEvents::AFTER_GENERATE_MODEL);
Event::dispatch(eventName: AfterGenerateTables::class);

Check warning on line 107 in src/Components/model/src/Cli/Table/TableGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Components/model/src/Cli/Table/TableGenerate.php#L107

Added line #L107 was not covered by tests
}

/**
Expand Down

0 comments on commit 5b0d4b4

Please sign in to comment.