Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/makise-co/middleware
Provides PSR-15 Middleware Dispatcher implementation
https://github.com/makise-co/middleware
Last synced: about 4 hours ago
JSON representation
Provides PSR-15 Middleware Dispatcher implementation
- Host: GitHub
- URL: https://github.com/makise-co/middleware
- Owner: makise-co
- License: mit
- Created: 2020-10-10T15:25:40.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2020-10-14T23:57:52.000Z (about 4 years ago)
- Last Synced: 2024-08-03T23:23:48.371Z (3 months ago)
- Language: PHP
- Homepage:
- Size: 18.6 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-psr15-middlewares - makise-co/middleware - A high performance strict typed PSR-15 dispatcher that supports middleware resolution though PSR container. Optimized for long-running applications (like Swoole, ReactPHP, AmPHP). (Packages / Dispatcher)
README
# PSR-15 Request dispatcher
This package provides a two high performance implementations of the PSR-15 request dispatcher
* Dispatcher - Flat List implementationThis is a centralized architecture, the Dispatcher acts as the coordinator.
Middleware receives the Dispatcher instance as the request handler.
And the Dispatcher knows which next Middleware needs to be called.
It works like:
* Dispatcher->handle($request) ->
* Middleware1->process($request, Dispatcher) ->
* Dispatcher->handle($request) ->
* Middleware2->process($request, Dispatcher) ->
* Dispatcher->handle($request) ->
* RequestHandler->handle($request)* MiddlewarePipe - Linked List implementation (harder to understand, but works a bit faster)
This is a decentralized architecture, each pipeline acts as a request handler.
MiddlewarePipe is a wrapper over a middleware or a request handler.
It works like:
* $pipeline->handle($request) ->
* Middleware1->process($request, $nextPipeline) ->
* $nextPipeline->handle($request) ->
* Middleware2->process($request, $nextPipeline) ->
* $nextPipeline->handle($request) ->
* RequestHandler->handle($request)## Requirements
* PHP >= 7.4## Installation
`composer require makise-co/middleware`## Benchmarks
10000 calls:
```
Makise time took: 0.011235 secs (0.00000112 secs per request)
Makise (flat) time took: 0.011854 secs (0.00000119 secs per request)
Laminas time took: 0.034769 secs (0.00000348 secs per request)
Relay time took: 0.021777 secs (0.00000218 secs per request)
```1 million calls:
```
Makise time took: 1.077460 secs (0.00000108 secs per request)
Makise (flat) time took: 1.122735 secs (0.00000112 secs per request)
Laminas time took: 2.027957 secs (0.00000203 secs per request)
Relay time took: 1.482862 secs (0.00000148 secs per request)
```* Laminas version used: 3.2.2
* Relay version used: 2.1.1Benchmark code can be found [here](benchmark/bench.php).
* Benchmarks were performed on PHP 7.4 with OPcache enabled
* CPU: Intel Core i7-9750H 6 cores (CPU frequency during benchmarks: 4.07 GHz)
* OS: Ubuntu 20.04 (WSL 2)## Usage
### Dispatcher (Flat List)
```php
create([Middleware1::class, Middleware2::class, RequestHandler::class]);$response = $dispatcher->handle($request);
```
### MiddlewarePipe (Linked List)
#### Creating middleware pipeline
```php
create([$middleware1, $middleware2, $requestHanlder]);// or you can use MiddlewareResolver with PsrContainer implementation to resolve middlewares
$factory = new MiddlewarePipeFactory(new MiddlewareResolver($container));
$pipeline = $factory->create([Middleware1::class, MIddleware2::class, RequestHandler::class]);$response = $pipeline->handle($request);
```Each pipeline must end with Response producer, otherwise pipeline will fail with RuntimeException (Empty handler)
#### Merging pipelines
```php
create([$middleware1_1, $middleware1_2]);
$pipeline = $factory->create([$middleware1, $subPipeline, $middleware2, $requestHanlder]);
```Execution order will be: middleware1 -> middleware1_1 -> middleware1_2 -> middleware2 -> requestHandler
#### Adding error handling middleware
According to the [PSR-15](https://www.php-fig.org/psr/psr-15/#14-handling-exceptions) error handling middleware must be a first middleware in a pipeline.```php
responseFactory = $responseFactory;
$this->logger = $logger;
$this->debug = $debug;
}public function handle(Throwable $e, ServerRequestInterface $request): ResponseInterface
{
// TODO: Write own error handler
$this->logger->error(...);if ($this->debug) {
// TODO: Create detailed error response
}
return $this->responseFactory->createResponse(500);
}
}$errorHandlingMiddleware = new ErrorHandlingMiddleware(
new HttpErrorHandler($responseFactory, $logger, true)
);$factory = new MiddlewarePipeFactory();
$pipeline = $factory->create([
$errorHandlingMiddleware,
$middleware1,
$middleware2,
$requestHandler,
// ...
]);$response = $pipeline->handle($request);
```