-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release V0.1 Finished the initial v0.1 package code. See merge request !1
- Loading branch information
Showing
22 changed files
with
1,382 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,38 @@ | ||
# Mw Http | ||
|
||
Middlewares written for http applications | ||
Middlewares written for http applications | ||
|
||
## Installation | ||
|
||
Install via composer at `krak/mw-http` | ||
|
||
## Usage | ||
|
||
```php | ||
<?php | ||
|
||
use Krak\Mw\Http; | ||
|
||
$app = new Http\App(); | ||
$app->get('/', function($req) { | ||
return "Hello World!"; | ||
}); | ||
$app->with(Http\Package\std()); | ||
$app->serve(); | ||
``` | ||
|
||
More documentation coming soon! For now look over the examples and source code for information. | ||
|
||
## Packages | ||
|
||
Packages are a way to extend an App. They can be as simple as adding a middleware to as complex as mounting an entire application on top of the app. | ||
|
||
### Standard | ||
|
||
### Pimple | ||
|
||
### Rest | ||
|
||
### Plates | ||
|
||
### Create Your Own |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "krak/mw-http", | ||
"description": "Http Middlewares and Application", | ||
"type": "library", | ||
"license": "MIT", | ||
"homepage": "https://gitlab.bighead.net/krak-mw/http", | ||
"keywords": [ | ||
"middleware", | ||
"framework", | ||
"psr-7", | ||
"http", | ||
"micro" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "RJ Garcia", | ||
"email": "rj@bighead.net" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Krak\\Mw\\Http\\": "src" | ||
}, | ||
"files": ["src/inc.php"] | ||
}, | ||
"require": { | ||
"krak/mw": "^0.2.0", | ||
"nikic/fast-route": "^1.0", | ||
"nikic/iter": "^1.4", | ||
"zendframework/zend-diactoros": "^1.3" | ||
}, | ||
"require-dev": { | ||
"guzzlehttp/psr7": "^1.3", | ||
"league/plates": "^3.1", | ||
"peridot-php/peridot": "^1.18", | ||
"pimple/pimple": "^3.0", | ||
"symfony/var-dumper": "^3.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
use Krak\Mw\Http; | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
class Controller { | ||
public function getAction($req, $params) { | ||
$c = $req->getAttribute('pimple'); | ||
return [200, [1,2,3, $c['param'], (int) $params['num']]]; | ||
} | ||
} | ||
|
||
$c = new Pimple\Container(); | ||
$c['controller'] = function() { | ||
return new Controller(); | ||
}; | ||
$c['param'] = 4; | ||
|
||
$plates = new League\Plates\Engine(__DIR__ . '/views', 'phtml'); | ||
|
||
$app = new Http\App(); | ||
$app->get('/', function($req) { | ||
return [ | ||
'a' => 1, | ||
'x' => $req->getAttribute('x'), | ||
]; | ||
})->push(function($req, $next) { | ||
return $next($req->withAttribute('x', 'y')); | ||
}); | ||
$app->get('/bad', function() { | ||
throw new Exception('Bad!'); | ||
}); | ||
$app->get('/controller/{num}', 'controller@getAction'); | ||
$app->with(Http\Package\std()); | ||
$app->with(Http\Package\rest(null, JSON_PRETTY_PRINT)); | ||
$app->with(Http\Package\pimple($c)); | ||
|
||
$app1 = new Http\App(); | ||
$app1->with(Http\Package\std()); | ||
$app1->with(Http\Package\plates($plates)); | ||
|
||
$app1->get('/', function() { | ||
return 'Hi!'; | ||
}); | ||
$app1->get('/a', function() { | ||
return 'a!'; | ||
}); | ||
$app1->get('/view', function() { | ||
return ['test', ['var' => 'Hello World!']]; | ||
}); | ||
$app1->get('/view-http', function() { | ||
return [ | ||
201, | ||
['test', ['var' => 'Check the http status']] | ||
]; | ||
}); | ||
$app1->get('/ex', function() { | ||
throw new \Exception('Somthing Bad Happened!'); | ||
}); | ||
$app->mws()->push( | ||
http\mount('/admin', $app1) | ||
); | ||
$app->serve(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
$serve = Krak\Mw\Http\server(); | ||
$serve(function($req) { | ||
return new GuzzleHttp\Response(200, [], 'Hello World!'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php $this->layout('layout') ?> | ||
<h1>500 Error</h1> | ||
<pre> | ||
<?=$exception?> | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head></head> | ||
<body> | ||
<?=$this->section('content')?> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Test</h1> | ||
<p><?=$var?></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Krak\Mw\Http\Package; | ||
|
||
use Krak\Mw\Http, | ||
Pimple\Container; | ||
|
||
class PimplePackage extends Http\AbstractPackage | ||
{ | ||
private $container; | ||
private $config; | ||
|
||
public function __construct(Container $container, $config = []) { | ||
$this->container = $container; | ||
$this->config = [ | ||
'prefix' => '', | ||
'method_sep' => '@', | ||
'name' => 'pimple', | ||
] + $config; | ||
} | ||
|
||
public function with(Http\App $app) { | ||
$app->invokeAction()->push( | ||
pimpleInvokeAction($this->container, $this->config['prefix'], $this->config['method_sep']) | ||
); | ||
$app->mws()->push(injectPimple($this->container, $this->config['name'])); | ||
} | ||
} | ||
|
||
function pimple(...$args) { | ||
return new PimplePackage(...$args); | ||
} | ||
|
||
/** injects pimple into the request */ | ||
function injectPimple(Container $container, $name = 'pimple') { | ||
return function($req, $next) use ($container, $name) { | ||
return $next($req->withAttribute($name, $container)); | ||
}; | ||
} | ||
|
||
/** try to get the action out of a pimple container and then delegate the calling to the next | ||
invoker. If you pass in null for $method_sep, then it won't try and split the method from | ||
the action string. | ||
class Controller { | ||
public function getIndexAction($req) { | ||
return 'response'; | ||
} | ||
} | ||
$container['namespace.prefix.controller'] = function() { | ||
return new Controller(); | ||
}; | ||
$invoke = pimpleInvokeAction( | ||
callableInvokeAction(), | ||
$container, | ||
'namespace.prefix.', | ||
); | ||
assert($invoke($req, 'controller@getIndexAction', []) == 'response'); | ||
*/ | ||
function pimpleInvokeAction(\Pimple\Container $app, $prefix = '', $method_sep = '@') { | ||
return function($req, $action, $params, $next) use ($app, $prefix, $method_sep) { | ||
if (!is_string($action)) { | ||
return $next($req, $action, $params); | ||
} | ||
|
||
if (isset($app[$prefix . $action])) { | ||
return $next($req, $app[$prefix . $action], $params); | ||
} | ||
if ($method_sep && strpos($action, $method_sep) !== false) { | ||
list($controller, $method) = explode($method_sep, $action); | ||
return $next( | ||
$req, | ||
[$app[$prefix . $controller], $method], | ||
$params | ||
); | ||
} | ||
|
||
return $next($req, $action, $params); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Krak\Mw\Http\Package; | ||
|
||
use Krak\Mw\Http, | ||
League\Plates; | ||
|
||
class PlatesPackage extends Http\AbstractPackage | ||
{ | ||
private $plates; | ||
private $config; | ||
|
||
public function __construct(Plates\Engine $plates, $config = []) { | ||
$this->plates = $plates; | ||
$this->config = [ | ||
'name' => 'plates', | ||
'error_paths' => [ | ||
'404' => 'errors/404', | ||
'500' => 'errors/500', | ||
] | ||
] + $config; | ||
} | ||
|
||
public function with(Http\App $app) { | ||
$rf = $app->responseFactory(); | ||
$app->notFoundHandler()->push(platesNotFoundHandler( | ||
$app->responseFactory(), | ||
$this->plates, | ||
$this->config['error_paths']['404'] | ||
)); | ||
$app->exceptionHandler()->push(platesExceptionHandler( | ||
$app->responseFactory(), | ||
$this->plates, | ||
$this->config['error_paths']['500'] | ||
)); | ||
$app->marshalResponse()->push(platesMarshalResponse($this->plates)); | ||
$app->mws()->push(injectPlates($this->plates, $this->config['name'])); | ||
} | ||
} | ||
|
||
function plates(...$args) { | ||
return new PlatesPackage(...$args); | ||
} | ||
|
||
/** injects pimple into the request */ | ||
function injectPlates(Plates\Engine $plates, $name = 'plates') { | ||
return function($req, $next) use ($plates, $name) { | ||
$plates->addData(['request' => $req]); | ||
return $next($req->withAttribute($name, $plates)); | ||
}; | ||
} | ||
|
||
/** Allows the returning of a 2-tuple of path and data */ | ||
function platesMarshalResponse(Plates\Engine $plates) { | ||
return function($result, $rf, $req, $next) use ($plates) { | ||
$matches = Http\isTuple($result, "string", "array"); | ||
if (!$matches) { | ||
return $next($result, $rf, $req, $next); | ||
} | ||
|
||
list($template, $data) = $result; | ||
return $rf(200, [], $plates->render($template, $data)); | ||
}; | ||
} | ||
|
||
function platesNotFoundHandler($rf, Plates\Engine $plates, $path) { | ||
return function($req, $result, $next) use ($rf, $plates, $path) { | ||
if (!$plates->exists($path)) { | ||
return $next($req, $result); | ||
} | ||
|
||
return $rf(404, [], $plates->render($path, [ | ||
'dispatch_result' => $result, | ||
])); | ||
}; | ||
} | ||
function platesExceptionHandler($rf, Plates\Engine $plates, $path) { | ||
return function($req, $ex, $next) use ($rf, $plates, $path) { | ||
if (!$plates->exists($path)) { | ||
return $next($req, $ex); | ||
} | ||
|
||
return $rf(500, [], $plates->render($path, [ | ||
'exception' => $ex, | ||
])); | ||
}; | ||
} |
Oops, something went wrong.