Skip to content

Commit

Permalink
Merge pull request #21 from krakphp/19-serve-static-middleware
Browse files Browse the repository at this point in the history
Serve Static Middleware #19

Closes #19
  • Loading branch information
ragboyjr committed May 1, 2017
2 parents 7e85be9 + f22c1d0 commit 153cbde
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 12 deletions.
22 changes: 11 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Change Log

All notable changes to this project will be documented in this file.
## Unreleased

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

## [Unreleased]
- HttpServiceProvider which provides basic services of the library
- serveStatic middleware #19

## [0.3.0] - 2017-03-18
## 0.3.0 - 2017-03-18

### Changed

Expand All @@ -24,7 +24,7 @@ Completely re-tooled the entire package. This no longer holds any micro framewor
- This CHANGELOG
- ResponseFactoryStore for easily storing and utilizing response factories.

## [0.2.4] - 2016-12-05
## 0.2.4 - 2016-12-05

### Changed

Expand All @@ -38,14 +38,14 @@ Completely re-tooled the entire package. This no longer holds any micro framewor
- a few more services to the REST package for convenience


## [0.2.3] - 2016-12-05
## 0.2.3 - 2016-12-05

### Added

- Mountable Middleware \#5
- Redirect Marshal Response Matching \#3

## [0.2.2] - 2016-12-04
## 0.2.2 - 2016-12-04

Several minor changes to the system.

Expand All @@ -54,13 +54,13 @@ Several minor changes to the system.
- Adding server to app service dependencies
- Documentation updates and Std package update

## [0.2.1] - 2016-11-30
## 0.2.1 - 2016-11-30

### Fixed

- Hotfix for fixing package.php inclusion

## [0.2.0] - 2016-11-28
## 0.2.0 - 2016-11-28

### Added

Expand All @@ -74,7 +74,7 @@ Several minor changes to the system.

- Fixed bug with config defaults

### [0.1.0] - 2016-11-23
## 0.1.0 - 2016-11-23

Initial Release

Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@
"files": ["src/inc.php"]
},
"require": {
"nikic/iter": "^1.4"
"nikic/iter": "^1.4",
"psr/http-message": "^1.0"
},
"require-dev": {
"guzzlehttp/psr7": "^1.3",
"krak/cargo": "^0.2.4",
"krak/mw": "^0.5.0",
"krak/php-inc": "^0.1.3",
"nikic/fast-route": "^1.0",
"peridot-php/peridot": "^1.18",
"symfony/var-dumper": "^3.2",
"zendframework/zend-diactoros": "^1.3"
},
"suggest": {
"nikic/fast-route": "For the FastRoute dispatcher"
}
}
16 changes: 16 additions & 0 deletions src/Concerns/Response.php
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);
}
}
60 changes: 60 additions & 0 deletions src/HttpServiceProvider.php
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');
}
}
11 changes: 11 additions & 0 deletions src/Middleware/HttpLink.php
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;
}
20 changes: 20 additions & 0 deletions src/Middleware/mw.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ function injectRequestAttribute($name, $value) {
};
}

/** Serves static files over a directory. The $root is a path to physical directory
to where the static files lie. */
function serveStatic($root) {
return function($req, $next) use ($root) {
if ($req->getMethod() != 'GET') {
return $next($req);
}

$path = $req->getUri()->getPath();
$file_path = $root . $path;
if (!is_file($file_path)) {
return $next($req);
}

return $next->response(200, [
'Content-Type' => mime_content_type($file_path),
], fopen($file_path, "r"));
};
}

function mount($path, $mw) {
$mw = mw\group([
function(ServerRequestInterface $req, $next) {
Expand Down
3 changes: 3 additions & 0 deletions test/http.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
describe('Route', function() {
require_once __DIR__ . '/route.php';
});
describe('Middleware', function() {
require_once __DIR__ . '/middleware.php';
});
});
39 changes: 39 additions & 0 deletions test/middleware.php
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);
});
});
1 change: 1 addition & 0 deletions test/resources/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar

0 comments on commit 153cbde

Please sign in to comment.