Skip to content

Commit

Permalink
Merge pull request #22 from krakphp/20-url-builder
Browse files Browse the repository at this point in the history
Url Builder #20

Closes #20
  • Loading branch information
ragboyjr committed May 1, 2017
2 parents 153cbde + d5518f2 commit a89edda
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- HttpServiceProvider which provides basic services of the library
- serveStatic middleware #19
- Request Path #20

## 0.3.0 - 2017-03-18

Expand Down
6 changes: 3 additions & 3 deletions src/Middleware/mw.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function mount($path, $mw) {
$mw = mw\group([
function(ServerRequestInterface $req, $next) {
$stack = $req->getAttribute('original_uri_path');
$path = $stack->pop();
list($path) = $stack->pop();
$req = $req->withUri($req->getUri()->withPath($path));
if (!count($stack)) {
$req = $req->withoutAttribute('original_uri_path');
Expand All @@ -46,8 +46,8 @@ function(ServerRequestInterface $req, $next) {
$mw,
function(ServerRequestInterface $req, $next) use ($path) {
$orig_path = $req->getUri()->getPath();
$stack = $req->getAttribute('original_uri_path') ?: new \SplStack();
$stack->push($orig_path);
$stack = $req->getAttribute('original_uri_path') ?: new \SplDoublyLinkedList();
$stack->push([$orig_path, $path]);
$new_path = substr($orig_path, strlen($path));
if (!$new_path) {
$new_path = '/';
Expand Down
34 changes: 34 additions & 0 deletions src/RequestPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Krak\Http;

use Psr\Http\Message\ServerRequestInterface;
use SplStack;
use function iter\reduce;

/** returns the full path from a request that might have been mounted or not */
class RequestPath
{
private $req;

public function __construct(ServerRequestInterface $req) {
$this->req = $req;
}

public function path($path = null) {
$path = $path ?: $this->req->getUri()->getPath();
$path_stack = $this->req->getAttribute('original_uri_path');
if (!$path_stack) {
return $path;
}

$path_stack = clone $path_stack;
$path_stack->setIteratorMode(SplStack::IT_MODE_FIFO | SplStack::IT_MODE_KEEP);

$base_path = reduce(function($acc, $tup) {
return $acc . $tup[1];
}, $path_stack, '');

return $base_path . $path;
}
}
23 changes: 23 additions & 0 deletions test/middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,26 @@ function($req, $next) { return $next->response(404); },
assert($resp->getStatusCode() == 404);
});
});
describe('#mount', function() {
it('mounts a middleware on a specific path', function() {
$mw = function($req, $next) {
$path = new Http\RequestPath($req);
return $next->response(200, [], $path->path('/assets/app.css'));
};

$compose = $this->container['krak.http.compose'];
$handler = $compose([
Http\Middleware\mount(
'/admin',
Http\Middleware\mount('/module', $mw)
)
]);

$req = $this->container['request'];
$req = $req->withUri(
$req->getUri()->withPath('/admin/module')
)->withMethod('GET');
$resp = $handler($req);
assert($resp->getStatusCode() == 200 && (string) $resp->getBody() == "/admin/module/assets/app.css");
});
});

0 comments on commit a89edda

Please sign in to comment.