Skip to content

Migrate to vertex connections #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f1ef0c9
Migrate to vertex connections
sandrokeil Jul 9, 2021
e140cf6
Add support for role and hotSpot type
sandrokeil Jul 21, 2021
9aca0a7
Rename streamCollection to stream
sandrokeil Jul 23, 2021
d9322bc
Use camelCase
sandrokeil Jul 23, 2021
c2ecdbc
Add missing metadata class for external system
sandrokeil Jul 23, 2021
f4ce3a8
Add support for generating class map
sandrokeil Jul 23, 2021
7b1d577
Use camelCase instead of snake_case for constant values (message name…
sandrokeil Jul 26, 2021
6683fb8
Message name should be PascalCase
sandrokeil Jul 27, 2021
ad63cdf
Catch metadata parsing errors to improve error message
sandrokeil Jul 27, 2021
566052d
Introduce feature metadata voNamespace and always make value objects …
sandrokeil Jul 27, 2021
2d29b10
Add Behaviour suffix to aggregate class and move aggregate state clas…
sandrokeil Jul 27, 2021
fd51555
Use printer from config
sandrokeil Aug 4, 2021
2b7c1ba
Update open-code-modeling/json-schema-to-php-ast to 0.6.x-dev
sandrokeil Aug 5, 2021
f90c6cb
Generate API description with JsonSchemaArray::fromFile() method
sandrokeil Aug 5, 2021
3cb816b
Generate schema files for command, event and value objects in same na…
sandrokeil Aug 6, 2021
d81c02b
Fix not needed namespace import
sandrokeil Aug 27, 2021
196d0e6
Support namespace definition with FQCN and remove shared option support
sandrokeil Aug 27, 2021
e0218fe
Use voNamespace from current vertex instead of feature vertex and sup…
sandrokeil Sep 17, 2021
65e0f66
Resolve metadata references
sandrokeil Sep 23, 2021
3708d4a
Exclude exception classes from immutable record
sandrokeil Sep 24, 2021
cf89452
Fix wrong identifier to support namespace
sandrokeil Sep 24, 2021
6af68c3
Generate query and resolver with api description
sandrokeil Oct 1, 2021
9e946ad
Generate finder class for query
sandrokeil Oct 6, 2021
cd994c9
Generate finder class methods for query
sandrokeil Oct 6, 2021
2042264
Add missing namespace import for TypeRef
sandrokeil Oct 8, 2021
01dcaca
Set "is required" from type to resolved type
sandrokeil Oct 8, 2021
a63d426
Merge remote-tracking branch 'origin/feature/simplify-connections' in…
sandrokeil Oct 8, 2021
9a1a181
Add some method body lines for queries
sandrokeil Oct 8, 2021
e0ce49e
Generate complete resolver method
sandrokeil Oct 15, 2021
44b9077
Generate complete finder method and collection class
sandrokeil Oct 15, 2021
cf95658
Update event-engine/php-inspectio-graph-cody to ^0.1.0 and prooph/php…
sandrokeil Feb 23, 2022
726c2d5
Suggest laminas/laminas-filter
sandrokeil Feb 23, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

$config = new Prooph\CS\Config\Prooph();
$finder = $config->getFinder();

$finder->exclude('vendor');
$finder->in(__DIR__);
$finder->append(['.php_cs']);

$cacheDir = \getenv('TRAVIS') ? \getenv('HOME') . '/.php-cs-fixer' : __DIR__;

$config->setCacheFile($cacheDir . '/.php_cs.cache');

return $config;
10 changes: 0 additions & 10 deletions .php_cs

This file was deleted.

152 changes: 51 additions & 101 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Event Engine - PHP Code Generator via PHP AST

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

It supports the following code generation:

Expand All @@ -17,6 +17,12 @@ 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`.

```bash
$ composer require laminas/laminas-filter
```

## Usage

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

For out-of-the-box usage you can use one of the preconfigured *Command* (`Config\PreConfiguredCommand`),
*Aggregate* (`Config\PreConfiguredAggregate`) and *Event* (`Config\PreConfiguredEvent`) configurations. You are free to
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)!

### Command Code Generation
### 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 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
<?php
// Assume $command is an instance of \EventEngine\InspectioGraph\CommandType
// Assume $analyzer is an instance of \EventEngine\InspectioGraph\EventSourcingAnalyzer

$commandConfig = new \EventEngine\CodeGenerator\EventEngineAst\Config\PreConfiguredCommand();

// configure namespaces for code generation via Composer
$commandConfig->addComposerInfo('[path to your composer.json file]');

// create instance for command code generation
$command = new \EventEngine\CodeGenerator\EventEngineAst\Command($commandConfig);

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

// path where the Command API Event Engine description should be generated based on Composer autoloader info
$apiCommandFilename = 'src/Domain/Api/Command.php';

// generate command API description based on the InspectIO Graph info
$command->generateApiDescription($analyzer, $fileCollection, $apiCommandFilename);

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

// generate PHP code with filenames
$files = $commandConfig->getObjectGenerator()->generateFiles($fileCollection);
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;

// 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/Command
$file['code']; // contains generated PHP code
}
```

### Domain Event Code Generation

The following quick example shows how to generate PHP code for *Domain Event* classes with the preconfigured configuration.

> Please see event unit tests (`tests/EventTest.php`) for comprehensive examples which code will be generated.

```php
<?php
// Assume $event is an instance of \EventEngine\InspectioGraph\EventType
// Assume $analyzer is an instance of \EventEngine\InspectioGraph\EventSourcingAnalyzer
$contextName = 'Acme';
$basePath = '../app';
$composerFile = $basePath . '/composer.json';

$eventConfig = new \EventEngine\CodeGenerator\EventEngineAst\Config\PreConfiguredEvent();
$config = new EventEngineConfig();
$config->setBasePath($basePath);
$config->addComposerInfo($composerFile);

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

// create instance for command code generation
$event = new \EventEngine\CodeGenerator\EventEngineAst\Event($eventConfig);
$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();

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

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

// generate Event file with corresponding value objects (if any)
$event->generateEventFile($analyzer, $fileCollection);

// generate PHP code with filenames
$files = $eventConfig->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/Event
$file['code']; // contains generated PHP code
foreach ($schemas as $schema) {
$schema['filename']; // contains path with filename depending on your configuration
$schema['code']; // contains generated JSON schema
}

```

### Aggregate Code Generation

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

> Please see event unit tests (`tests/AggregateTest.php`) for comprehensive examples which code will be generated.

```php
<?php
// Assume $aggregate is an instance of \EventEngine\InspectioGraph\AggregateType
// Assume $analyzer is an instance of \EventEngine\InspectioGraph\EventSourcingAnalyzer
// 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);

$aggregateConfig = new \EventEngine\CodeGenerator\EventEngineAst\Config\PreConfiguredAggregate();

// configure namespaces for code generation via Composer
$aggregateConfig->addComposerInfo('[path to your composer.json file]');

// create instance for aggregate code generation
$aggregate = new \EventEngine\CodeGenerator\EventEngineAst\Aggregate($aggregateConfig);

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

// path where the Aggregate API Event Engine description should be generated based on Composer autoloader info
$apiAggregateFilename = 'src/Domain/Api/Aggregate.php';

// generate aggregate API description based on the InspectIO Graph info
$aggregate->generateApiDescription($analyzer, $fileCollection, $apiAggregateFilename);

// generate Aggregate file with corresponding value objects (if any)
$aggregate->generateAggregateFile($analyzer, $fileCollection, $apiEventFilename);

// generate Aggregate state file to store state changes (ImmutableRecord)
$aggregate->generateAggregateStateFile($analyzer, $fileCollection);

// generate PHP code with filenames
$files = $aggregateConfig->getObjectGenerator()->generateFiles($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
}

```
18 changes: 10 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@
"require": {
"php": "^7.4 || ^8.0",
"ext-json": "*",
"event-engine/php-inspectio-graph-cody": "dev-feature/add-missing-types as dev-master",
"open-code-modeling/json-schema-to-php": "^0.3",
"open-code-modeling/json-schema-to-php-ast": "^0.5.1",
"open-code-modeling/php-code-ast": "^0.12.0",
"open-code-modeling/php-filter": "^0.1.2",
"symfony/console": "^4.4 || ^5.0"
"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"
},
"require-dev": {
"laminas/laminas-filter": "^2.10",
Expand All @@ -56,8 +55,11 @@
"phpstan/phpstan": "^0.12.33",
"phpstan/phpstan-strict-rules": "^0.12.4",
"phpunit/phpunit": "^9.5.0",
"prooph/php-cs-fixer-config": "^0.4",
"roave/security-advisories": "dev-master"
"prooph/php-cs-fixer-config": "^0.5.0",
"roave/security-advisories": "dev-latest"
},
"suggest": {
"laminas/laminas-filter": "If you want to use PreConfiguredNaming configuration"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ parameters:
excludes_analyse:
- src/Metadata/InspectioJson/JsonMetadataTrait.php
ignoreErrors:
- '#EventEngine\\InspectioGraph\\EventSourcingAnalyzer given#'
- '#returns EventEngine\\InspectioGraph\\VertexType\|null#'
Loading