patchlevel / event-sourcing-psr-container
PSR-11 容器的事件源工厂
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-09-06 06:25:31 UTC
README
事件源 PSR-11 容器
patchlevel/event-sourcing 为 PSR-11 容器提供的工厂。
安装
composer require patchlevel/event-sourcing-psr-container
文档
配置构建器
要创建配置数组,您可以使用 ConfigBuilder。这提供了一些调整配置的方法。
$eventSourcingConfig = (new ConfigBuilder()) ->singleTable() ->databaseUrl('mysql://user:secret@localhost/app') ->addAggregatePath(__DIR__ . '/Aggregate') ->addEventPath(__DIR__ . '/Events') ->addProcessor(SendEmailProcessor::class) ->addProjector(ProfileProjection::class) ->build();
默认内置容器
自己的 PSR 容器实现已经集成了所有必要的工厂。因此,我们只需要传递配置。
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 服务管理器
工厂也可以与其他 PSR-11 兼容的库一起使用。以下是一个使用 Laminas 的示例。
composer require laminas/laminas-servicemanager
我们只需要指定工厂并传递配置。
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);