PHP code example of ajgl / simple-bus-query-bus

1. Go to this page and download the library: Download ajgl/simple-bus-query-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/ */

    

ajgl / simple-bus-query-bus example snippets


use Ajgl\SimpleBus\Message\Bus\Middleware\CatchReturnMessageBusSupportingMiddleware;

$query = new CatchReturnMessageBusSupportingMiddleware();

use SimpleBus\Message\CallableResolver\CallableMap;
use SimpleBus\Message\CallableResolver\ServiceLocatorAwareCallableResolver;

// Provide a map of query names to callables. You can provide actual callables, or lazy-loading ones.
$queryHandlersByQueryName = [
    'Fully\Qualified\Class\Name\Of\Query' => ... // a "callable"
];

// Provide a service locator callable. It will be used to instantiate a handler service whenever requested.
$serviceLocator = function ($serviceId) {
    $handler = ...;

    return $handler;
}

$queryHandlerMap = new CallableMap(
    $queryHandlersByQueryName,
    new ServiceLocatorAwareCallableResolver($serviceLocator)
);

use SimpleBus\Message\Name\ClassBasedNameResolver;

$queryNameResolver = new ClassBasedNameResolver();

use SimpleBus\Message\Name\NamedMessageNameResolver;

$queryNameResolver = new NamedMessageNameResolver();

use SimpleBus\Message\Name\NamedMessage;

class YourQuery implements NamedMessage
{
    public static function messageName()
    {
        return 'your_query';
    }
}

use SimpleBus\Message\Handler\Resolver\NameBasedMessageHandlerResolver;

$queryHandlerResolver = new NameBasedMessageHandlerResolver(
    $queryNameResolver,
    $queryHandlerMap
);

use Ajgl\SimpleBus\Message\Handler\DelegatesToMessageHandlerAndCatchReturnMiddleware;

$queryBus->appendMiddleware(
    new DelegatesToMessageHandlerAndCatchReturnMiddleware(
        $queryHandlerResolver
    )
);

class FindUserByEmailAddress
{
    private $emailAddress;

    public function __construct($emailAddress)
    {
        $this->emailAddress = $emailAddress;
    }

    public function emailAddress()
    {
        return $this->emailAddress;
    }
}

class FindUserByEmailAddressQueryHandler
{
    ...

    public function handle(FindUserByEmailAddress $query)
    {
        $user = $this->userRepository->findOneByEmailAddress(
            $query->emailAddress()
        );

        return $user;
    }
}

$query = new FindUserByEmailAddress(
    '[email protected]'
);

$queryBus->handle($query, $queryResult);