Giter Site home page Giter Site logo

broker's Introduction

Northwoods Broker

Become a Supporter Latest Stable Version License Build Status Code Coverage Scrutinizer Code Quality

Broker is a PSR-15 middleware dispatcher that uses conditional matching. As opposed to most middleware dispatcher, Broker verifies each middleware can be applied to the request before executing it. This allows for a centralized configuration of middleware without compromising flexibility.

Attempts to be PSR-1, PSR-2, PSR-4, and PSR-15 compliant.

Install

composer require northwoods/broker

Usage

Broker works best when combined with a PSR-11 container. When a container is provided middleware can be added by class name. Without the container middleware must be fully constructed.

use Northwoods\Broker\Broker;

$broker = new Broker($container);

Broker operates on conditions. The middleware will not be executed unless the condition returns true. Because some middleware is not conditional the always() method makes it easy to add middleware that must be executed on every request. The when() method accepts a condition and a list of middleware to execute if the condition passes. All middleware is executed in the order added.

// Always execute these middleware
$broker->always([
    Acme\HandleErrors::class,
    Acme\DetectClientIp::class,
]);

// Do we need to redirect to a secure connection?
$broker->when(
    function (ServerRequestInterface $request) {
        $scheme = $request->getUri()->getScheme();
        return $scheme !== 'https';
    },
    Acme\RedirectHttps::class,
);

// Add more middleware to be executed
$broker->always([
    Acme\ParseBody::class,
    Acme\HandleRequest::class,
]);

To dispatch the middleware call the handle() method with a request instance and a callable $default that returns the response, typically a 404 Not Found, if no middleware can handle the request.

// Define the callable to create a 404 response
$default = [Acme\ResponseFactor::class, 'notFound'];

// Execute the middleware as a dispatcher
$response = $broker->handle($request, $default);

Broker also implements MiddlewareInterface and can be used as a middleware:

// Execute the broker like any other PSR-15 middleware
$response = $broker->process($request, $delegate);

License

MIT

broker's People

Contributors

shadowhand avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.