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

[5.x] Fix primitive type hint handling in Statamic routes #11476

Merged
merged 2 commits into from
Feb 21, 2025
Merged
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
5 changes: 4 additions & 1 deletion src/Http/Controllers/FrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ private static function resolveRouteClosure(Closure $closure, array $params)
$reflect = new ReflectionFunction($closure);

$params = collect($reflect->getParameters())
->map(fn ($param) => $param->hasType() ? app($param->getType()->getName()) : $params[$param->getName()])
->map(fn ($param) => $param->hasType() && class_exists($class = $param->getType()->getName())
? app($class)
: $params[$param->getName()]
)
->all();

return $closure(...$params);
Expand Down
30 changes: 30 additions & 0 deletions tests/Routing/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ protected function resolveApplicationConfiguration($app)
return view('test', ['hello' => "view closure dependencies: $request->value $foo->value $bar->value $baz $qux"]);
});

Route::statamic('/route/with/placeholders/view/closure-primitive-type-hints/{name}/{age}', function (string $name, int $age) {
return view('test', ['hello' => "view closure placeholders: $name $age"]);
});

Route::statamic('/route/with/placeholders/data/closure/{foo}/{bar}/{baz}', 'test', function ($foo, $bar, $baz) {
return ['hello' => "data closure placeholders: $foo $bar $baz"];
});
Expand All @@ -86,6 +90,10 @@ protected function resolveApplicationConfiguration($app)
return ['hello' => "data closure dependencies: $request->value $foo->value $bar->value $baz $qux"];
});

Route::statamic('/route/with/placeholders/data/closure-primitive-type-hints/{name}/{age}', 'test', function (string $name, int $age) {
return ['hello' => "data closure placeholders: $name $age"];
});

Route::statamic('/route-with-custom-layout', 'test', [
'layout' => 'custom-layout',
'hello' => 'world',
Expand Down Expand Up @@ -320,6 +328,17 @@ public function it_renders_a_view_with_placeholders_using_a_view_closure_and_dep
->assertSee('Hello view closure dependencies: request_value foo_class bar_class_modified one two');
}

#[Test]
public function it_renders_a_view_with_placeholders_using_a_view_closure_using_primitive_type_hints()
{
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('test', 'Hello {{ hello }}');

$this->get('/route/with/placeholders/view/closure-primitive-type-hints/darth/42')
->assertOk()
->assertSee('Hello view closure placeholders: darth 42');
}

#[Test]
public function it_renders_a_view_with_placeholders_using_a_data_closure()
{
Expand Down Expand Up @@ -360,6 +379,17 @@ public function it_renders_a_view_with_placeholders_using_a_data_closure_and_dep
->assertSee('Hello data closure dependencies: request_value foo_class bar_class_modified one two');
}

#[Test]
public function it_renders_a_view_with_placeholders_using_a_data_closure_using_primitive_type_hints()
{
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('test', 'Hello {{ hello }}');

$this->get('/route/with/placeholders/data/closure-primitive-type-hints/darth/42')
->assertOk()
->assertSee('Hello data closure placeholders: darth 42');
}

#[Test]
public function it_renders_a_view_with_custom_layout()
{
Expand Down
Loading