PHP code example of icanboogie / message-bus

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

    

icanboogie / message-bus example snippets




namespace ICanBoogie\MessageBus;

/* @var Dispatcher $dispatcher */
/* @var object $message */

// The message is dispatched to its handler, the result is returned.
$result = $dispatcher->dispatch($message);



// …

final class DeleteMenu
{
    public function __construct(
        public /*readonly*/ int $id
    ) {
    }
}



use ICanBoogie\MessageBus\Safe;

// …

final class ShowMenu implements Safe
{
    public function __construct(
        public /*readonly*/ int $id
    ) {
    }
}



final class ShowMenuHandler
{
    public function __invoke(ShowMenu $message): Menu
    {
        // …
    }
}



/* @var ICanBoogie\MessageBus\HandlerProvider $provider */
/* @var object $message */

$handler = $provider->getHandlerForMessage($message);
$result = $handler($message);



use ICanBoogie\MessageBus\PSR\HandlerProviderWithContainer;
use Psr\Container\ContainerInterface;

/* @var $container ContainerInterface */

$handlerProvider = new HandlerProviderWithContainer($container, [

	Acme\MenuService\Application\MessageBus\CreateMenu::class => Acme\MenuService\Application\MessageBus\CreateMenuHandler::class,
	Acme\MenuService\Application\MessageBus\DeleteMenu::class => Acme\MenuService\Application\MessageBus\DeleteMenuHandler::class,

]);



use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
use ICanBoogie\MessageBus\Symfony\MessageBusPass;

/* @var string $config */

$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__));
$loader->load($config);
$container->addCompilerPass(new MessageBusPass());
$container->compile();


namespace Acme\MenuService\Application\MessageBus;

use ICanBoogie\MessageBus\Attribute\Handler;
use ICanBoogie\MessageBus\Attribute\Permission;

#[Permission('is_admin')]
#[Permission('can_write_menu')]
final class CreateMenu
{
    // ...
}

#[Handler]
final class CreateMenuHandler
{
    // ...
}



namespace Acme\MenuService\Presentation\Security\Voters;

use ICanBoogie\MessageBus\Attribute\Vote;

#[Vote('can_write_menu')]
final class CanWriteMenu
{
    // ...
}



// ...
$container->addCompilerPass(new MessageBusPassWithAttributes());
$container->addCompilerPass(new MessageBusPass());
// ...



namespace ICanBoogie\MessageBus;

/* @var HandlerProviderWithHandlers $providerWithHandlers */
/* @var PSR\HandlerProviderWithContainer $providerWithContainer */

$provider = new HandlerProviderWithChain([ $providerWithHandlers, $providerWithContainer ]);

/* @var object $message */

$handler = $provider->getHandlerForMessage($message);

    

    // ...

    use ICanBoogie\MessageBus\RestrictedDispatcher;

    final class MenuController
    {
        public function __construct(
            private RestrictedDispatcher $dispatcher
        ) {}
    }
    

    

    // ...

    use ICanBoogie\MessageBus\Context;

    final class MenuController
    {
        // ...

        public function delete(Request $request): Response
        {
            // ...

            $this->dispatch(
                new MenuDelete($id),
                new Context([ $token ])
            );
        }
    }