-
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.
Merge pull request #21 from krakphp/19-serve-static-middleware
- Loading branch information
Showing
9 changed files
with
167 additions
and
12 deletions.
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
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
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,16 @@ | ||
<?php | ||
|
||
namespace Krak\Http\Concerns; | ||
|
||
use Krak\Http; | ||
|
||
trait Response | ||
{ | ||
public function response(...$args) { | ||
if (count($args) == 0) { | ||
return $this[Http\ResponseFactoryStore::class]; | ||
} | ||
|
||
return $this[Http\ResponseFactory::class]->createResponse(...$args); | ||
} | ||
} |
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,60 @@ | ||
<?php | ||
|
||
namespace Krak\Http; | ||
|
||
use Krak\Cargo; | ||
use Krak\Mw; | ||
use Zend\Diactoros; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class HttpServiceProvider implements Cargo\ServiceProvider | ||
{ | ||
public function register(Cargo\Container $c) { | ||
$c[ResponseFactory::class] = function() { | ||
return new ResponseFactory\DiactorosResponseFactory(); | ||
}; | ||
$c[ResponseFactoryStore::class] = function($c) { | ||
$store = new ResponseFactoryStore(); | ||
$rf = $c[ResponseFactory::class]; | ||
$store->store('json', new ResponseFactory\JsonResponseFactory( | ||
$rf, | ||
$c['krak.http.response_factory.json_encode_options'] | ||
)); | ||
$store->store('html', new ResponseFactory\HtmlResponseFactory($rf)); | ||
$store->store('text', new ResponseFactory\TextResponseFactory($rf)); | ||
|
||
return $store; | ||
}; | ||
$c[RouteCompiler::class] = function() { | ||
return new Http\Route\RecursiveRouteCompiler(); | ||
}; | ||
$c[DispatcherFactory::class] = function() { | ||
return new Dispatcher\FastRoute\FastRouteDispatcherFactory(); | ||
}; | ||
$c[Diactoros\Response\EmitterInterface::class] = function() { | ||
return new Diactoros\Response\SapiEmitter(); | ||
}; | ||
$c->factory(ServerRequestInterface::class, function() { | ||
return Diactoros\ServerRequestFactory::fromGlobals(); | ||
}); | ||
$c[Server::class] = function($app) { | ||
return new Server\DiactorosServer( | ||
$app[Diactoros\Response\EmitterInterface::class], | ||
function() use ($app) { | ||
return $app[ServerRequestInterface::class]; | ||
} | ||
); | ||
}; | ||
$c[Http\Route\RouteGroup::class] = function() { | ||
return new Http\Route\RouteGroup(''); | ||
}; | ||
$c['krak.http.response_factory.json_encode_options'] = 0; | ||
$c['krak.http.compose'] = function($c) { | ||
return Mw\composer( | ||
new Mw\Context\ContainerContext($c->toInterop()), | ||
Middleware\HttpLink::class | ||
); | ||
}; | ||
$c->alias(ServerRequestInterface::class, 'request'); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace Krak\Http\Middleware; | ||
|
||
use Krak\Mw; | ||
use Krak\Http; | ||
|
||
class HttpLink extends Mw\Link\ContainerLink | ||
{ | ||
use Http\Concerns\Response; | ||
} |
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
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
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 @@ | ||
<?php | ||
|
||
use Krak\Mw; | ||
use Krak\Http; | ||
|
||
beforeEach(function() { | ||
$this->container = Krak\Cargo\container(); | ||
$this->container->register(new Http\HttpServiceProvider()); | ||
}); | ||
describe('#serveStatic', function() { | ||
it('serves static files from a directory', function() { | ||
$serve_static = Http\Middleware\serveStatic(__DIR__ . '/resources'); | ||
$compose = $this->container['krak.http.compose']; | ||
|
||
$handler = $compose([ | ||
function($req, $next) { return $next->response(404); }, | ||
$serve_static | ||
]); | ||
|
||
$req = $this->container['request']; | ||
$req = $req->withUri( | ||
$req->getUri()->withPath('/foo.txt') | ||
)->withMethod('GET'); | ||
$resp = $handler($req); | ||
assert($resp->getStatusCode() == 200 && (string) $resp->getBody() == "bar\n"); | ||
}); | ||
it('falls through if no match is found', function() { | ||
$serve_static = Http\Middleware\serveStatic(__DIR__ . '/resources'); | ||
$compose = $this->container['krak.http.compose']; | ||
|
||
$handler = $compose([ | ||
function($req, $next) { return $next->response(404); }, | ||
$serve_static | ||
]); | ||
|
||
$resp = $handler($this->container['request']); | ||
assert($resp->getStatusCode() == 404); | ||
}); | ||
}); |
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 @@ | ||
bar |