event-engine/php-code-generator-event-engine-ast
Composer 安装命令:
composer require event-engine/php-code-generator-event-engine-ast
包简介
PHP Code Generator for Event Engine based on PHP AST
README 文档
README
PHP Code Generator based on PHP Abstract Syntax Tree. It provides a comprehensive high level API to generate PHP code from prooph board for Event Engine.
It supports the following code generation:
- Event Engine API description for commands, aggregates and domain events
- Command, aggregate and domain event classes with corresponding value objects based on metadata (JSON schema)
- Glue code between command, corresponding aggregate and corresponding domain events
Installation
Run the following to install this library:
$ composer require event-engine/php-code-generator-event-engine-ast
If you want to use Config\PreConfiguredNaming please install also laminas/laminas-filter.
$ composer require laminas/laminas-filter
Usage
The code generation is based on the InspectIO Graph. There are two implementations of InspectIO Graph. The first one is based on the InspectIO GraphML graph format and the second is based on the InspectIO Cody graph format.
It is recommended to use the InspectIO Cody graph format because it's based on a simple JSON structure.
For out-of-the-box usage you can use the preconfigured configuration file Config\PreConfiguredNaming. You are free to
change the configuration for your needs. The following example uses the preconfigured configurations.
Feel free to modify the generated PHP code, because your changes will NOT be overwritten (can be overwritten if you want)!
Code Generation
The following quick example shows how to generate PHP code for Command classes with the preconfigured configuration.
- Please see command unit tests (
tests/CommantTest.php) for comprehensive examples which code will be generated. - Please see event unit tests (
tests/EventTest.php) for comprehensive examples which code will be generated. - Please see aggregate unit tests (
tests/AggregateTest.php) for comprehensive examples which code will be generated. - Please see query unit tests (
tests/QueryTest.php) for comprehensive examples which code will be generated. - Please see value object unit tests (
tests/ValueObjectTest.php) for comprehensive examples which code will be generated.
<?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 }
event-engine/php-code-generator-event-engine-ast 适用场景与选型建议
event-engine/php-code-generator-event-engine-ast 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 207 次下载、GitHub Stars 达 5, 最近一次更新时间为 2020 年 11 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「event-engine」 「ast」 「code-generator」 「code-generation」 「ast-php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 event-engine/php-code-generator-event-engine-ast 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 event-engine/php-code-generator-event-engine-ast 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 event-engine/php-code-generator-event-engine-ast 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Editable JSON AST with visitor traversal and formatting-preserving printing.
Parse Handlebars templates to a spec-compliant AST with PHP.
An AST nodes of the TypeLang
Alfabank REST API integration
Programmatically edit PHP and Laravel files.
Framework for building BNF LR(1) parsers
统计信息
- 总下载量: 207
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-11-20