fyre / middleware
A middleware library.
Requires
- fyre/server: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^10
README
FyreMiddleware is a free, open-source middleware library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/middleware
Middleware Queues
use Fyre\Middleware\MiddlewareQueue;
$queue = new MiddlewareQueue();
Add
Add Middleware.
$middleware
is a Middleware class instance or name, Closure or a MiddlewareRegistry alias.
$queue->add($middleware);
Count
Get the Middleware count.
$count = $queue->count();
Current
Get the Middleware at the current index.
$middleware = $queue->current();
Insert At
Insert Middleware at a specified index.
$index
is a number representing the index.$middleware
is a Middleware class instance or name, Closure or a MiddlewareRegistry alias.
$queue->insertAt($index, $middleware);
Key
Get the current index.
$key = $queue->key();
Next
Progress the index.
$queue->next();
Prepend
Prepend Middleware.
$middleware
is a Middleware class instance or name, Closure or a MiddlewareRegistry alias.
$queue->prepend($middleware);
Rewind
Reset the index.
$queue->rewind();
Valid
Determine if the current index is valid.
$valid = $queue->valid();
Middleware
Custom middleware can be created by extending \Fyre\Middleware\Middleware
, ensuring all below methods are implemented.
Process
$request
is a ServerRequest.$handler
is a RequestHandler.
$response = $middleware->process($request, $handler);
This method will return a ClientResponse.
The implemented method should call the handle
method of the RequestHandler to handle the next middleware in the queue.
Middleware Registry
use Fyre\Middleware\MiddlewareRegistry;
Clear
Clear all aliases and middleware.
MiddlewareRegistry::clear();
Map
Map an alias to middleware.
$alias
is a string representing the middleware alias.$middleware
is a string representing the Middleware class name, or a closure that returns an instance of a Middleware class.
MiddlewareRegistry::map($alias, $middleware);
Resolve
Resolve Middleware.
$middleware
is a Middleware class instance or name, Closure or a MiddlewareRegistry alias.
$resolvedMiddleware = MiddlewareRegistry::resolve($middleware);
Use
Load a shared Middleware instance.
$alias
is a string representing the middleware alias.
$middleware = MiddlewareRegistry::use($alias);
Request Handlers
use Fyre\Middleware\RequestHandler;
$queue
is a MiddlewareQueue.$initialResponse
is a ClientResponse to be used as the initial response, and will default to null.
$handler = new RequestHandler($queue, $initialResponse);
If the $initialResponse
is set to null, a new ClientResponse will be created.
Handle
Handle the next middleware in the queue.
$request
is a ServerRequest.
$response = $handler->handle($request);
This method will return a ClientResponse.