patchlevel / event-sourcing-psr-container
event sourcing factories for PSR-11 containers
dev-main
2024-04-22 06:40 UTC
Requires
- php: ~8.1.0 || ~8.2.0
- patchlevel/event-sourcing: ^2.1.0
- psr/container: ^1.0|^2.0
Requires (Dev)
- doctrine/migrations: ^3.3.2
- infection/infection: ^0.26.13
- laminas/laminas-servicemanager: ^3.20
- patchlevel/coding-standard: ^1.2.0
- phpspec/prophecy-phpunit: ^2.0.1
- phpstan/phpstan: ^1.8.2
- phpunit/phpunit: ^9.5.23
- psalm/plugin-phpunit: ^0.18.0
- roave/infection-static-analysis-plugin: ^1.22.0
- vimeo/psalm: ^5.0.0
- dev-main
- dev-renovate/php-8.x
- dev-renovate/shivammathur-setup-php-2.x
- dev-renovate/infection-infection-0.x
- dev-renovate/eomm-why-don-t-you-tweet-2.x
- dev-renovate/patchlevel-event-sourcing-3.x
- dev-renovate/lock-file-maintenance
- dev-renovate/phpunit-phpunit-11.x
- dev-renovate/laminas-laminas-servicemanager-4.x
- dev-renovate/psalm-plugin-phpunit-0.x
This package is auto-updated.
Last update: 2024-11-06 06:49:35 UTC
README
Event-Sourcing PSR-11 Container
patchlevel/event-sourcing factories for PSR-11 containers.
Installation
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.
$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.
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.
composer require laminas/laminas-servicemanager
We only have to specify the factories and pass the configuration.
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);