1. Go to this page and download the library: Download sbooker/command-bus 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/ */
sbooker / command-bus example snippets
// src/Mailing/Command/SendEmail.php
final class SendEmail { /* ... DTO с to, subject, body ... */ }
// src/Mailing/Endpoint/SendEmailEndpoint.php
use Sbooker\CommandBus\Endpoint;
final class SendEmailEndpoint implements Endpoint
{
private Mailer $mailer;
public function process(UuidInterface $id, object $payload): void
{
/** @var SendEmail $payload */
$this->mailer->send(/* ... */);
}
}
// src/UseCase/RegisterUser/Handler.php
/** @var Sbooker\CommandBus\CommandBus $commandBus */
$command = new SendEmail('[email protected]', 'Welcome!', '...');
$commandBus->accept(Uuid::uuid4(), $command); // Быстро и надежно
// src/Warehouse/Command/ReserveItems.php
final class ReserveItems { /* ... DTO с orderId, items ... */ }
// src/Warehouse/Endpoint/ReserveItemsEndpoint.php
use Sbooker\CommandBus\Endpoint;
final class ReserveItemsEndpoint implements Endpoint
{
private WarehouseService $warehouseService;
public function process(UuidInterface $id, object $payload): void
{
/** @var ReserveItems $payload */
$this->warehouseService->reserve(/* ... */); // Может выбросить исключение
}
}
// src/Order/Subscriber/OrderPaidSubscriber.php
use Sbooker\DomainEvents\DomainEventSubscriber;
use Sbooker\CommandBus\CommandBus;
final class OrderPaidSubscriber implements DomainEventSubscriber
{
private CommandBus $commandBus;
public function handleEvent(DomainEvent $event): void
{
if (!$event instanceof OrderPaid) return;
$command = new ReserveItems($event->getOrderId(), $event->getItems());
$this->commandBus->accept(Uuid::uuid4(), $command);
}
// ...
}
// bootstrap.php или ваш DI-контейнер
use Sbooker\CommandBus\Registry\Containerized\ContainerizedRegistry;
use Sbooker\CommandBus\Registry\Containerized\ContainerAdapter;
/** @var Psr\Container\ContainerInterface $container */
$endpointsMap = new ContainerAdapter($container, null, [
'send-email' => SendEmailEndpoint::class,
'reserve-items' => ReserveItemsEndpoint::class,
]);
$timeoutsMap = new ContainerAdapter($container, 'default_timeout');
$registry = new ContainerizedRegistry($endpointsMap, $timeoutsMap);
// worker.php
/** @var Sbooker\CommandBus\Handler $handler */
while (true) {
if (!$handler->handleNext()) sleep(5);
}
// Рекомендуется использовать sbooker/event-loop-worker для более эффективной реализации
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.