Skip to content

Commit bab8d2c

Browse files
authored
Merge pull request #3 from event-engine/feature/add-missing-types
Simplify configuration and node analysis and add missing metadata types
2 parents 7567d2c + d82287a commit bab8d2c

File tree

95 files changed

+6567
-1859
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+6567
-1859
lines changed

.php-cs-fixer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$config = new Prooph\CS\Config\Prooph();
6+
$finder = $config->getFinder();
7+
8+
$finder->exclude('vendor');
9+
$finder->in(__DIR__);
10+
$finder->append(['.php_cs']);
11+
12+
$cacheDir = \getenv('TRAVIS') ? \getenv('HOME') . '/.php-cs-fixer' : __DIR__;
13+
14+
$config->setCacheFile($cacheDir . '/.php_cs.cache');
15+
16+
return $config;

.php_cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

README.md

Lines changed: 51 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Event Engine - PHP Code Generator via PHP AST
22

33
PHP Code Generator based on PHP Abstract Syntax Tree. It provides a comprehensive high level API to
4-
generate PHP code from [InspectIO](https://github.com/event-engine/inspectio "InspectIO") for [Event Engine](https://event-engine.io "Event Engine").
4+
generate PHP code from [prooph board](https://prooph-board.com/ "prooph board") for [Event Engine](https://event-engine.github.io/ "Event Engine").
55

66
It supports the following code generation:
77

@@ -17,6 +17,12 @@ Run the following to install this library:
1717
$ composer require event-engine/php-code-generator-event-engine-ast
1818
```
1919

20+
If you want to use `Config\PreConfiguredNaming` please install also `laminas/laminas-filter`.
21+
22+
```bash
23+
$ composer require laminas/laminas-filter
24+
```
25+
2026
## Usage
2127

2228
The code generation is based on the [InspectIO Graph](https://github.com/event-engine/php-inspectio-graph "InspectIO Graph").
@@ -26,135 +32,79 @@ and the second is based on the [InspectIO Cody graph format](https://github.com/
2632
> It is recommended to use the [InspectIO Cody graph format](https://github.com/event-engine/php-inspectio-graph-cody "InspectIO Graph Cody")
2733
because it's based on a simple JSON structure.
2834

29-
For out-of-the-box usage you can use one of the preconfigured *Command* (`Config\PreConfiguredCommand`),
30-
*Aggregate* (`Config\PreConfiguredAggregate`) and *Event* (`Config\PreConfiguredEvent`) configurations. You are free to
35+
For out-of-the-box usage you can use the preconfigured configuration file `Config\PreConfiguredNaming`. You are free to
3136
change the configuration for your needs. The following example uses the preconfigured configurations.
3237

3338
> Feel free to modify the generated PHP code, because your changes will *NOT* be overwritten (can be overwritten if you want)!
3439
35-
### Command Code Generation
40+
### Code Generation
3641

3742
The following quick example shows how to generate PHP code for *Command* classes with the preconfigured configuration.
3843

39-
> Please see command unit tests (`tests/CommantTest.php`) for comprehensive examples which code will be generated.
44+
- Please see command unit tests (`tests/CommantTest.php`) for comprehensive examples which code will be generated.
45+
- Please see event unit tests (`tests/EventTest.php`) for comprehensive examples which code will be generated.
46+
- Please see aggregate unit tests (`tests/AggregateTest.php`) for comprehensive examples which code will be generated.
47+
- Please see query unit tests (`tests/QueryTest.php`) for comprehensive examples which code will be generated.
48+
- Please see value object unit tests (`tests/ValueObjectTest.php`) for comprehensive examples which code will be generated.
4049

4150
```php
4251
<?php
43-
// Assume $command is an instance of \EventEngine\InspectioGraph\CommandType
44-
// Assume $analyzer is an instance of \EventEngine\InspectioGraph\EventSourcingAnalyzer
45-
46-
$commandConfig = new \EventEngine\CodeGenerator\EventEngineAst\Config\PreConfiguredCommand();
47-
48-
// configure namespaces for code generation via Composer
49-
$commandConfig->addComposerInfo('[path to your composer.json file]');
50-
51-
// create instance for command code generation
52-
$command = new \EventEngine\CodeGenerator\EventEngineAst\Command($commandConfig);
53-
54-
// contains all generated PHP classes
55-
$fileCollection = \OpenCodeModeling\CodeAst\Builder\FileCollection::emptyList();
56-
57-
// path where the Command API Event Engine description should be generated based on Composer autoloader info
58-
$apiCommandFilename = 'src/Domain/Api/Command.php';
59-
60-
// generate command API description based on the InspectIO Graph info
61-
$command->generateApiDescription($analyzer, $fileCollection, $apiCommandFilename);
6252

63-
// generate Command file with corresponding value objects (if any)
64-
$command->generateCommandFile($analyzer, $fileCollection);
53+
declare(strict_types=1);
6554

66-
// generate PHP code with filenames
67-
$files = $commandConfig->getObjectGenerator()->generateFiles($fileCollection);
55+
use EventEngine\CodeGenerator\EventEngineAst\Command;
56+
use EventEngine\CodeGenerator\EventEngineAst\Config\EventEngineConfig;
57+
use EventEngine\CodeGenerator\EventEngineAst\Config\PreConfiguredNaming;
58+
use EventEngine\CodeGenerator\EventEngineAst\Metadata;
59+
use EventEngine\InspectioGraphCody\EventSourcingAnalyzer;
60+
use EventEngine\InspectioGraphCody\EventSourcingGraph;
6861

69-
// loop over files and store them in filesystem
70-
foreach ($files as $file) {
71-
$file['filename']; // contains path with filename depending on your configuration e.g. src/Domain/Command
72-
$file['code']; // contains generated PHP code
73-
}
74-
```
75-
76-
### Domain Event Code Generation
77-
78-
The following quick example shows how to generate PHP code for *Domain Event* classes with the preconfigured configuration.
79-
80-
> Please see event unit tests (`tests/EventTest.php`) for comprehensive examples which code will be generated.
81-
82-
```php
83-
<?php
84-
// Assume $event is an instance of \EventEngine\InspectioGraph\EventType
85-
// Assume $analyzer is an instance of \EventEngine\InspectioGraph\EventSourcingAnalyzer
62+
$contextName = 'Acme';
63+
$basePath = '../app';
64+
$composerFile = $basePath . '/composer.json';
8665

87-
$eventConfig = new \EventEngine\CodeGenerator\EventEngineAst\Config\PreConfiguredEvent();
66+
$config = new EventEngineConfig();
67+
$config->setBasePath($basePath);
68+
$config->addComposerInfo($composerFile);
8869

89-
// configure namespaces for code generation via Composer
90-
$eventConfig->addComposerInfo('[path to your composer.json file]');
70+
$namingConfig = new PreConfiguredNaming($config);
71+
$namingConfig->setDefaultContextName($contextName);
9172

92-
// create instance for command code generation
93-
$event = new \EventEngine\CodeGenerator\EventEngineAst\Event($eventConfig);
73+
$analyzer = new EventSourcingAnalyzer(
74+
new EventSourcingGraph(
75+
$config->getFilterConstName(),
76+
new Metadata\MetadataFactory(new Metadata\InspectioJson\MetadataFactory())
77+
)
78+
);
79+
// create class to generate code for commands
80+
// same behaviour for the other classes e.g. EventEngine\CodeGenerator\EventEngineAst\Event
81+
$commandGenerator = new Command($namingConfig);
9482

9583
// contains all generated PHP classes
9684
$fileCollection = \OpenCodeModeling\CodeAst\Builder\FileCollection::emptyList();
9785

98-
// path where the Event API Event Engine description should be generated based on Composer autoloader info
99-
$apiEventFilename = 'src/Domain/Api/Event.php';
86+
// assume that $codyNode is an instance of \EventEngine\InspectioGraphCody\Node which describes a command
87+
$connection = $analyzer->analyse($codyNode);
10088

101-
// generate command API description based on the InspectIO Graph info
102-
$event->generateApiDescription($analyzer, $fileCollection, $apiEventFilename);
89+
// generate JSON schema file of the command
90+
$schemas = $commandGenerator->generateJsonSchemaFile($connection, $analyzer);
10391

104-
// generate Event file with corresponding value objects (if any)
105-
$event->generateEventFile($analyzer, $fileCollection);
106-
107-
// generate PHP code with filenames
108-
$files = $eventConfig->getObjectGenerator()->generateFiles($fileCollection);
109-
110-
// loop over files and store them in filesystem
111-
foreach ($files as $file) {
112-
$file['filename']; // contains path with filename depending on your configuration e.g. src/Domain/Event
113-
$file['code']; // contains generated PHP code
92+
foreach ($schemas as $schema) {
93+
$schema['filename']; // contains path with filename depending on your configuration
94+
$schema['code']; // contains generated JSON schema
11495
}
11596

116-
```
117-
118-
### Aggregate Code Generation
119-
120-
The following quick example shows how to generate PHP code for *Aggregate* classes with the preconfigured configuration.
121-
122-
> Please see event unit tests (`tests/AggregateTest.php`) for comprehensive examples which code will be generated.
123-
124-
```php
125-
<?php
126-
// Assume $aggregate is an instance of \EventEngine\InspectioGraph\AggregateType
127-
// Assume $analyzer is an instance of \EventEngine\InspectioGraph\EventSourcingAnalyzer
97+
// call the different generate methods of the code generator class
98+
$commandGenerator->generateApiDescription($connection, $analyzer, $fileCollection);
99+
$commandGenerator->generateApiDescriptionClassMap($connection, $analyzer, $fileCollection);
100+
$commandGenerator->generateCommandFile($connection, $analyzer, $fileCollection);
128101

129-
$aggregateConfig = new \EventEngine\CodeGenerator\EventEngineAst\Config\PreConfiguredAggregate();
130-
131-
// configure namespaces for code generation via Composer
132-
$aggregateConfig->addComposerInfo('[path to your composer.json file]');
133-
134-
// create instance for aggregate code generation
135-
$aggregate = new \EventEngine\CodeGenerator\EventEngineAst\Aggregate($aggregateConfig);
136-
137-
// contains all generated PHP classes
138-
$fileCollection = \OpenCodeModeling\CodeAst\Builder\FileCollection::emptyList();
139-
140-
// path where the Aggregate API Event Engine description should be generated based on Composer autoloader info
141-
$apiAggregateFilename = 'src/Domain/Api/Aggregate.php';
142-
143-
// generate aggregate API description based on the InspectIO Graph info
144-
$aggregate->generateApiDescription($analyzer, $fileCollection, $apiAggregateFilename);
145-
146-
// generate Aggregate file with corresponding value objects (if any)
147-
$aggregate->generateAggregateFile($analyzer, $fileCollection, $apiEventFilename);
148-
149-
// generate Aggregate state file to store state changes (ImmutableRecord)
150-
$aggregate->generateAggregateStateFile($analyzer, $fileCollection);
151-
152-
// generate PHP code with filenames
153-
$files = $aggregateConfig->getObjectGenerator()->generateFiles($fileCollection);
102+
$files = $config->getObjectGenerator()->generateFiles($fileCollection);
154103

155104
// loop over files and store them in filesystem
156105
foreach ($files as $file) {
157106
$file['filename']; // contains path with filename depending on your configuration e.g. src/Domain/Aggregate
158107
$file['code']; // contains generated PHP code
159108
}
109+
160110
```

composer.json

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
"description": "PHP Code Generator for Event Engine based on PHP AST",
44
"license": "MIT",
55
"type": "library",
6+
"keywords": [
7+
"php",
8+
"code-generator",
9+
"code-generation",
10+
"ast-php",
11+
"ast",
12+
"abstract-syntax-tree",
13+
"event-engine"
14+
],
615
"authors": [
716
{
817
"name": "Alexander Miertsch",
@@ -32,24 +41,25 @@
3241
"require": {
3342
"php": "^7.4 || ^8.0",
3443
"ext-json": "*",
35-
"event-engine/php-inspectio-graph": "^0.3.0",
36-
"open-code-modeling/json-schema-to-php": "^0.3",
37-
"open-code-modeling/json-schema-to-php-ast": "^0.5.1",
38-
"open-code-modeling/php-code-ast": "^0.12.0",
39-
"open-code-modeling/php-filter": "^0.1.2",
40-
"symfony/console": "^4.4 || ^5.0"
44+
"event-engine/php-inspectio-graph-cody": "^0.1.0",
45+
"open-code-modeling/json-schema-to-php": "^0.4.x-dev",
46+
"open-code-modeling/json-schema-to-php-ast": "0.6.x-dev",
47+
"open-code-modeling/php-code-ast": "0.13.x-dev",
48+
"open-code-modeling/php-filter": "^0.2.1 || 0.3.x-dev"
4149
},
4250
"require-dev": {
43-
"event-engine/php-inspectio-graph-cody": "dev-master",
4451
"laminas/laminas-filter": "^2.10",
4552
"league/flysystem": "^2.0",
4653
"league/flysystem-memory": "^2.0",
4754
"phpspec/prophecy-phpunit": "^2.0",
4855
"phpstan/phpstan": "^0.12.33",
4956
"phpstan/phpstan-strict-rules": "^0.12.4",
5057
"phpunit/phpunit": "^9.5.0",
51-
"prooph/php-cs-fixer-config": "^0.4",
52-
"roave/security-advisories": "dev-master"
58+
"prooph/php-cs-fixer-config": "^0.5.0",
59+
"roave/security-advisories": "dev-latest"
60+
},
61+
"suggest": {
62+
"laminas/laminas-filter": "If you want to use PreConfiguredNaming configuration"
5363
},
5464
"minimum-stability": "dev",
5565
"prefer-stable": true,

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ parameters:
22
level: 5
33
paths:
44
- src/
5+
excludes_analyse:
6+
- src/Metadata/InspectioJson/JsonMetadataTrait.php
7+
ignoreErrors:

0 commit comments

Comments
 (0)