Skip to content

Commit

Permalink
Total Refactor of Mw Library
Browse files Browse the repository at this point in the history
- Removed all http related code from the library
  to make this library a general purpose middleware
  library
- Only include functions that make sense for all types
  of middleware
- require php 5.6

Signed-off-by: RJ Garcia <rj@bighead.net>
  • Loading branch information
ragboyjr committed Nov 23, 2016
1 parent 63f4508 commit 965aed0
Show file tree
Hide file tree
Showing 11 changed files with 471 additions and 474 deletions.
91 changes: 47 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mw (Middleware)

The Mw library is a very flexible framework for handling requests.
The Mw library is a very flexible framework for converting middleware into handlers. Middleware offer a clean syntax for implementing the [Decorator Pattern](https://en.wikipedia.org/wiki/Decorator_pattern)

## Installation

Expand All @@ -13,68 +13,71 @@ Here's an example of basic usage of the mw library.
```php
<?php

use Krak\Mw,
Krak\HttpMessage\Match;
use Krak\Mw;

function injectAttribute($name, $value) {
return function($req, $next) use ($name, $value) {
return $next($req->withAttribute($name, $value));
function sum() {
return funciton($a, $b, $next) {
return $a + $b;
};
}

function show404($resp_factory) {
return function($req, $next) use ($resp_factory) {
return $resp_factory(404, ['Content-Type' => 'text/plain'], 'not found');
function modifyBy($value) {
return function($a, $b, $next) use ($value) {
return $next($a + $value, $b);
};
}

$rf = Mw\diactorosResponseFactory();
$json_rf = Mw\jsonResponseFactory($rf, JSON_PRETTY_PRINT);
$html_rf = Mw\htmlResponseFactory($rf);
$text_rf = Mw\textResponseFactory($rf);

$kernel = Mw\mwHttpKernel([
injectAttribute('x-attr', 'value'),
Mw\filter(show404($rf), function($req) { return $req->getUri()->getPath() == '/d'; }),
Mw\on('/a', function() use ($text_rf) {
return $text_rf(200, [], 'A Text Response');
}),
Mw\on('GET', '/b', function() use ($html_rf) {
return $html_rf(200, [], '<h1>An Html Response</h1>');
}),
function($req) use ($json_rf) {
return $json_rf(200, [], $req->getAttributes());
}
$sum = mw\compose([
sum(),
modifyBy(1),
]);

$app = Mw\diactorosApp();
$app($kernel);
$res = $sum(1, 2);
// $res = 4
```

For a more full featured example, look in the `example` directory.
The first value in the array is executed last; the last value is executed first.

### HttpKernel

The kernels are responsible for taking a `ServerRequestInterface` and returning a `ResponseInterface`.
```
1,2 -> modifyBy(1) -> 2,2 -> sum() -> 4 -> modifyBy(1) -> 4
```

### Middleware
Each middleware shares the same format:

A middleware is also responsible for returning a request into a response; however, they are designed to easily be chainable instead of having to use decoration to add functionality. Currently, middleware is only useful when using the `mwHttpKernel` which transforms a set of middleware into a kernel.
```
function($arg1, $arg2, ..., $next);
```

### Response Factory
A list of arguments, with a final argument $next which is the next middleware function to execute in the stack of middleware.

A response factory creates a response PSR7 response using any psr7 library you wish. Currently, we only support guzzle and diactoros, but you create your own simply or use the `defaultResponseFactory`, to always return a defualt resonse.
You need to have at least 1 argument and can have as many as you want. Every middleware needs to share the same signature. Composing a stack of middleware will return a handler which has the same signature as the middleware, but without the `$next` function.

The benefit of using the response factory comes from 3rd party middleware so that they don't have to be dependent on a psr7 library.
### Stack

### HttpApp
The library also comes with a MwStack that allows you to easily build a set of middleware.

The app component simply accepts a kernel and runs everything. The typical job of the http app is to generate a request, feed it to the kernel, and then emit the response.
```php
<?php

## Components
use Krak\Mw;

$stack = mw\stack();
$stack->push(function($a, $next) {
return $next($a . 'b');
})
->push(function($a, $next) {
return $next($a) . 'c';
})
// this goes on first
->unshift(function($a, $next) {
return $a;
}));

$handler = $stack->compose();
$res = $handler('a');
// $res = abc
```

This is just the core library for the middleware framework. There are a ton more components for things like http auth, routing, exception handling, REST Framework integration, Web Framework Integration, Symfony/Silex integration, CodeIgniter integration, and more!
## Api

- [Routing](https://gitlab.bighead.net/krak-mw/mw-routing)
- [JWT Authentication](https://gitlab.bighead.net/krak-mw/mw-jwt-auth)
- [CodeIgniter Integration](https://gitlab.bighead.net/krak-mw/mw-codeigniter)
Todo - finish api
12 changes: 4 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"homepage": "http://gitlab.bighead.net/krak/mw",
"keywords": [
"middleware",
"http",
"framework",
"psr7"
"framework"
],
"authors": [
{
Expand All @@ -17,13 +15,11 @@
}
],
"require": {
"krak/http-message": "^0.1.0",
"krak/mw-compose": "^0.1.0"
"nikic/iter": "^1.4",
"php": ">=5.6"
},
"require-dev": {
"guzzlehttp/psr7": "^1.3",
"peridot-php/peridot": "^1.18",
"zendframework/zend-diactoros": "^1.3"
"peridot-php/peridot": "^1.18"
},
"autoload": {
"psr-4": {
Expand Down
100 changes: 0 additions & 100 deletions example/diactoros-app.php

This file was deleted.

42 changes: 0 additions & 42 deletions example/readme.php

This file was deleted.

0 comments on commit 965aed0

Please sign in to comment.