PHP code example of vshf / php-bus

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

    

vshf / php-bus example snippets


$bus = new \VSHF\Bus\Bus();

$command = new MyCommand($someParamsIfAny);

$bus->dispatch($command);

$result = $bus->ask(new GetUserQuery($id));

public function getResultType(): string
{
    return User::class;
}

$bus->addMiddleware(MyMiddleware::class);

class MyMiddleware implements \VSHF\Bus\MiddlewareInterface {

    public function before() : void
    {
        //  You can access:
        //  $this->$command or $this->query
        //  $this->agent_type
        //  $this->agent_id
        
        $this->next(); // Omit this to stop command/query execution.
    }
    
    public function after() : void
    {
        //  Runs after the handler.
        //
        //  You can access:
        //  $this->$command or $this->query
        //  $this->agent_type
        //  $this->agent_id
    }
}

$bus->addMiddleware(MyMiddleware::class, 99);

$bus->subscribeCommand(MyCommand::class, MyCommandHandler::class);
$bus->subscribeQuery(GetUserQuery::class, GetUserQueryHandler::class);

$bus->getCommandSubscriptions();
$bus->getQuerySubscriptions();

public function after(): void

public function after($result = null): void

      public function after($result = null): void
      

MyCommand.php
MyCommandHandler.php