Middleware: Run queued processes using the middleware pattern

Recommend this page to a friend!
  Info   Documentation   View files (5)   Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 167 All time: 8,649 This week: 362
Version License PHP version Categories
middleware 1.0.1MIT/X Consortium ...5.5PHP 5, Design Patterns
Description Author

This package can run queued processes using the middleware pattern.

A queue class can add middleware objects or closure functions to a queue to be executed on the first in, first out basis.

The queue class can take the middleware objects from the queue, call them and return the responses.

The middleware objects can execute the next object in the queue.

Innovation Award
PHP Programming Innovation award nominee
February 2016
Number 3


Prize: One ebook of choice by Packt
Middleware is a piece of software that connects different pieces of software to perform a certain action.

This package implements generic middle functionality for the process of queuing and processing a certain task using middleware objects and returning the results to the calling code.

Manuel Lemos
Picture of Jeremiah Ogbomo
  Performance   Level  
Name: Jeremiah Ogbomo <contact>
Classes: 3 packages by
Country: Nigeria Nigeria
Age: 26
All time rank: 353022 in Nigeria Nigeria
Week rank: 694 15 in Nigeria Nigeria
Innovation award
Innovation award
Nominee: 2x

Details

Class Application {
  function __construct(){
    $this->request = [];
    $this->response = [];
    // or
    $this->request = new \stdClass;
    $this->response = new \stdClass;
  }
}

$application = new Application;

or

$application = new \stdClass()
$application->request = new \stdClass()
$application->response = new \stdClass()

$MiddlewareQueue = new MiddlewareQueue($application);

$MiddlewareQueue->add(function($req, $res, $next){
  echo 'A ';
  $next();
  echo 'A_2 ';
});
 
Class OneMiddleware extends Middleware {
  public function call(){
    echo 'B ';
    $this->next->call();
    echo 'B_2 ';
  }
}
$MiddlewareQueue->add(new OneMiddleware);

Class Two {
  public function call(){
    echo 'C ';
    $this->next->call();
    echo 'C_2 ';
  }
}
$MiddlewareQueue->add(new Two);

$MiddlewareQueue->add(function($req, $res, $next){
  echo 'D ->';
  $next();
  echo 'D_2 ';
});

$MiddlewareQueue->run();

RESULT:

A B C D -> D_2 C_2 B_2 A_2

  Files  
File Role Description
Middleware.php Class Middleware base class
MiddlewareInterface.php Class Middleware objects interface
MiddlewareObject.php Class Wrapper for middleware processes
MiddlewareQueue.php Class Queue object class
README.md Doc. A basic demonstration

 Version Control Unique User Downloads Download Rankings  
 0%
Total:167
This week:0
All time:8,649
This week:362

For more information send a message to info at phpclasses dot org.