Prooph进入Laravel CommandBus无法识别CommandHandler

时间:2018-11-20 17:16:32

标签: php laravel cqrs event-sourcing prooph

我有一个laravel 5.7应用程序,我想在其中添加用于事件源的prooph。我已按照说明进行操作,但我检索到此错误:

Prooph\ServiceBus\Exception\RuntimeException: CommandBus was not able to identify a CommandHandler for command App\src\Prooph\Model\Procedure\Command\ChangeProcedureState in /var/www/html/vendor/prooph/service-bus/src/CommandBus.php:58

这是我的config / prooph.php文件

return [
    'event_store' => [
        'adapter' => [
            'type' => \Prooph\EventStore\Pdo\MySqlEventStore::class,
            'options' => [
                'connection_alias' => 'laravel.connections.pdo',
            ],
        ],
        'plugins' => [
            \Prooph\EventStoreBusBridge\EventPublisher::class,
            \Prooph\EventStoreBusBridge\TransactionManager::class,
        ],
        'procedure_collection' => [
            'repository_class' => App\src\Prooph\Infrastructure\Repository\EventStoreProcedureCollection::class,
            'aggregate_type' => App\src\Prooph\Model\Procedure\Procedure::class,
        ],
    ],
    'service_bus' => [
        'command_bus' => [
            'router' => [
                'type' => \Prooph\ServiceBus\Plugin\Router\CommandRouter::class,
                'routes' => [
                    // list of commands with corresponding command handler
                    \App\src\Prooph\Model\Procedure\Command\ChangeProcedureState::class => App\src\Prooph\Model\Procedure\Handler\ChangeProcedureStateHandler::class,
                ],
            ],
        ],
        'event_bus' => [
            'plugins' => [
                \Prooph\ServiceBus\Plugin\InvokeStrategy\OnEventStrategy::class,
            ],
            'router' => [
                'routes' => [
                    // list of events with a list of projectors
                ],
            ],
        ],
        /*'event_bus' => [
            'router' => [
                'routes' => [
                    // list of events with a list of projectors
                    App\src\Prooph\ProophessorDo\Model\User\Event\UserWasRegistered::class => [
                        \App\src\Prooph\ProophessorDo\Projection\User\UserProjector::class
                    ],
                ],
            ],
        ],*/
    ],
];

这是我派遣命令的服务

class ProcedureRetrieveStateChanged
{
    /** @var \FluentProceduresRepository $procedureRepository */
    private $procedureRepository;
    /**
     * @var CommandBus
     */
    private $commandBus;

    public function __construct(\FluentProceduresRepository $procedureRepository, CommandBus $commandBus)
    {
        $this->procedureRepository = $procedureRepository;
        $this->commandBus = $commandBus;
    }

    public function execute($procedureId, array $groups)
    {
        $procedure = $this->procedureRepository->find($procedureId);

        if (null === $procedure) {
            return false;
        }

        foreach ($groups as $group) {
            $actualState = $procedure->getStatebyGroup($group['pivot']['group_id']);

            if ($actualState !== $group['pivot']['state']) {
                $command = new ChangeProcedureState(
                    [
                        'procedure_id' => $procedureId,
                        'group_id' => $group['pivot']['group_id'],
                        'old_state' => $actualState,
                        'new_state' => $group['pivot']['state'],
                    ]
                );

                $this->commandBus->dispatch($command);

            }
        }
    }
}

这是我的命令

final class ChangeProcedureState extends Command implements PayloadConstructable
{
    use PayloadTrait;

    public function procedureId(): ProcedureId
    {
        return ProcedureId::fromString($this->payload['procedure_id']);
    }

    public function state(): string
    {
        return $this->payload['state'];
    }

    protected function setPayload(array $payload): void
    {
        Assertion::keyExists($payload, 'procedure_id');
        Assertion::keyExists($payload, 'group_id');
        Assertion::keyExists($payload, 'old_state');
        Assertion::keyExists($payload, 'new_state');

        $this->payload = $payload;
    }
}

这是我的服务员

class ChangeProcedureStateHandler
{
    /**
     * @var ProcedureCollection
     */
    private $procedureCollection;

    public function __construct(ProcedureCollection $procedureCollection)
    {
        $this->procedureCollection = $procedureCollection;
    }

    public function __invoke(ChangeProcedureState $command): void
    {
        $procedure = $this->procedureCollection->get($command->procedureId());
        $procedure->changeState($command->state());

        $this->procedureCollection->save($procedure);
    }
}

有人可以帮助我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

要将处理程序用作字符串,您需要使用ServiceLocatorPlugin,但是在此之前,您需要在laravel容器中注册所有处理程序,例如$ app-> singletor ...或$ app-> bind。

干杯。