Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add route group #241

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class App
/** @var ReactiveHandler|SapiHandler */
private $sapi;

/** @var string */
protected $currentGroupPrefix;

/** @var array<callable|class-string> */
protected $currentGroupHandlers = [];

/**
* Instantiate new X application
*
Expand Down Expand Up @@ -193,6 +199,26 @@ public function any(string $route, $handler, ...$handlers): void
$this->map(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $route, $handler, ...$handlers);
}

/**
* Create a route group with a common prefix.
*
* All routes created in the passed callback will have the given group prefix prepended.
*
* @param string $prefix
* @param array<callable|class-string> $handlers
* @param callable $callback
*/
public function addGroup(string $prefix, array $handlers, callable $callback): void
{
$previousGroupPrefix = $this->currentGroupPrefix;
$previousGroupHandlers = $this->currentGroupHandlers;
$this->currentGroupPrefix = $previousGroupPrefix . $prefix;
$this->currentGroupHandlers = array_merge($previousGroupHandlers, $handlers);
$callback($this);
$this->currentGroupPrefix = $previousGroupPrefix;
$this->currentGroupHandlers = $previousGroupHandlers;
}

/**
*
* @param string[] $methods
Expand All @@ -202,7 +228,13 @@ public function any(string $route, $handler, ...$handlers): void
*/
public function map(array $methods, string $route, $handler, ...$handlers): void
{
$this->router->map($methods, $route, $handler, ...$handlers);
if (!empty($this->currentGroupHandlers)) {
\array_unshift($handlers, $handler);
$currentGroupHandlers = $this->currentGroupHandlers;
$handler = \array_shift($currentGroupHandlers);
$handlers = \array_merge($currentGroupHandlers, $handlers);
}
$this->router->map($methods, $this->currentGroupPrefix . $route, $handler, ...$handlers);
}

/**
Expand Down
319 changes: 319 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,325 @@ public function testInvokeWithMatchingRouteReturnsInternalServerErrorResponseWhe
$this->assertStringContainsString("<p>Expected request handler to return <code>Psr\Http\Message\ResponseInterface</code> but got <code>null</code>.</p>\n", (string) $response->getBody());
}

public function testInvokeWithMatchingGroupRouteReturnsResponseFromMatchingRouteHandler(): void
{
$app = $this->createAppWithoutLogger();
$app->addGroup('/users', [], function ($app) {
$app->get('', function () {
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK\n"
);
});
});
$request = new ServerRequest('GET', 'http://localhost/users');

$response = $app($request);
assert($response instanceof ResponseInterface);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
$this->assertEquals("OK\n", (string) $response->getBody());
}

public function testInvokeWithMatchingChildGroupRouteReturnsResponseFromMatchingRouteHandler(): void
{
$app = $this->createAppWithoutLogger();
$app->addGroup('/users', [], function ($app) {
$app->addGroup('/{name}', [], function ($app) {
$app->get('/posts/{post}', function (ServerRequestInterface $request) {
$name = $request->getAttribute('name');
$post = $request->getAttribute('post');
assert(is_string($name));
assert(is_string($post));

return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK $name $post\n"
);
});
});
});
$request = new ServerRequest('GET', 'http://localhost/users/alice/posts/first');

$response = $app($request);
assert($response instanceof ResponseInterface);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
$this->assertEquals("OK alice first\n", (string) $response->getBody());
}

public function testConstractAndGroupRouteWithMiddlewareReturnContructMiddleware(): void
{

$middleware1 = function () {
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK1\n"
);
};
$middleware2 = function () {
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK2\n"
);
};

$app = $this->createAppWithoutLogger(
$middleware1
);
$app->addGroup('/users', [
$middleware2
], function ($app) {
$app->get('', function () {
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK3\n"
);
});
});
$request = new ServerRequest('GET', 'http://localhost/users');

$response = $app($request);
assert($response instanceof ResponseInterface);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
$this->assertEquals("OK1\n", (string) $response->getBody());
}

public function testGroupRouteWithMiddlewareReturnGroupMiddleware(): void
{

$middleware2 = function () {
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK2\n"
);
};

$app = $this->createAppWithoutLogger();
$app->addGroup('/users', [
$middleware2
], function ($app) {
$app->get('', function () {
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK3\n"
);
});
});
$request = new ServerRequest('GET', 'http://localhost/users');

$response = $app($request);
assert($response instanceof ResponseInterface);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
$this->assertEquals("OK2\n", (string) $response->getBody());
}

public function testGroupRouteWithMiddlewareReturnRouteMiddleware(): void
{

$middleware2 = function ($request, $next) {
return $next($request);
};

$app = $this->createAppWithoutLogger();
$app->addGroup('/users', [
$middleware2
], function ($app) {
$app->get('', function () {
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK3\n"
);
});
});
$request = new ServerRequest('GET', 'http://localhost/users');

$response = $app($request);
assert($response instanceof ResponseInterface);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
$this->assertEquals("OK3\n", (string) $response->getBody());
}

public function testSetRequestGroupRouteWithMiddlewareAndReturnRouteMiddleware(): void
{

$middleware2 = function ($request, $next) {
return $next($request->withAttribute('group', 'group'));
};

$app = $this->createAppWithoutLogger();
$app->addGroup('/users', [
$middleware2
], function ($app) {
$app->get('', function ($request) {
$group = $request->getAttribute('group');
assert(is_string($group));
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK {$group}\n"
);
});
});
$request = new ServerRequest('GET', 'http://localhost/users');

$response = $app($request);
assert($response instanceof ResponseInterface);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
$this->assertEquals("OK group\n", (string) $response->getBody());
}

public function testGroupRouteMiddlewareSort(): void
{
$app = $this->createAppWithoutLogger();

$fn1 = function ($request, $next) {
return $next($request->withAddedHeader('fn', 'fn1'));
};

$fn2 = function ($request, $next) {
return $next($request->withAddedHeader('fn', 'fn2'));
};

$fn3 = function ($request, $next) {
return $next($request->withAddedHeader('fn', 'fn3'));
};

$fn4 = function ($request, $next) {
return $next($request->withAddedHeader('fn', 'fn4'));
};

$fn5 = function ($request, $next) {
return $next($request->withAddedHeader('fn', 'fn5'));
};

$fn6 = function ($request, $next) {
return $next($request->withAddedHeader('fn', 'fn6'));
};


$app->addGroup('/sorts', [
$fn1
], function ($app) use ($fn2, $fn3, $fn4, $fn5, $fn6) {
$app->get('/sort1', function ($request) {
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK {$request->getHeaderLine('fn')}\n"
);
});
$app->addGroup('', [
$fn2,
$fn3,
$fn4,
], function ($app) use ($fn5, $fn6) {
$app->get('/sort2', function ($request) {
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK {$request->getHeaderLine('fn')}\n"
);
});

$app->addGroup('', [
$fn5
], function ($app) {
$app->get('/sort3', function ($request) {
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK {$request->getHeaderLine('fn')}\n"
);
});

});
$app->addGroup('', [
$fn6
], function ($app) {
$app->get('/sort4', function ($request) {
return new Response(
200,
[
'Content-Type' => 'text/html'
],
"OK {$request->getHeaderLine('fn')}\n"
);
});
});
});
});

$request = new ServerRequest('GET', 'http://localhost/sorts/sort1');
$response = $app($request);
assert($response instanceof ResponseInterface);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
$this->assertEquals("OK fn1\n", (string) $response->getBody());

$request = new ServerRequest('GET', 'http://localhost/sorts/sort2');
$response = $app($request);
assert($response instanceof ResponseInterface);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
$this->assertEquals("OK fn1, fn2, fn3, fn4\n", (string) $response->getBody());

$request = new ServerRequest('GET', 'http://localhost/sorts/sort3');
$response = $app($request);
assert($response instanceof ResponseInterface);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
$this->assertEquals("OK fn1, fn2, fn3, fn4, fn5\n", (string) $response->getBody());

$request = new ServerRequest('GET', 'http://localhost/sorts/sort4');
$response = $app($request);
assert($response instanceof ResponseInterface);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
$this->assertEquals("OK fn1, fn2, fn3, fn4, fn6\n", (string) $response->getBody());
}

private function createAppWithoutLogger(callable ...$middleware): App
{
return new App(
Expand Down