emerido/domain

此包已被废弃且不再维护。未建议替代包。
关于此包的最新版本(0.0.3)没有可用的许可证信息。

0.0.3 2017-06-28 11:19 UTC

This package is not auto-updated.

Last update: 2020-01-17 01:04:08 UTC


README

PHP库,用于管理域名逻辑

为什么?

  • 为什么如果找不到消息的处理程序,CommandBus 和 QueryBus 会抛出异常?

    • 如果您执行消息且找不到处理程序,这不是一个好的情况。但您可以在中间件中处理这种情况。

CommandBus 示例

创建 CommandBus

<?php require_once __DIR__ . '/../vendor/autoload.php';

$commandBus = \Domain\Bus\CommandBusBuilder::make()
    // Let's define method name resolver.
    // In this case CreateTodo command will handled with handleCreateTodo() method 
    ->usingHandlerMethodResolver(new \Domain\Handler\Method\NamedMethodResolver())
    
    // Let's define handler class resolver (see below)
    // In this case we use PHP-DI with autowiring for creating our handler
    ->usingHandlerResolver(new \Domain\Handler\ContainerResolver($container))
    
    // Register command handlers via providers (see below)
    ->register(new TodoCommandsHandlerProvider())
    
    // Finally build our CommandBus
    ->build()
;

创建您的第一个命令消息

<?php

class CreateTodo
{

    protected $subject;
    
    public function getSubject() : string 
    {
        return $this->subject;
    }
    
    public function setSubject(string $subject)
    {
        $this->subject = $subject;    
    }
    
}

此外,您可以在消息中实现一些接口,并在自定义中间件中处理这些契约。

例如,在我的情况下,我有

  • StoryInterface 用于标记某些消息,如一条历史记录
  • PersistentInterface 用于自动将消息和有效载荷保存到事件存储中

命令处理程序的示例

<?php

class TodoCommandsHandler
{
    
    private $table;
    
    private $eventBus;
    
    public function __construct(TodoGateway $table, EventBusInterface $eventBus) 
    {
        $this->table = $table;
        $this->eventBus = $eventBus;
    }
    
    public function handleCreateTodo(CreateTodo $command)
    {
        $todo = new Todo();
        $todo->setSubject($command->getSubject());
        $todo->setCompleted(false);
        
        try {
            // Save todo
            $this->table->create($todo);
            
            // Emit successful event
            $this->eventBus->emit(new TodoCreated($todo));
            
        } catch (DatabaseException $exception) {
            // TODO : Write error log
            throw $exception;
        }
        
        // Yes, you can return newly created todo
        return $todo;
    }
    
}

创建命令处理程序提供程序并注册之前创建的处理程序,用于 Todo 命令

<?php


class TodoCommandsHandlerProvider implements \Domain\Handler\HandlersProvider
{
    
    public function register(\Domain\Handler\HandlersMap $handlers)
    {
        // Tell handler class name which contains method to handle this command
        $handlers->handle(CreateTodo::class, TodoCommandsHandler::class);    
    }
    
}