bezdomni/tactician-pimple

Pimple DI 容器用 Tactician 命令定位器

1.0.0 2016-01-23 08:47 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:52:57 UTC


README

允许从 Pimple 依赖注入容器中惰性加载命令处理器。

Travis Packagist Packagist

安装

通过 composer 安装

composer require bezdomni/tactician-pimple

用法

假设您有几个命令:UserAddCommandUserDeleteCommand,以及相应的处理器 UserAddHandlerUserDeleteHandler

use Bezdomni\Tactician\Pimple\PimpleLocator;
use League\Tactician\CommandBus;
use League\Tactician\Handler\CommandHandlerMiddleware;
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
use League\Tactician\Handler\MethodNameInflector\HandleInflector;
use Pimple\Container;

// Create a container and configure the handlers on it
$container = new Container();
$container['handlers.user.add'] = function () {
    echo "Creating AddUserHandler\n";
    return new AddUserHandler();
};
$container['handlers.user.delete'] = function () {
    echo "Creating DeleteUserHandler\n";
    return new DeleteUserHandler();
};

// Map command class names to container keys holding corresponding handlers
$locatorMap = [
    AddUserCommand::class => 'handlers.user.add',
    DeleteUserCommand::class => 'handlers.user.delete',
];

// Create the locator
$locator = new PimpleLocator($container, $locatorMap);

// Create a command handler middleware using the pimple locator
$middleware = new CommandHandlerMiddleware(
    new ClassNameExtractor(),
    $locator,
    new HandleInflector()
);

// Create the command bus using the middleware, and you're ready to go
$commandBus = new CommandBus([$middleware]);

// Create and run commands on the command bus
$addUserCommand = new AddUserCommand();
$deleteUserCommand = new DeleteUserCommand();

$commandBus->handle($addUserCommand);
$commandBus->handle($deleteUserCommand);

许可证

MIT 许可证 (MIT)。请参阅许可证文件以获取更多信息。