https://github.com/gpslab/middleware
Infrastructure for use middleware in applications
https://github.com/gpslab/middleware
cqrs domain-event infrastructure middleware php psr-11 psr-3 symfony
Last synced: 12 months ago
JSON representation
Infrastructure for use middleware in applications
- Host: GitHub
- URL: https://github.com/gpslab/middleware
- Owner: gpslab
- License: mit
- Created: 2017-05-24T16:19:54.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-17T12:06:51.000Z (over 8 years ago)
- Last Synced: 2025-03-26T20:21:18.642Z (12 months ago)
- Topics: cqrs, domain-event, infrastructure, middleware, php, psr-11, psr-3, symfony
- Language: PHP
- Size: 70.3 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/gpslab/middleware)
[](https://packagist.org/packages/gpslab/middleware)
[](https://travis-ci.org/gpslab/middleware)
[](https://coveralls.io/github/gpslab/middleware?branch=master)
[](https://scrutinizer-ci.com/g/gpslab/middleware/?branch=master)
[](https://insight.sensiolabs.com/projects/ed9115e0-283f-4799-993c-3777a044114d)
[](https://styleci.io/repos/92312680)
[](https://github.com/gpslab/middleware)
# Infrastructure for use middleware in applications

## Installation
Pretty simple with [Composer](http://packagist.org), run:
```sh
composer require gpslab/middleware
```
## Middleware chain
`MiddlewareChain` contains a middlewares (`Middleware`) and sequentially apply them to the message by chain.
There are 3 implementations of the chain, but you can make your own.
* `DirectBindingMiddlewareChain` - direct binding;
* `ContainerMiddlewareChain` - [PSR-11](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container.md) container;
* `SymfonyContainerMiddlewareChain` - Symfony container *(Symfony 3.3
[implements](http://symfony.com/blog/new-in-symfony-3-3-psr-11-containers) a
[PSR-11](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container.md))*.
## Handle command (CQRS)
Example usage middleware for handle Commands in [CQRS](https://github.com/gpslab/cqrs).
```php
// middleware chain
$chain = new DirectBindingMiddlewareChain();
// add logger middleware
$chain->append(new LoggerMiddleware($logger));
// add validator middleware
$chain->append(new ValidatorMiddleware($validator));
// add middleware for handle command from origin command bus
$chain->append(new CommandMiddleware($command_bus));
// configure command bus
$bus = new MiddlewareCommandBus($chain);
// handle command
try {
$bus->handle($my_command);
} catch(InvalidMessageException $e) {
// show validation errors
var_dump($e->getMessages());
}
```
## Handle query (CQRS)
Example usage middleware for handle Queries in [CQRS](https://github.com/gpslab/cqrs).
```php
// middleware chain
$chain = new DirectBindingMiddlewareChain();
// add logger middleware
$chain->append(new LoggerMiddleware($logger));
// add validator middleware
$chain->append(new ValidatorMiddleware($validator));
// add middleware for handle query from origin query bus
$chain->append(new QueryMiddleware($query_bus));
// configure query bus
$bus = new MiddlewareQueryBus($chain);
// handle query
try {
$bus->handle($my_query);
} catch (InvalidMessageException $e) {
// show validation errors
var_dump($e->getMessages());
}
```
## Handle Domain event
Example usage middleware for handle [domain events](https://github.com/gpslab/domain-event).
```php
// middleware chain
$chain = new DirectBindingMiddlewareChain();
// add logger middleware
$chain->append(new LoggerMiddleware($logger));
// add middleware for handle event from origin domain event bus
$chain->append(new DomainEventMiddleware($domain_event_bus));
// configure domain event bus
$bus = new MiddlewareDomainEventBus($chain);
// publish domain event
$bus->publish($my_event);
```
## License
This bundle is under the [MIT license](http://opensource.org/licenses/MIT). See the complete license in the file: LICENSE