Skip to content

Repository files navigation

Query Bus

Latest Version on PackagistSoftware LicenseBuild StatusCoverage StatusQuality ScoreTotal Downloads

Install

Via Composer

$ composer require smoothphp/querybus

Usage

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.

Laravel Users

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.

Service Provider

<?phpreturn [
// ...'providers' => [
// ...SmoothPhp\QueryBus\Laravel\LaravelQueryBusServiceProvider::class,
],
];

Query

<?phpclass FindUserById {
public$id;
publicfunction__construct(string$id) {
$this->id = $id;
}
}

Query Handler

<?phpclass FindUserByIdHandler {
private$client;
publicfunction__construct(DBClient$client) {
$this->client = $client;
}
publicfunctionhandle(FindUserById$query) {
return$this->client->table('users')->where('id', $query->id)->get();
}
}

Using

<?phpclass ExampleController extends Controller {
private$bus;
publicfunction__construct(\SmoothPhp\QueryBus\QueryBus$queryBus) {
$this->bus = $queryBus;
}
publicfunctionshowUser(string$userId) {
returnview('users.show')->with('user', $this->bus->query(newFindUserById($userId)));
}
}

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email simon@smoothphp.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

A simple query bus, Much like the command bus but a way to query with DTO

Resources

Contributing

Stars

10 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages