Skip to content

Commit c4b61fa

Browse files
author
Florian Stascheck
authored
[9.x] Allow enum route bindings to have default values (#44255)
* Allow enum route bindings to have default values * Attempt for compatability with PHP <8.1 * Fix for StyleCI * Make $isEnum statement nicer
1 parent d58c89f commit c4b61fa

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/Illuminate/Routing/RouteDependencyResolverTrait.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Reflector;
7+
use ReflectionClass;
78
use ReflectionFunctionAbstract;
89
use ReflectionMethod;
910
use ReflectionParameter;
@@ -77,7 +78,11 @@ protected function transformDependency(ReflectionParameter $parameter, $paramete
7778
// the list of parameters. If it is we will just skip it as it is probably a model
7879
// binding and we do not want to mess with those; otherwise, we resolve it here.
7980
if ($className && ! $this->alreadyInParameters($className, $parameters)) {
80-
return $parameter->isDefaultValueAvailable() ? null : $this->container->make($className);
81+
$isEnum = method_exists(ReflectionClass::class, 'isEnum') && (new ReflectionClass($className))->isEnum();
82+
83+
return $parameter->isDefaultValueAvailable()
84+
? ($isEnum ? $parameter->getDefaultValue() : null)
85+
: $this->container->make($className);
8186
}
8287

8388
return $skippableValue;

tests/Integration/Routing/ImplicitBackedEnumRouteBindingTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public function testWithRouteCachingEnabled()
2424
Route::get('/categories/{category}', function (CategoryBackedEnum \$category) {
2525
return \$category->value;
2626
})->middleware('web');
27+
28+
Route::get('/categories-default/{category?}', function (CategoryBackedEnum \$category = CategoryBackedEnum::Fruits) {
29+
return \$category->value;
30+
})->middleware('web');
2731
PHP);
2832

2933
$response = $this->get('/categories/fruits');
@@ -34,6 +38,15 @@ public function testWithRouteCachingEnabled()
3438

3539
$response = $this->get('/categories/cars');
3640
$response->assertNotFound(404);
41+
42+
$response = $this->get('/categories-default/');
43+
$response->assertSee('fruits');
44+
45+
$response = $this->get('/categories-default/people');
46+
$response->assertSee('people');
47+
48+
$response = $this->get('/categories-default/fruits');
49+
$response->assertSee('fruits');
3750
}
3851

3952
public function testWithoutRouteCachingEnabled()
@@ -44,6 +57,10 @@ public function testWithoutRouteCachingEnabled()
4457
return $category->value;
4558
})->middleware(['web']);
4659

60+
Route::post('/categories-default/{category?}', function (CategoryBackedEnum $category = CategoryBackedEnum::Fruits) {
61+
return $category->value;
62+
})->middleware('web');
63+
4764
$response = $this->post('/categories/fruits');
4865
$response->assertSee('fruits');
4966

@@ -52,5 +69,14 @@ public function testWithoutRouteCachingEnabled()
5269

5370
$response = $this->post('/categories/cars');
5471
$response->assertNotFound(404);
72+
73+
$response = $this->post('/categories-default/');
74+
$response->assertSee('fruits');
75+
76+
$response = $this->post('/categories-default/people');
77+
$response->assertSee('people');
78+
79+
$response = $this->post('/categories-default/fruits');
80+
$response->assertSee('fruits');
5581
}
5682
}

tests/Routing/ImplicitRouteBindingTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ public function test_it_can_resolve_the_implicit_backed_enum_route_bindings_for_
3636
$this->assertSame('fruits', $route->parameter('category')->value);
3737
}
3838

39+
/**
40+
* @requires PHP >= 8.1
41+
*/
42+
public function test_it_can_resolve_the_backed_enum_default_value_for_the_given_route()
43+
{
44+
$action = ['uses' => function (CategoryBackedEnum $category = CategoryBackedEnum::Fruits) {
45+
return $category->value;
46+
}];
47+
48+
$route = new Route('GET', '/test', $action);
49+
$route->parameters = [];
50+
51+
$this->assertSame('fruits', $route->run());
52+
}
53+
3954
/**
4055
* @requires PHP >= 8.1
4156
*/

0 commit comments

Comments
 (0)