PHP code example of open-solid / bus

1. Go to this page and download the library: Download open-solid/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/ */

    

open-solid / bus example snippets


use App\Message\MyMessage;
use OpenSolid\Bus\Handler\MessageHandlersLocator;
use OpenSolid\Bus\Middleware\HandlingMiddleware;
use OpenSolid\Bus\NativeMessageBus;

// This is your custom function that does something when a message arrives.
$handler = function (MyMessage $message): mixed {
    // Do stuff with the message here...
};

// Setting up the bus with a middleware that knows who handles the message.
$bus = new NativeMessageBus([
    new HandlingMiddleware(new MessageHandlersLocator([
        MyMessage::class => [$handler], // Maps messages to handlers.
    ])),
]);

// Send a message using the bus.
$bus->dispatch(new MyMessage());

use App\Message\MyMessage;

class MyMessageHandler
{
    public function __invoke(MyMessage $message): mixed
    {
        // Process the message here...
    }
}

use OpenSolid\Bus\Envelope\Envelope;
use OpenSolid\Bus\Middleware\Middleware;
use OpenSolid\Bus\Middleware\NextMiddleware;

class MyMiddleware implements Middleware
{
    public function handle(Envelope $envelope, NextMiddleware $next): void
    {
        // Do something before the message handler works.

        $next->handle($envelope); // Call the next middleware

        // Do something after the message handler is done.
    }
}