1
1
# Event Engine - PHP Code Generator via PHP AST
2
2
3
3
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 ") .
5
5
6
6
It supports the following code generation:
7
7
@@ -17,6 +17,12 @@ Run the following to install this library:
17
17
$ composer require event-engine/php-code-generator-event-engine-ast
18
18
```
19
19
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
+
20
26
## Usage
21
27
22
28
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/
26
32
> It is recommended to use the [ InspectIO Cody graph format] ( https://github.com/event-engine/php-inspectio-graph-cody " InspectIO Graph Cody ")
27
33
because it's based on a simple JSON structure.
28
34
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
31
36
change the configuration for your needs. The following example uses the preconfigured configurations.
32
37
33
38
> Feel free to modify the generated PHP code, because your changes will * NOT* be overwritten (can be overwritten if you want)!
34
39
35
- ### Command Code Generation
40
+ ### Code Generation
36
41
37
42
The following quick example shows how to generate PHP code for * Command* classes with the preconfigured configuration.
38
43
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.
40
49
41
50
``` php
42
51
<?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);
62
52
63
- // generate Command file with corresponding value objects (if any)
64
- $command->generateCommandFile($analyzer, $fileCollection);
53
+ declare(strict_types=1);
65
54
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;
68
61
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';
86
65
87
- $eventConfig = new \EventEngine\CodeGenerator\EventEngineAst\Config\PreConfiguredEvent();
66
+ $config = new EventEngineConfig();
67
+ $config->setBasePath($basePath);
68
+ $config->addComposerInfo($composerFile);
88
69
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 );
91
72
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);
94
82
95
83
// contains all generated PHP classes
96
84
$fileCollection = \OpenCodeModeling\CodeAst\Builder\FileCollection::emptyList();
97
85
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) ;
100
88
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 );
103
91
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
114
95
}
115
96
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);
128
101
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);
154
103
155
104
// loop over files and store them in filesystem
156
105
foreach ($files as $file) {
157
106
$file['filename']; // contains path with filename depending on your configuration e.g. src/Domain/Aggregate
158
107
$file['code']; // contains generated PHP code
159
108
}
109
+
160
110
```
0 commit comments