event-engine / php-code-generator-event-engine-ast
基于 PHP 抽象语法树 (AST) 的 Event Engine PHP 代码生成器
0.1.0
2022-02-23 12:09 UTC
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- event-engine/php-inspectio-graph-cody: ^0.1.0
- open-code-modeling/json-schema-to-php: ^0.4.x-dev
- open-code-modeling/json-schema-to-php-ast: 0.6.x-dev
- open-code-modeling/php-code-ast: 0.13.x-dev
- open-code-modeling/php-filter: ^0.2.1 || 0.3.x-dev
Requires (Dev)
- laminas/laminas-filter: ^2.10
- league/flysystem: ^2.0
- league/flysystem-memory: ^2.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^0.12.33
- phpstan/phpstan-strict-rules: ^0.12.4
- phpunit/phpunit: ^9.5.0
- prooph/php-cs-fixer-config: ^0.5.0
- roave/security-advisories: dev-latest
Suggests
- laminas/laminas-filter: If you want to use PreConfiguredNaming configuration
This package is auto-updated.
Last update: 2024-09-16 11:01:09 UTC
README
基于 PHP 抽象语法树 (AST) 的 PHP 代码生成器。它提供了一个全面的、高级的 API,可以从 prooph board 生成 Event Engine 的代码。
它支持以下代码生成
- 命令、聚合和领域事件的 Event Engine API 描述
- 基于元数据(JSON 模式)的命令、聚合和领域事件类,以及相应的值对象
- 命令、对应聚合和对应领域事件之间的粘合代码
安装
运行以下命令来安装此库
$ composer require event-engine/php-code-generator-event-engine-ast
如果您想使用 Config\PreConfiguredNaming,请同时安装 laminas/laminas-filter。
$ composer require laminas/laminas-filter
用法
代码生成基于 InspectIO Graph。有两个 InspectIO Graph 的实现。第一个是基于 InspectIO GraphML 图形格式,第二个是基于 InspectIO Cody 图形格式。
建议使用 InspectIO Cody 图形格式,因为它基于简单的 JSON 结构。
对于开箱即用的使用,您可以使用预配置的配置文件 Config\PreConfiguredNaming。您可以根据需要更改配置。以下示例使用预配置的配置。
请随意修改生成的 PHP 代码,因为您的更改 不会 被覆盖(如果需要可以覆盖)!
代码生成
以下快速示例展示了如何使用预配置配置生成 Command 类的 PHP 代码。
- 请参阅命令单元测试(
tests/CommantTest.php)以获取将生成的代码的全面示例。 - 请参阅事件单元测试(
tests/EventTest.php)以获取将生成的代码的全面示例。 - 请参阅聚合单元测试(
tests/AggregateTest.php)以获取将生成的代码的全面示例。 - 请参阅查询单元测试(
tests/QueryTest.php)以获取将生成的代码的全面示例。 - 请参阅值对象单元测试(
tests/ValueObjectTest.php)以获取将生成的代码的全面示例。
<?php declare(strict_types=1); use EventEngine\CodeGenerator\EventEngineAst\Command; use EventEngine\CodeGenerator\EventEngineAst\Config\EventEngineConfig; use EventEngine\CodeGenerator\EventEngineAst\Config\PreConfiguredNaming; use EventEngine\CodeGenerator\EventEngineAst\Metadata; use EventEngine\InspectioGraphCody\EventSourcingAnalyzer; use EventEngine\InspectioGraphCody\EventSourcingGraph; $contextName = 'Acme'; $basePath = '../app'; $composerFile = $basePath . '/composer.json'; $config = new EventEngineConfig(); $config->setBasePath($basePath); $config->addComposerInfo($composerFile); $namingConfig = new PreConfiguredNaming($config); $namingConfig->setDefaultContextName($contextName); $analyzer = new EventSourcingAnalyzer( new EventSourcingGraph( $config->getFilterConstName(), new Metadata\MetadataFactory(new Metadata\InspectioJson\MetadataFactory()) ) ); // create class to generate code for commands // same behaviour for the other classes e.g. EventEngine\CodeGenerator\EventEngineAst\Event $commandGenerator = new Command($namingConfig); // contains all generated PHP classes $fileCollection = \OpenCodeModeling\CodeAst\Builder\FileCollection::emptyList(); // assume that $codyNode is an instance of \EventEngine\InspectioGraphCody\Node which describes a command $connection = $analyzer->analyse($codyNode); // generate JSON schema file of the command $schemas = $commandGenerator->generateJsonSchemaFile($connection, $analyzer); foreach ($schemas as $schema) { $schema['filename']; // contains path with filename depending on your configuration $schema['code']; // contains generated JSON schema } // call the different generate methods of the code generator class $commandGenerator->generateApiDescription($connection, $analyzer, $fileCollection); $commandGenerator->generateApiDescriptionClassMap($connection, $analyzer, $fileCollection); $commandGenerator->generateCommandFile($connection, $analyzer, $fileCollection); $files = $config->getObjectGenerator()->generateFiles($fileCollection); // loop over files and store them in filesystem foreach ($files as $file) { $file['filename']; // contains path with filename depending on your configuration e.g. src/Domain/Aggregate $file['code']; // contains generated PHP code }