|
| 1 | +[](https://travis-ci.org/remotelyliving/php-query-bus) |
| 2 | +[](https://packagist.org/packages/remotelyliving/php-query-bus) |
| 3 | +[](https://coveralls.io/github/remotelyliving/php-query-bus?branch=master) |
| 4 | +[](https://packagist.org/packages/remotelyliving/php-query-bus) |
| 5 | +[](https://scrutinizer-ci.com/g/remotelyliving/php-query-bus/?branch=master) |
| 6 | + |
| 7 | +# php-query-bus: A Query Bus Implementation For PHP |
| 8 | + |
| 9 | +### Use Cases |
| 10 | + |
| 11 | +If you want a light weight compliment to your Command Bus for CQRS, hopefully this library helps out. |
| 12 | +It's very similar to a Command Bus, but it returns a Result. |
| 13 | + |
| 14 | +I've used magical data loading solutions before, but good old fashioned set of specific Query, Result, and Handler objects for a given Use Case |
| 15 | +is generally more performant, predictable, and explicit than array or magic-based implementations. |
| 16 | + |
| 17 | +### Installation |
| 18 | + |
| 19 | +```sh |
| 20 | +composer require remotelyliving/php-query-bus |
| 21 | +``` |
| 22 | + |
| 23 | +### Usage |
| 24 | + |
| 25 | +#### Create the Query Resolver |
| 26 | + |
| 27 | +The resolver can have handlers added manually or locate them in a PSR-11 Service Container |
| 28 | +Queries are mapped 1:1 with a handler and are mapped by the Query class name as the lookup key. |
| 29 | +```php |
| 30 | +$resolver = Resolver::create($serviceContainer) // can locate in service container |
| 31 | + ->pushHandler(MyQueryHandler1::class, new MyQueryHandler1()) // can locate in a local map |
| 32 | + ->pushHandlerDeferred(MyQueryHandler2::class, $lazyCreateMethod); // can locate deferred to save un unnecessary object creation |
| 33 | + |
| 34 | +``` |
| 35 | + |
| 36 | +#### Create the Query Bus |
| 37 | + |
| 38 | +The Query Bus takes in a Query Resolver and pushes whatever Middleware you want on the stack. |
| 39 | +```php |
| 40 | +$queryBus = QueryBus::create($resolver) |
| 41 | + ->pushMiddleware($myMiddleware1); |
| 42 | + |
| 43 | +$query = new MyQuery1('id'); |
| 44 | +$result = $queryBus->handle($result); |
| 45 | +``` |
| 46 | + |
| 47 | +That's really all there is to it! |
| 48 | + |
| 49 | +### Query |
| 50 | + |
| 51 | +The DTO's for this library are left intentionally unimplemented. They are just interfaces to implement. |
| 52 | +Eventually all magic breaks down somewhere and I'm not providing any here. My suggestion for Query objects |
| 53 | +is to keep them as a DTO of what you need to query your data source by. |
| 54 | + |
| 55 | +An example query might look like this: |
| 56 | + |
| 57 | +```php |
| 58 | +class GetUserQuery implements Interfaces\Query |
| 59 | +{ |
| 60 | + /** |
| 61 | + * @var bool |
| 62 | + */ |
| 63 | + private $shouldGetProfile = false; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var string |
| 67 | + */ |
| 68 | + private $userId; |
| 69 | + |
| 70 | + public function __construct(string $userId) |
| 71 | + { |
| 72 | + $this->userId = $userId; |
| 73 | + } |
| 74 | + |
| 75 | + public function getUserId(): string |
| 76 | + { |
| 77 | + return $this->userId; |
| 78 | + } |
| 79 | + |
| 80 | + public function addUserProfile(): self |
| 81 | + { |
| 82 | + $this->shouldGetProfile = true; |
| 83 | + return $this; |
| 84 | + } |
| 85 | + |
| 86 | + public function getGetUserProfileQuery(): ?GetUserProfileQuery |
| 87 | + { |
| 88 | + return ($this->shouldGetProfile) |
| 89 | + ? new GetUserProfileQuery($this->userId) |
| 90 | + : null; |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +As you can see, it's just a few getters and option builder. |
| 96 | + |
| 97 | +### Result |
| 98 | + |
| 99 | +The Result is similarly unimplemented. |
| 100 | +Results must implement `\JsonSerializable` but that's about it. |
| 101 | +They can have their own custom getters for your use case. An example Result for the `GetUserQuery` above might look like: |
| 102 | + |
| 103 | +```php |
| 104 | +class GetUserResult implements Result |
| 105 | +{ |
| 106 | + |
| 107 | + /** |
| 108 | + * @var \stdClass|null |
| 109 | + */ |
| 110 | + private $user; |
| 111 | + |
| 112 | + /** |
| 113 | + * @var \RemotelyLiving\PHPQueryBus\Tests\Stubs\GetUserProfileResult|null |
| 114 | + */ |
| 115 | + private $userProfileResult; |
| 116 | + |
| 117 | + public function __construct(?\stdClass $user, GetUserProfileResult $userProfileResult = null) |
| 118 | + { |
| 119 | + $this->user = $user; |
| 120 | + $this->userProfileResult = $userProfileResult; |
| 121 | + } |
| 122 | + |
| 123 | + public function getUser(): ?\stdClass |
| 124 | + { |
| 125 | + return $this->user; |
| 126 | + } |
| 127 | + |
| 128 | + public function getUserProfileResult(): ?GetUserProfileResult |
| 129 | + { |
| 130 | + return $this->userProfileResult; |
| 131 | + } |
| 132 | + |
| 133 | + public function jsonSerialize(): array |
| 134 | + { |
| 135 | + return [ |
| 136 | + 'user' => $this->getUser(), |
| 137 | + 'profile' => $this->getUserProfileResult(), |
| 138 | + ]; |
| 139 | + } |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +As you can see, it's not too hard to start building Result graphs for outputting a response or to feed another part of your app. |
| 144 | + |
| 145 | +### Handler |
| 146 | + |
| 147 | +The handlers are where the magic happens. Inject what ever repository or ORM you need to load data. |
| 148 | +It will ask the query for query parameters and return a result. You can also request other query results inside a handler from the bus. |
| 149 | +Going with our GetUserQuery example, a Handler could look like: |
| 150 | + |
| 151 | +```php |
| 152 | +class GetUserHandler implements Handler |
| 153 | +{ |
| 154 | + public function handle(Query $query, QueryBus $bus): Result |
| 155 | + { |
| 156 | + $user = $this->userRepository->getUserById($query->getUserId()); |
| 157 | + |
| 158 | + return ($query->getGetUserProfileQuery()) |
| 159 | + ? new GetUserResult($user, $bus->handle($query->getGetUserProfileQuery())) |
| 160 | + : new GetUserResult($user); |
| 161 | + } |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +### Middleware |
| 166 | + |
| 167 | +There are a few middleware that this library ships with. Take a look and see if any are worth pushing on to the stack. |
| 168 | +The default execution order is LIFO and the signature very simple. |
| 169 | + |
| 170 | +A Middleware must return an instance of Result and be callable. That's it! |
| 171 | + |
| 172 | +An example Middleware could be as simple as this: |
| 173 | + |
| 174 | +```php |
| 175 | +$cachingMiddleware = function (Query $query, callable $next) use ($queryCacher) : Result { |
| 176 | + if ($query instanceof CacheableQuery) { |
| 177 | + return $queryCacher->get($query, function () use ($next, $query) { return $next($query); }); |
| 178 | + } |
| 179 | + |
| 180 | + return $next($query); |
| 181 | +}; |
| 182 | +``` |
| 183 | + |
| 184 | +### Future Future Development |
| 185 | + |
| 186 | +- Result Filtering (should be done at a query level, but would be nice to be able to specify sparse field sets |
0 commit comments