Skip to content

Latest commit

 

History

History
111 lines (87 loc) · 2.99 KB

handler.md

File metadata and controls

111 lines (87 loc) · 2.99 KB

Query handler

You can use any implementations of callable type as a query handler.

The query handler can be a anonymous function:

$handler = static function (ContactByIdentityQuery $query) {
    // do something
};

// register query handler in handler locator
$locator = new DirectBindingQueryHandlerLocator();
$locator->registerHandler(ContactByIdentityQuery::class, $handler);

It can be a some function:

function ContactByIdentityHandler(ContactByIdentityQuery $query)
{
    // do something
}

// register query handler in handler locator
$locator = new DirectBindingQueryHandlerLocator();
$locator->registerHandler(ContactByIdentityQuery::class, 'ContactByIdentityHandler');

It can be a called object:

class ContactByIdentityHandler
{
    public function __invoke(ContactByIdentityQuery $query)
    {
        // do something
    }
}

// register query handler in handler locator
$locator = new DirectBindingQueryHandlerLocator();
$locator->registerHandler(ContactByIdentityQuery::class, new ContactByIdentityHandler());

It can be a static method of class:

class ContactByIdentityHandler
{
    public static function handleContactByIdentity(ContactByIdentityQuery $query)
    {
        // do something
    }
}

// register query handler in handler locator
$locator = new DirectBindingQueryHandlerLocator();
$locator->registerHandler(ContactByIdentityQuery::class, 'ContactByIdentityHandler::handleContactByIdentity');

It can be a public method of class:

class ContactByIdentityHandler
{
    public function handleContactByIdentity(ContactByIdentityQuery $query)
    {
        // do something
    }
}

// register query handler in handler locator
$locator = new DirectBindingQueryHandlerLocator();
$locator->registerHandler(ContactByIdentityQuery::class, [new ContactByIdentityHandler(), 'handleContactByIdentity']);

You can handle many querys in one handler.

class ArticleHandler
{
    public function handleContactByIdentity(ContactByIdentityQuery $query)
    {
        // do something
    }

    public function handleContactByName(ContactByNameQuery $query)
    {
        // do something
    }
}

// register query handler in handler locator
$locator = new DirectBindingQueryHandlerLocator();
$locator->registerHandler(ContactByIdentityQuery::class, [new ArticleHandler(), 'handleContactByIdentity']);
$locator->registerHandler(ContactByNameQuery::class, [new ArticleHandler(), 'handleContactByName']);

Query handler locator

You can use exists locators of query handler:

Or you can create custom locator that implements GpsLab\Component\Query\Handler\Locator\QueryHandlerLocator interface.