event-engine / php-code-generator-event-engine-ast
PHP Code Generator for Event Engine based on PHP AST
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-12-16 11:31:33 UTC
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 }