Skip to content

Commit f469976

Browse files
committed
[FrameworkBundle] Add support for first-class callable route controller in MicroKernelTrait
1 parent e3629d2 commit f469976

File tree

4 files changed

+9
-0
lines changed

4 files changed

+9
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Add `xliff` support in addition to `xlf` for `XliffFileDumper`
1111
* Deprecate the `reset_on_message` config option. It can be set to `true` only and does nothing now
1212
* Add `trust_x_sendfile_type_header` option
13+
* Add support for first-class callable route controller in `MicroKernelTrait`
1314

1415
6.0
1516
---

Kernel/MicroKernelTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
214214

215215
if (\is_array($controller) && [0, 1] === array_keys($controller) && $this === $controller[0]) {
216216
$route->setDefault('_controller', ['kernel', $controller[1]]);
217+
} elseif ($controller instanceof \Closure && $this === ($r = new \ReflectionFunction($controller))->getClosureThis() && !str_contains($r->name, '{closure}')) {
218+
$route->setDefault('_controller', ['kernel', $r->name]);
217219
}
218220
}
219221

Tests/Kernel/MicroKernelTraitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ public function testFlexStyle()
8989
$response = $kernel->handle($request);
9090

9191
$this->assertEquals('Have a great day!', $response->getContent());
92+
93+
$request = Request::create('/h');
94+
$response = $kernel->handle($request);
95+
96+
$this->assertEquals('Have a great day!', $response->getContent());
9297
}
9398

9499
public function testSecretLoadedFromExtension()

Tests/Kernel/flex-style/src/FlexStyleMicroKernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function __destruct()
7474
protected function configureRoutes(RoutingConfigurator $routes): void
7575
{
7676
$routes->add('halloween', '/')->controller([$this, 'halloweenAction']);
77+
$routes->add('halloween2', '/h')->controller($this->halloweenAction(...));
7778
}
7879

7980
protected function configureContainer(ContainerConfigurator $c): void

0 commit comments

Comments
 (0)