Search by

romanfedorskij / message-bus-spiral

wolfcharaa

Message bus adapter for Spiral Framework

Package info

github.com/wolfcharaa/message-bus-spiral

pkg:composer/romanfedorskij/message-bus-spiral

Statistics

Installs: 20

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v6.1.0 2026-09-14 17:02 UTC

This package is auto-updated.

Last update: 2026-09-14 17:06:40 UTC


README

romanfedorskij/message-bus-spiral — адаптер Spiral Framework для romanfedorskij/message-bus v6.

Поддерживаемые версии: Spiral Framework ^3.16, RoadRunner Bridge ^3.8 || ^4.0.

Пакет не содержит отдельного registry builder. В v6 связи message -> action компилируются основным message-bus, а Spiral-пакет подключает:

  • Spiral\Tokenizer listener для поиска классов с attributes;
  • compiler listener, который пишет compiled registry в runtime-файл;
  • Spiral DI resolver;
  • Spiral invoker для handler/middleware;
  • Spiral Queue provider;
  • Spiral Queue job для выполнения serialized envelope.

Установка

composer require romanfedorskij/message-bus-spiral

Конфигурация

В приложении нужно указать путь к compiled registry. Listener создаст этот файл при boot приложения:

<?php

declare(strict_types=1);

use Wolfcharaa\MessageBus\Flow\FlowDefinition;
use Wolfcharaa\MessageBus\Registry\MessageRegistryCompilerOptions;
use Wolfcharaa\MessageBus\Spiral\Application\Job\QueueHandlerJob;

return [
    'registryFile' => directory('runtime') . 'cache/message_bus_registry.php',
    'queueJob' => QueueHandlerJob::class,
    'runtimePlan' => true,
    'compilerOptions' => new MessageRegistryCompilerOptions(failOnWarning: false),
    'flows' => [
        FlowDefinition::sync('default'),
        FlowDefinition::sync('domain_capability'),
    ],
];

compilerOptions необязателен. По умолчанию compilation падает только на ошибках registry.

Файл подключается как message_bus config.

Discovery через Spiral Tokenizer

MessageBusBootloader регистрирует listener в TokenizerListenerBootloader. Listener получает классы с attributes:

  • CommandHandler;
  • QueryHandler;
  • EventSubscriber;
  • MessageAlias.

После обхода проекта listener вызывает core MessageRegistryCompiler и сохраняет результат в registryFile. Runtime MessageBus читает уже compiled registry через CompiledMessageRegistry::fromFile().

Runtime plan для long-running Spiral

После чтения compiled registry адаптер собирает RuntimePlanRegistry в памяти процесса:

  • заранее подготавливает карту bindingId -> plan;
  • заранее сортирует bindings по priority;
  • заранее объединяет flow middleware + binding middleware;
  • заранее объединяет flow delivery + binding delivery;
  • автоматически подменяет default sync/async strategies на runtime-plan strategies.

Runtime plan хранит только class-string и immutable definition-данные. Объекты action и middleware не кешируются адаптером: их продолжает создавать Spiral DI в текущем scope. Это сохраняет корректную работу request/auth scope и подмен интерфейсов в long-running RoadRunner worker.

С core message-bus 6.1+ runtime plan использует binding-specific context: каждый middleware/handler invocation получает Envelope с текущим bindingId. Это важно для cache, audit, flow contract diagnostics и idempotency middleware.

Flow для Spiral Queue

transport используется как имя Spiral queue connection. queue используется как queue name внутри options.

use Wolfcharaa\MessageBus\Flow\FlowDefinition;
use Wolfcharaa\MessageBus\Flow\FlowRegistry;

$flows = new FlowRegistry(
    FlowDefinition::sync('default'),
    FlowDefinition::async('notifications')
        ->transport('roadrunner', 'notifications'),
);

Async binding обязан иметь стабильный bindingId.

Worker

Producer сохраняет в Spiral Queue переносимый array payload. Worker восстанавливает SerializedEnvelope и вызывает core worker:

\Wolfcharaa\MessageBus\Spiral\Application\Job\QueueHandlerJob::class

PHP serialize() не используется.

Примеры

Тесты

composer test

Coverage gate:

composer test:coverage

Команда пишет Clover report в /tmp/messagebus-spiral-coverage.xml и падает, если line coverage ниже 80%.