Via Composer
$ composer require smoothphp/querybus
The query bus exists to execute queries within the domain of the application. Typically these are read only commands, with write operations being performed using the Command Bus.
The QueryBus is a simple concept, and leaves the majority of the implementation decisions to the developer. A simple implementation is provided for Laravel users.
The query bus exists of 3 components.
- Query
The DTO containing the intent and parameters for the query. - Query Bus
Takes a query object, resolves the query handler, and executes it. - Query Translator
Takes a query, and translates to the query handler class name.
The Laravel Query bus takes a Query object, and resolves the handler by adding 'Handler' to the class name. This handler class is then resolved by the container and all dependencies are injected.
For example, App\Queries\FindUserById
is resolved to App\Queries\FindUserByIdHandler
, and the handle
method is executed.
You are free to implement query handler resolution however you like, though.
<?php
return [
// ...
'providers' => [
// ...
SmoothPhp\QueryBus\Laravel\LaravelQueryBusServiceProvider::class,
],
];
<?php
class FindUserById {
public $id;
public function __construct(string $id) {
$this->id = $id;
}
}
<?php
class FindUserByIdHandler {
private $client;
public function __construct(DBClient $client) {
$this->client = $client;
}
public function handle(FindUserById $query) {
return $this->client->table('users')->where('id', $query->id)->get();
}
}
<?php
class ExampleController extends Controller {
private $bus;
public function __construct(\SmoothPhp\QueryBus\QueryBus $queryBus) {
$this->bus = $queryBus;
}
public function showUser(string $userId) {
return view('users.show')->with('user', $this->bus->query(new FindUserById($userId)));
}
}
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING and CONDUCT for details.
If you discover any security related issues, please email simon@smoothphp.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.