Skip to content

Commit

Permalink
Merge pull request #84 from juherr/master
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Jun 29, 2023
2 parents 9374b67 + 4d4afd5 commit d14c95b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/ControllerInvoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __invoke(
): ResponseInterface {
// Inject the request and response by parameter name
$parameters = [
'request' => $request,
'request' => self::injectRouteArguments($request, $routeArguments),
'response' => $response,
];
// Inject the route arguments by name
Expand All @@ -46,4 +46,13 @@ public function __invoke(

return $this->invoker->call($callable, $parameters);
}

private static function injectRouteArguments(ServerRequestInterface $request, array $routeArguments): ServerRequestInterface
{
$requestWithArgs = $request;
foreach ($routeArguments as $key => $value) {
$requestWithArgs = $requestWithArgs->withAttribute($key, $value);
}
return $requestWithArgs;
}
}
15 changes: 15 additions & 0 deletions tests/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ public function injects_route_placeholder()
$this->assertEquals('Hello matt', (string) $response->getBody());
}

/**
* @test
*/
public function injects_route_placeholder_from_request()
{
$app = Bridge::create();
$app->get('/{name}', function ($request, $response) {
$response->getBody()->write('Hello ' . $request->getAttribute('name'));
return $response;
});

$response = $app->handle(RequestFactory::create('/matt'));
$this->assertEquals('Hello matt', (string) $response->getBody());
}

/**
* @test
*/
Expand Down

0 comments on commit d14c95b

Please sign in to comment.