Commit 2796e721 authored by Averor's avatar Averor

init, no tests [skip ci]

parents
Pipeline #28613645 skipped
cache:
paths:
- vendor/
before_script:
# Update container apt repo and install required packages
- apt-get update -yqq
- apt-get install -yqq git
# Install & enable Xdebug for code coverage reports
- pecl install xdebug
- docker-php-ext-enable xdebug
# Install and run Composer
- curl -sS https://getcomposer.org/installer | php
- php composer.phar update --no-interaction --prefer-source --prefer-stable
# Test @ PHP 7.1
test:7.1:
image: php:7.1
only:
- master
script: vendor/bin/phpunit --configuration phpunit.xml.dist --coverage-text --colors=never
# Test @ PHP 7.2
test:7.2:
image: php:7.2
only:
- master
script: vendor/bin/phpunit --configuration phpunit.xml.dist --coverage-text --colors=never
MIT License
Copyright (c) 2018
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Averor/Query-Bus
[![pipeline status](https://gitlab.com/Averor/query-bus/badges/master/pipeline.svg)](https://gitlab.com/Averor/query-bus/commits/master)
[![coverage report](https://gitlab.com/Averor/query-bus/badges/master/coverage.svg)](https://gitlab.com/Averor/query-bus/commits/master)
[![MIT License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://gitlab.com/Averor/query-bus/blob/master/LICENSE)
[![Semver](http://img.shields.io/SemVer/2.0.0-pre-alpha.png)](http://semver.org/spec/v2.0.0.html)
Simple [Averor/message-bus](https://gitlab.com/Averor/message-bus) extension aimed to help dealing with cqrs query part.
## Install using Composer:
`composer require averor/query-bus`
## Documentation
Will be surely written... someday. As for now - just take a look at [examples/](https://gitlab.com/Averor/query-bus/tree/master/examples)
or [tests/](https://gitlab.com/Averor/query-bus/tree/master/tests)
### QueryBus
_MessageBus_ is not able to return value from handler, so that's where _QueryBus_ comes to the rescue.
It uses some contracts from _MessageBus_ but **middlewares are not interchangeable**.
> \Averor\MessageBus\QueryBus\Contract\QueryBusMiddleware
introduced to assure that no base _MessageBus_ middleware would be registered.
## Testing with PHPUnit (>=7.0)
`$ ./vendor/bin/phpunit ./tests`
{
"name": "averor/query-bus",
"type": "library",
"description": "Message Bus extension aimed to help dealing with cqrs query part",
"license": "MIT",
"keywords": ["message bus", "query", "query bus"],
"minimum-stability": "dev",
"authors": [
{
"name": "Averor",
"homepage": "https://gitlab.com/Averor"
}
],
"require": {
"php": ">=7.1",
"averor/message-bus": "^2.0"
},
"require-dev": {
"averor/simple-logger": "*",
"phpunit/phpunit": "^7.0",
"psr/log": "^1.0"
},
"autoload": {
"psr-4": {
"Averor\\MessageBus\\QueryBus\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Averor\\MessageBus\\QueryBus\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.0-dev"
}
}
}
<?php declare(strict_types=1);
use Averor\MessageBus\QueryBus\Contract\Query;
use Averor\MessageBus\QueryBus\Contract\QueryHandler;
use Averor\MessageBus\QueryBus\Middleware\LoggingMiddleware;
use Averor\MessageBus\QueryBus\Middleware\QueryDispatchingMiddleware;
use Averor\MessageBus\QueryBus\QueryBus;
use Averor\MessageBus\Util\CallableMapResolver;
use Averor\SimpleLogger\Logger;
require __DIR__ . '/../vendor/autoload.php';
class SomeQuery implements Query
{
public $id, $name;
public function __construct(int $id, string $name)
{
$this->id = $id;
$this->name = $name;
}
}
class SomeQueryHandler implements QueryHandler
{
public function __invoke(SomeQuery $query)
{
return sprintf(
'Some result retrieved for query of id: %d and name: %s',
$query->id,
$query->name
);
}
}
$map = [
SomeQuery::class => new SomeQueryHandler()
];
$bus = new QueryBus([
new QueryDispatchingMiddleware(
new CallableMapResolver($map)
),
new LoggingMiddleware(
new Logger('php://output')
)
]);
$result = $bus->dispatch(
new SomeQuery($id=34, $name='foo')
);
printf("Query (%d:%s) result: %s\n", $id, $name, $result);
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>
<testsuites>
<testsuite name="averor/query-bus :: Middleware test suite">
<directory>tests/Middleware</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Contract;
use Averor\MessageBus\Contract\Message;
/**
* Interface Query
*
* @package Averor\MessageBus\QueryBus\Contract
* @author Averor <averor.dev@gmail.com>
*/
interface Query extends Message
{
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Contract;
/**
* Interface QueryBusMiddleware
*
* @package Averor\MessageBus\QueryBus\Contract
* @author Averor <averor.dev@gmail.com>
*/
interface QueryBusMiddleware
{
/**
* @param Query $message
* @param callable $next
* @return mixed
*/
public function execute(Query $message, callable $next);
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Contract;
/**
* Interface QueryDispatcher
*
* @package Averor\MessageBus\QueryBus\Contract
* @author Averor <averor.dev@gmail.com>
*/
interface QueryDispatcher extends QueryBusMiddleware
{
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Contract;
/**
* Interface QueryHandler
*
* @package Averor\MessageBus\QueryBus\Contract
* @author Averor <averor.dev@gmail.com>
*/
interface QueryHandler
{
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Middleware;
use Averor\MessageBus\QueryBus\Contract\Query;
use Averor\MessageBus\QueryBus\Contract\QueryBusMiddleware;
use Psr\Log\LoggerInterface;
/**
* Class LoggingMiddleware
*
* @package Averor\MessageBus\QueryBus\Middleware
* @author Averor <averor.dev@gmail.com>
*/
class LoggingMiddleware implements QueryBusMiddleware
{
/** @var LoggerInterface */
protected $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function execute(Query $query, callable $next)
{
$this->logger->info(sprintf(
"Dispatching query %s",
get_class($query)
));
$result = $next($query);
$this->logger->info(sprintf(
"Query %s dispatched",
get_class($query)
));
return $result;
}
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Middleware;
use Averor\MessageBus\Contract\OneToOneResolver;
use Averor\MessageBus\QueryBus\Contract\Query;
use Averor\MessageBus\QueryBus\Contract\QueryDispatcher;
/**
* Class QueryDispatchingMiddleware
*
* @package Averor\MessageBus\QueryBus\Middleware
* @author Averor <averor.dev@gmail.com>
*/
class QueryDispatchingMiddleware implements QueryDispatcher
{
/** @var OneToOneResolver */
protected $resolver;
public function __construct(OneToOneResolver $resolver)
{
$this->resolver = $resolver;
}
public function execute(Query $query, callable $next)
{
/** @var Callable $handler */
$handler = $this->resolver->resolve($query);
if ($this->resolver instanceof OneToOneResolver && is_callable($handler)) {
$result = $handler($query);
}
$next($query);
return $result ?? null;
}
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus;
use Averor\MessageBus\Contract\Message;
use Averor\MessageBus\QueryBus\Contract\QueryBusMiddleware;
/**
* Class QueryBus
*
* @package Averor\MessageBus\QueryBus
* @author Averor <averor.dev@gmail.com>
*/
class QueryBus
{
protected $middlewares;
public function __construct(array $middlewares = [])
{
foreach (array_reverse($middlewares) as $middleware) {
$this->prependMiddleware($middleware);
}
}
public function appendMiddleware(QueryBusMiddleware $middleware)
{
array_unshift($this->middlewares, $middleware);
}
public function prependMiddleware(QueryBusMiddleware $middleware)
{
$this->middlewares[] = $middleware;
}
public function dispatch(Message $message)
{
return call_user_func($this->execute(), $message);
}
public function execute()
{
$next = function () {};
reset($this->middlewares);
while ($middleware = current($this->middlewares)) {
$next = function ($message) use ($middleware, $next) {
return $middleware->execute($message, $next);
};
next($this->middlewares);
}
return $next;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment