fyre/middleware

A middleware library.

v2.1.4 2024-06-29 06:50 UTC

This package is auto-updated.

Last update: 2024-08-29 07:09:20 UTC


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.

$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.

$queue->insertAt($index, $middleware);

Key

Get the current index.

$key = $queue->key();

Next

Progress the index.

$queue->next();

Prepend

Prepend Middleware.

$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

$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;
$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.

$response = $handler->handle($request);

This method will return a ClientResponse.