dev-master
9999999-devGeneralized middleware implementation for PHP.
MIT
The Requires
- php >=5.3
The Development Requires
by Jan Mentzel
middleware php
Wallogit.com
2017 © Pedro Peláez
Generalized middleware implementation for PHP.
This is a generalized library for using middleware patterns within your PHP projects., (*1)
php-middleware is a PHP port of the ruby middleware library., (*2)
Only a subset of ruby middleware is implemented yet. Stay tuned to see more features ported., (*3)
To get started, the best place to look is the user guide., (*4)
This project is distributed as a composer package., (*6)
in your project root folder create a composer.json file, (*7)
{
"require": {
"hypercharge/php-middleware": "dev-master"
}
}
````
In a shell `cd` to your project root folder and run the command
```console
$ php composer.phar install
Below is a basic example of the library in use. If you don't understand what middleware is, please read the ruby middleware doc. This example is simply meant to give you a quick idea of what the library looks like., (*8)
<?php
// basic env instance class
class LogEnv {
public $log = array();
public function to_string() {
return join($this->log, "\n");
}
}
// Basic middleware that just logs the inbound and
// outbound steps to env
class Trace implements Middleware\Middleware {
private $app;
private $value;
public function __construct($app, $value) {
$this->app = $app;
$this->value = $value;
}
public function call($env) {
$env->log[] = '--> '.$this->value;
$this->app->call($env);
$env->log[] = '<-- '.$this->value;
}
}
// the env object passed to each middleware call($env) method
$env = new LogEnv();
// build the actual middleware stack which runs a sequence
// of slightly different versions of our middleware
$stack = new Middleware\Builder();
$stack
->uses('Trace', 'A')
->uses('Trace', 'B')
->uses('Trace', 'C');
// run it
$stack->call($env);
echo $env->to_string();
And the output:, (*9)
--> A --> B --> C <-- C <-- B <-- A
$ ./vendor/bin/phpunit
Generalized middleware implementation for PHP.
MIT
middleware php