antidot-fw/tactician

Tactician Command bus adapter for Antidot Framework.

0.0.3 2021-01-10 15:49 UTC

This package is auto-updated.

Last update: 2024-10-20 15:31:57 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Getting started

Installation

composer require antidot-fw/tactician

Config

Create command-bus.global.php file inner config autoload directory.

<?php
// command-bus.global.php

return [
    'dependencies' => [
        'invokables' => [
            \App\Handler\PingHandler::class => \App\Handler\PingHandler::class,
        ]
    ],
    'command-bus' => [
        'handler-map' => [
            \App\Command\PingCommand::class => \App\Handler\PingHandler::class
        ],
    ],
];

Example command and handler.

<?php

namespace App\Command;

class PingCommand
{

}
<?php

namespace App\Handler;

use App\Command\PingCommand;

class PingHandler
{
    public function __invoke(PingCommand $command)
    {
        return time();
    }
}

You can use InFw\TacticianAdapter\Action\AbstractAction as base action.

<?php

namespace App\Action;

use App\Command\PingCommand;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;

class PingAction implements RequestHandlerInterface
{
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        return new JsonResponse(['ack' => $this->bus->handle(new PingCommand())]);
    }
}

Modify Command Bus

You can modify the entire command bus to meet the needs of your project.

This is default config.

<?php
// command-bus.global.php

return [
    ...,
    'command_bus' => [
        'locator' => \League\Tactician\Handler\Locator\HandlerLocator::class,
        'inflector' => \League\Tactician\Handler\MethodNameInflector\MethodNameInflector::class,
        'extractor' => \League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor::class,
        'middleware' => [
            \League\Tactician\Plugins\LockingMiddleware::class,
            \League\Tactician\Logger\LoggerMiddleware::class,
            \League\Tactician\CommandEvents\EventMiddleware::class,

        ],
    ],
];

Query Bus

You can use a query bus too, the difference with the command bus is that the query bus will always return an object.

<?php
// command-bus.global.php

return [
    ...,
    'query_bus' => [
        'locator' => 'query_bus.handler_locator',
        'inflector' => \League\Tactician\Handler\MethodNameInflector\MethodNameInflector::class,
        'extractor' => \League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor::class,
        'middleware' => [

        ],
    ],
];