PHP code example of brenoroosevelt / oni-bus

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

    

brenoroosevelt / oni-bus example snippets



use OniBus\Command\Command;   

class UserRegistrationCommand implements Command
{
    public $username;
    public $password;
}


use OniBus\Attributes\CommandHandler;

class UserRegistrationHandler
{
    #[CommandHandler]
    public function register(UserRegistrationCommand $userRegistration)
    {
        /* ... */
        return new UserDTO(/* ... */);
    }
}


use OniBus\Attributes\CommandHandler;
use OniBus\Buses\DispatchToHandler;
use OniBus\Command\CommandBus;
use OniBus\Handler\Builder\Resolver;

$resolver = 
    Resolver::new(new MyPsr11Container())
        ->withHandlers([UserRegistrationHandler::class])
        ->mapByAttributes(CommandHandler::class);

$commandBus = new CommandBus(new DispatchToHandler($resolver));

$userDTO = $commandBus->dispatch(new UserRegistrationCommand('username', 'secret'));


use OniBus\Query\Query;   

class UserByStatus implements Query
{
    protected $status;
    
    public function __construct(string $status)
    {
           $this->status = $status;
    }
    
    public function getStatus(): string
    {
        return $this->status;
    }
}


use OniBus\Attributes\QueryHandler;

class FindUserByStatusSQL
{
    #[QueryHandler]
    public function fetch(UserByStatus $query)
    {
        return [/* ... */];
    }
}


use OniBus\Attributes\QueryHandler;
use OniBus\Buses\DispatchToHandler;
use OniBus\Query\QueryBus;
use OniBus\Handler\Builder\Resolver;

$resolver = 
    Resolver::new(new MyPsr11Container())
        ->withHandlers([FindUserByStatusSQL::class])
        ->mapByAttributes(QueryHandler::class);

$queryBus = new QueryBus(new DispatchToHandler($resolver));

$users = $queryBus->dispatch(new UserByStatus('active'));


use OniBus\Event\Event;  

class UserCreatedEvent implements Event
{
    protected $username;
    
    public function __construct(string $username)
    {
           $this->username = $username;
    }
    
    public function getUsername(): string
    {
        return $this->username;
    }
}


use OniBus\Attributes\EventListener;

class CreateUserProfile
{
    #[EventListener]
    public function createProfile(UserCreatedEvent $event)
    {
        /* ... */
    }
}


use OniBus\Attributes\EventListener;
use OniBus\Buses\DispatchToHandler;
use OniBus\Event\EventBus;
use OniBus\Handler\Builder\Resolver;

$resolver = 
    Resolver::new(new MyPsr11Container())
        ->withHandlers([CreateUserProfile::class])
        ->mapByAttributes(EventListener::class);

$eventBus = new EventBus(new DispatchToHandler($resolver));

$eventBus->dispatch(new UserCreatedEvent('username'));



/* CONTAINER */
$container = new MyPsr11Container();

/* EVENT BUS */
$eventResolver = 
    Resolver::new($container)
        ->withHandlers([CreateUserProfile::class])
        ->mapByAttributes(EventListener::class);
       
$eventBus = new EventBus(new DispatchToHandler($eventResolver));

/* COMMAND BUS */
$commandResolver = 
    Resolver::new($container)
        ->withHandlers([UserRegistrationHandler::class])
        ->mapByAttributes(CommandHandler::class);

$commandBus = 
     new CommandBus(
        new EventsDispatcher(
            EventManager::eventProvider(),
            $eventBus,
        ),
        // new ProtectOrder(),
        // new Transactional(),
        new DispatchToHandler($commandResolver)
    );


use OniBus\Command\CommandBus;

$container->add(CommandBus::class, $commandBus);



class UserRegistrationController
{
    protected $commandBus;
    
    public function __construct(CommandBus $commandBus) 
    {
        $this->commandBus = $commandBus;    
    }

    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args = []): ResponseInterface
    {
        $body = $request->getParsedBody();
        $command = new UserRegistrationCommand($body['username'], $body['password']);
        $user = $this->commandBus->dispatch($command);
        $response->getBody()->write($user);
        return $response;
    }