fyre / middleware
A middleware library.
Requires
- fyre/container: ^1.0
- fyre/server: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^11
README
FyreMiddleware is a free, open-source middleware library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/middleware
In PHP:
use Fyre\Middleware\MiddlewareRegistry;
Basic Usage
$container
is a Container.
$middlewareRegistry = new MiddlewareRegistry($container);
It is recommended to bind the MiddlewareRegistry to the Container as a singleton.
$container->singleton(MiddlewareRegistry::class);
Any dependencies will be injected automatically when loading from the Container.
$middlewareRegistry = $container->use(MiddlewareRegistry::class);
Methods
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.$arguments
is an array containing additional arguments for creating the Middleware, and will default to [].
$middlewareRegistry->map($alias, $middleware, $arguments);
Resolve
Resolve Middleware.
$middleware
is a Middleware class instance, class name, alias or Closure.
$resolvedMiddleware = $middlewareRegistry->resolve($middleware);
You can pass additional arguments to the handle
method of the Middleware by appending a colon followed by a comma-separated list of arguments to the string.
$middlewareRegistry->resolve('alias:arg1,arg2');
Middleware dependencies will be resolved automatically from the Container.
Use
Load a shared Middleware instance.
$alias
is a string representing the middleware alias.
$middleware = $middlewareRegistry->use($alias);
Middleware dependencies will be resolved automatically from the Container.
Middleware Queues
use Fyre\Middleware\MiddlewareQueue;
$middlewares
is an array containing the Middleware.
$queue = new MiddlewareQueue($middlewares);
Add
Add Middleware.
$middleware
is a Middleware class instance, class name name, alias or Closure.
$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, class name name, alias or Closure.
$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, class name name, alias or Closure.
$queue->prepend($middleware);
Rewind
Reset the index.
$queue->rewind();
Valid
Determine whether 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.
Handle
Handle a ServerRequest.
$request
is a ServerRequest.$next
is a Closure.
$response = $middleware->handle($request, $next);
This method should call the $next
callback with the $request
, to handle the next middleware in the queue, then return the ClientResponse.
Closures
You can also provide custom middleware as a simple Closure.
$middleware = function(ServerRequest $request, Closure $next): ClientResponse { return $next($request); };
Request Handlers
use Fyre\Middleware\RequestHandler;
$container
is a Container.$middlewareRegistry
is a MiddlewareRegistry.$queue
is a MiddlewareQueue.$initialResponse
is a ClientResponse to be used as the initial response, and will default to null.
$handler = new RequestHandler($container, $middlewareRegistry, $queue, $initialResponse);
Any dependencies will be injected automatically when loading from the Container.
$handler = $container->use(RequestHandler::class, 'queue' => $queue);
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.
The provided $request
will be automatically set as the ServerRequest instance in the Container.