https://github.com/patchlevel/event-sourcing-psr-container
patchlevel/event-sourcing factories for PSR-11 containers
https://github.com/patchlevel/event-sourcing-psr-container
Last synced: 7 months ago
JSON representation
patchlevel/event-sourcing factories for PSR-11 containers
- Host: GitHub
- URL: https://github.com/patchlevel/event-sourcing-psr-container
- Owner: patchlevel
- License: mit
- Created: 2022-12-28T10:55:44.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-12-30T16:59:24.000Z (about 1 year ago)
- Last Synced: 2025-01-10T19:23:32.575Z (about 1 year ago)
- Language: PHP
- Homepage:
- Size: 91.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](//packagist.org/packages/patchlevel/event-sourcing-psr-container)
[](//packagist.org/packages/patchlevel/event-sourcing-psr-container)
# Event-Sourcing PSR-11 Container
[patchlevel/event-sourcing](https://github.com/patchlevel/event-sourcing) factories for PSR-11 containers.
## Installation
```bash
composer require patchlevel/event-sourcing-psr-container
```
## Documentation
### Config Builder
To create a configuration array, you can use the ConfigBuilder.
This offers methods to adjust the configuration.
```php
$eventSourcingConfig = (new ConfigBuilder())
->singleTable()
->databaseUrl('mysql://user:secret@localhost/app')
->addAggregatePath(__DIR__ . '/Aggregate')
->addEventPath(__DIR__ . '/Events')
->addProcessor(SendEmailProcessor::class)
->addProjector(ProfileProjection::class)
->build();
```
### Default Build-In Container
The own PSR container implementation already integrates all necessary factories.
So we only have to pass the configuration.
```php
use Patchlevel\EventSourcing\Container\ConfigBuilder;
use Patchlevel\EventSourcing\Container\DefaultContainer;
$container = new DefaultContainer(
$eventSourcingConfig,
[
HotelProjection::class => fn(DefaultContainer $container)
=> new HotelProjection($container->connection()),
SendEmailProcessor::class => fn(DefaultContainer $container)
=> new SendEmailProcessor($container->get('mailer')),
]
);
$container->get(SchemaDirector::class)->create();
$hotelRepository = $container->repository(Hotel::class);
```
### Laminas Service Manager
Factories can also be used with other PSR-11 compatible libraries.
Here is an example with [Laminas](https://docs.laminas.dev/laminas-servicemanager/).
```bash
composer require laminas/laminas-servicemanager
```
We only have to specify the factories and pass the configuration.
```php
use Laminas\ServiceManager\ServiceManager;
use Patchlevel\EventSourcing\Repository\RepositoryManager;
use Patchlevel\EventSourcing\Schema\SchemaDirector;
use Patchlevel\EventSourcingPsrContainer\ConfigBuilder;
use Patchlevel\EventSourcingPsrContainer\Factory\ConnectionFactory;
use Patchlevel\EventSourcingPsrContainer\Factory\RepositoryManagerFactory;
use Patchlevel\EventSourcingPsrContainer\Factory\SchemaDirectorFactory;
$serviceManager = new ServiceManager([
'services' => [
'config' => [
'event_sourcing' => $eventSourcingConfig
],
SendEmailProcessor::class => new SendEmailProcessor()
],
'factories' => [
'event_sourcing.connection' => new ConnectionFactory(),
RepositoryManager::class => new RepositoryManagerFactory(),
SchemaDirector::class => new SchemaDirectorFactory(),
HotelProjection::class => static fn (ServiceManager $container) => new HotelProjection($container->get('event_sourcing.connection')),
],
]);
$serviceManager->get(SchemaDirector::class)->create();
$repositoryManager = $serviceManager->get(RepositoryManager::class);
$hotelRepository = $repositoryManager->get(Hotel::class);
```