1. Go to this page and download the library: Download arkonsoft/ps-module-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/ */
arkonsoft / ps-module-cqrs example snippets
if (!defined('_PS_VERSION_')) {
exit;
}
use Arkonsoft\PsModule\CQRS\CommandBus;
use Arkonsoft\PsModule\CQRS\QueryBus;
uct()
{
// ... module setup ...
$resolveHandler = fn(string $handlerClass): object => $this->getContainer()->get($handlerClass);
$this->commandBus = new CommandBus($resolveHandler);
$this->queryBus = new QueryBus($resolveHandler);
}
private function getContainer(): object
{
// Your DI container (e.g. PSR-11, arkonsoft/ps-module-di, etc.)
static $container = null;
if ($container === null) {
$container = new \Some\Container();
}
return $container;
}
}
// src/Application/Command/CreateProductCommand.php
namespace MyModule\Application\Command;
use Arkonsoft\PsModule\CQRS\Attribute\HandledBy;
use MyModule\Application\Handler\CreateProductHandler;
#[HandledBy(CreateProductHandler::class)]
final readonly class CreateProductCommand
{
public function __construct(
public string $name,
public float $price,
public int $categoryId,
) {}
}
// src/Application/Handler/CreateProductHandler.php
namespace MyModule\Application\Handler;
use MyModule\Application\Command\CreateProductCommand;
final class CreateProductHandler
{
public function handle(CreateProductCommand $command): int
{
$product = new \Product();
$product->name = $command->name;
$product->price = $command->price;
$product->id_category_default = $command->categoryId;
$product->add();
return (int) $product->id;
}
}
// src/Application/Query/GetProductByIdQuery.php
namespace MyModule\Application\Query;
use Arkonsoft\PsModule\CQRS\Attribute\HandledBy;
use MyModule\Application\Handler\GetProductByIdHandler;
#[HandledBy(GetProductByIdHandler::class)]
final readonly class GetProductByIdQuery
{
public function __construct(public int $productId) {}
}
// src/Application/Handler/GetProductByIdHandler.php
namespace MyModule\Application\Handler;
use MyModule\Application\Query\GetProductByIdQuery;
final class GetProductByIdHandler
{
public function handle(GetProductByIdQuery $query): array
{
$product = new \Product($query->productId);
if (!\Validate::isLoadedObject($product)) {
throw new \RuntimeException('Product not found');
}
return [
'id' => $product->id,
'name' => $product->name,
'price' => (float) $product->price,
];
}
}
// Execute a command
$command = new \MyModule\Application\Command\CreateProductCommand('New Product', 29.99, 1);
$productId = $this->commandBus->handle($command);
// Execute a query
$query = new \MyModule\Application\Query\GetProductByIdQuery($productId);
$productData = $this->queryBus->handle($query);
$resolveHandler = fn(string $handlerClass): object => $container->get($handlerClass);
$this->commandBus = new CommandBus($resolveHandler);
$this->queryBus = new QueryBus($resolveHandler);
$resolveHandler = fn(string $handlerClass): object => new $handlerClass();
$this->commandBus = new CommandBus($resolveHandler);
$this->queryBus = new QueryBus($resolveHandler);