1. Go to this page and download the library: Download zumba/cqrs library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zumba / cqrs example snippets
namespace My\Command;
use Zumba\CQRS\Command\Command;
use Zumba\CQRS\Command\WithProperties;
final class MyCommand extends Command implements WithProperties
{
protected string $message;
public static function fromArray(array $props): Command
{
$command = new static();
$command->message = $props['message'];
return $command;
}
}
namespace My\Command;
use Zumba\CQRS\Command\Command;
use Zumba\CQRS\Command\CommandResponse;
use Zumba\CQRS\Command\Handler;
use Zumba\CQRS\CommandService;
final class MyCommandHandler implements Handler
{
public function handle(Command $command, CommandService $commandService): CommandResponse
{
echo $command->message;
return CommandResponse::fromSuccess();
}
}
use My\Command\MyCommand;
use Zumba\CQRS\CommandBus;
use Zumba\CQRS\Response\Success;
$commandBus = CommandBus::defaultBus();
$command = MyCommand::fromProperties([
'message' => 'Hello!',
]);
$result = $commandBus->dispatch($command);
// Hello! is echoed
var_dump($result instanceof Success);
// true
namespace My\Query;
use Zumba\CQRS\Query\Query;
use Zumba\CQRS\Query\WithProperties;
final class MyQuery extends Query implements WithProperties
{
protected string $ID;
public static function fromArray(array $props): Query
{
$query = new static();
$query->ID = $props['id'];
return $query;
}
}
namespace My\Query;
use Zumba\CQRS\Query\Handler;
use Zumba\CQRS\Query\Query;
use Zumba\CQRS\Query\QueryResponse;
final class MyQueryHandler implements Handler
{
public function handle(Query $query): QueryResponse
{
// Do appropriate lookups
return QueryResponse::fromMap([
'id' => 'example-id',
'name' => 'example-name',
]);
}
}
namespace My\Query;
use Zumba\CQRS\QueryBusTrait;
class Example {
use QueryBusTrait;
public function query(): array
{
$response = $this->queryBus()->dispatch(new MyQuery([
'id' => 'example-id',
]));
return [
'id' => $response['id'],
'name' => $response['name'],
];
}
}
var_dump((new Example)->query());
// [
// 'id' => 'example-id',
// 'name' => 'example-name',
// ]
namespace My\Command;
use Zumba\CQRS\Command\HandlerFactory;
use Zumba\CQRS\Command\Handler;
class MyCommandHandlerFactory implements HandlerFactory
{
public static function make(): Handler
{
return new MyCommandHandler();
}
}
namespace My\Command;
use Zumba\CQRS\Command\Command;
use Zumba\CQRS\Command\CommandResponse;
use Zumba\CQRS\Command\Handler;
use Zumba\CQRS\Command\HandlerFactory;
use Zumba\CQRS\CommandService;
final class MyCommandHandler implements Handler, HandlerFactory
{
public function handle(Command $command, CommandService $commandService): CommandResponse
{
echo $command->message;
return CommandResponse::fromSuccess();
}
public static function make(): Handler
{
return new static();
}
}
use My\Provider\CustomProvider;
$bus = CommandBus::fromProviders(
new CustomProvider(),
new ClassProvider(),
);
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use Zumba\CQRS\CommandBus;
use Zumba\CQRS\MiddlewarePipeline;
use Zumba\CQRS\Middleware\Logger;
$bus = CommandBus::defaultBus();
// Substitute with a real Psr logger.
$logger = new NullLogger();
$middlewarePipeline = MiddlewarePipeline::fromMiddleware(
Logger::fromLoggerAndLevel(
$logger,
LogLevel::INFO,
)
);
$bus = $bus->withMiddleware($middlewarePipeline);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.