marwa-framework

Events

Marwa Framework dispatches lifecycle events through the shared marwa-event bus. You can listen to them from application code through config/event.php or by registering subscribers.

Register Listeners In config/event.php

<?php

use App\Listeners\LogApplicationBoot;
use App\Listeners\LogRequestHandled;
use Marwa\Framework\Adapters\Event\AppBooted;
use Marwa\Framework\Adapters\Event\RequestHandled;

return [
    'listeners' => [
        AppBooted::class => [
            [LogApplicationBoot::class, 'handle'],
        ],
        RequestHandled::class => [
            [LogRequestHandled::class, 'handle'],
        ],
    ],
    'subscribers' => [],
];

Listener classes can be resolved from the container.

<?php

namespace App\Listeners;

use Marwa\Framework\Adapters\Event\RequestHandled;

final class LogRequestHandled
{
    public function handle(RequestHandled $event): void
    {
        logger()->info('request_handled', [
            'method' => $event->method,
            'path' => $event->path,
            'status' => $event->statusCode,
        ]);
    }
}

Register Subscribers

<?php

namespace App\Listeners;

use Marwa\Event\Contracts\Subscriber;
use Marwa\Framework\Adapters\Event\AppBooted;
use Marwa\Framework\Adapters\Event\ConsoleBootstrapped;

final class RuntimeSubscriber implements Subscriber
{
    public static function subscribe($events): void
    {
        $events->listen(AppBooted::class, [self::class, 'onBoot']);
        $events->listen(ConsoleBootstrapped::class, [self::class, 'onConsoleBoot']);
    }

    public static function onBoot(AppBooted $event): void {}

    public static function onConsoleBoot(ConsoleBootstrapped $event): void {}
}

Then add it to config/event.php:

'subscribers' => [
    App\Listeners\RuntimeSubscriber::class,
],

Available Lifecycle Events

Manual Dispatch

For custom domain events, you can still dispatch directly:

use Marwa\Framework\Facades\Event;

Event::dispatch(new UserRegistered($userId));