2017 © Pedro Peláez
 

library middleware

image

adagio/middleware

  • Thursday, March 16, 2017
  • by Keven
  • Repository
  • 1 Watchers
  • 1 Stars
  • 16 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

adagio/middleware

Latest Stable Version Build Status License Total Downloads, (*1)

adagio/middleware library allows to implement middlewares with various data types easily., (*2)

Installation

Install Composer and run the following command to get the latest version:, (*3)

composer require adagio/middleware

Quick start

todo., (*4)

Middlewares principles

  • A middleware signature MUST include the input data, the output data to hydrate and the next middleware to call.
  • A middleware MUST return a valid output data.
  • A middle ware CAN process data before or after calling the next midleware.
  • The very last middleware "hidden in the stack" just returns the output data.

Transition to middlewares

Imagine you want to add a middleware pipeline to an existing image-editing library., (*5)

Here is the way you can do it without middlewares:, (*6)

// I want to solarize, rotate, unblur and then sepia my image (parameters are 
// voluntarily omitted for clarity).
$image = (new SepiaFilter)
    ->filter((new UnblurFilter)
    ->filter((new RotateFilter)
    ->filter((new SolarizedFilter)
    ->filter(new Image('/path/to/image')))));

Problems are:, (*7)

  • you have to declare the pipeline backward
  • big parenthesis mess
  • you cannot declare a pipeline to use on other images later

With adagio/middleware, you can do it easily:, (*8)

use Adagio\Middleware\Stack;

$pipe = new Stack([
    new SolarizedFilter,
    new RotateFilter,
    new UnblurFilter,
    new SepiaFilter,
]);

$image = $stack(new Image('/path/to/image'));

Filters have just to respect the following signature convention:, (*9)

function (Image $image, callable $next): Image
{
    // Maybe do something with $image

    $resultingImage = $next($image);

    // Maybe do something with $resultingImage

    return $resultingImage;
}

Each filter must pass the $image to the $next element of the pipe and can modify it before of after passing it., (*10)

Filters can be any callable respecting the given signature., (*11)

More complex example: DB query processing

Middlewares are even more useful when the given and the returned objects are different. Think about a SQL query processor with the following signature:, (*12)

function (SqlQuery $query, ResultSet $resultSet, callable $next): ResultSet

You can then provide a caching middleware:, (*13)

final class QueryCache
{
    // ...

    public function __invoke(SqlQuery $query, ResultSet $resultSet, callable $next): ResultSet
    {
        // If the query is already in cache, return the ResultSet and don't 
        // trigger the rest of the middleware stack
        if ($this->resultSetCache->hasQuery($query)) {
            return $this->resultSetCache->getFromQuery($query);
        }

        $finalResultSet = $next($query, $resultSet);

        $this->resultSetCache->add($query, $finalResultSet);

        return $finalResultSet;
    }
}

You can also provide a middleware that translates from a SQL standard to another, a SQL validator, a client-side cluster/shard solution, a logger, a performance monitor, ..., (*14)

The Versions

16/03 2017

dev-master

9999999-dev

  Sources   Download

The Development Requires

16/03 2017

0.1.0

0.1.0.0

  Sources   Download

The Development Requires