https://github.com/northwoods/conditional-middleware
Middleware proxy for request condition checks
https://github.com/northwoods/conditional-middleware
conditional middleware proxy psr-15
Last synced: 22 days ago
JSON representation
Middleware proxy for request condition checks
- Host: GitHub
- URL: https://github.com/northwoods/conditional-middleware
- Owner: northwoods
- License: mit
- Created: 2018-10-17T14:21:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-17T14:35:36.000Z (over 7 years ago)
- Last Synced: 2025-10-10T20:09:52.210Z (4 months ago)
- Topics: conditional, middleware, proxy, psr-15
- Language: PHP
- Homepage:
- Size: 5.86 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Northwoods Conditional Middleware
[](https://travis-ci.com/northwoods/conditional-middleware)
[](https://scrutinizer-ci.com/g/northwoods/conditional-middleware/?branch=master)
[](https://scrutinizer-ci.com/g/northwoods/conditional-middleware/?branch=master)
[](https://packagist.org/packages/northwoods/conditional-middleware)
[](https://packagist.org/packages/northwoods/conditional-middleware)
[](https://packagist.org/packages/northwoods/conditional-middleware)
Middleware proxy that executes a middleware based on request conditions.
## Installation
The best way to install and use this package is with [composer](http://getcomposer.org/):
```shell
composer require northwoods/conditional-middleware
```
## Usage
```php
use Northwoods\Middleware\ConditionalMiddleware;
/** @var \Psr\Http\Server\MiddlewareInterface */
$actual = /* any existing middleware */
$middleware = new ConditionalMiddleware($actual, function (Request $request): bool {
return $request->getHeaderLine('accept') === 'application/json';
});
```
In this example, the wrapped `$actual` middleware will only be executed if the
request accepts the `application/json` content type.
### Condition Callable
The condition callable should use the following signature:
```php
function (Request $request): bool;
```
The condition must return `true` (by strict `===` comparison) for the wrapped
middleware to be executed. If the condition check fails the handler will be
called immediately.