Skip to content

Commit fe382c8

Browse files
committed
fix: preserve subclass types in route group callbacks
1 parent 699fc2f commit fe382c8

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎system/Router/RouteCollection.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,8 @@ public function getRedirectCode(string $routeKey): int
656656
* $route->resource('users');
657657
* });
658658
*
659-
* @param string $name The name to group/prefix the routes with.
660-
* @param array<string, mixed>|(callable(self): void) ...$params
659+
* @param string $name The name to group/prefix the routes with.
660+
* @param array<string, mixed>|(callable(static): void) ...$params
661661
*
662662
* @return void
663663
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Router;
15+
16+
use CodeIgniter\Router\RouteCollection;
17+
18+
final class RouteCollectionSubclass extends RouteCollection
19+
{
20+
}

‎tests/system/Router/RouteCollectionTest.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use PHPUnit\Framework\Attributes\DataProvider;
2828
use PHPUnit\Framework\Attributes\Group;
2929
use Tests\Support\Controllers\Hello;
30+
use Tests\Support\Router\RouteCollectionSubclass;
3031

3132
/**
3233
* @internal
@@ -314,6 +315,38 @@ static function ($routes): void {
314315
$this->assertSame($expected, $routes->getRoutes());
315316
}
316317

318+
public function testGroupingPassesSubclassToNestedCallbacks(): void
319+
{
320+
$modules = new Modules();
321+
$modules->enabled = false;
322+
$config = new Routing();
323+
$config->defaultNamespace = '\\';
324+
$routes = new RouteCollectionSubclass(service('locator'), $modules, $config);
325+
$routes->setHTTPVerb(Method::GET);
326+
327+
$routes->group('admin', ['namespace' => 'Admin', 'filter' => ['csrf']], function (RouteCollectionSubclass $group) use ($routes): void {
328+
$this->assertSame($routes, $group);
329+
330+
$group->group('profile', ['filter' => ['honeypot']], function (RouteCollectionSubclass $nested) use ($routes): void {
331+
$this->assertSame($routes, $nested);
332+
$nested->get('show', 'Profile::show');
333+
});
334+
335+
$group->get('dashboard', 'Dashboard::index');
336+
});
337+
338+
$routes->get('home', 'Home::index');
339+
340+
$this->assertSame([
341+
'admin/profile/show' => '\\Admin\\Profile::show',
342+
'admin/dashboard' => '\\Admin\\Dashboard::index',
343+
'home' => '\\Home::index',
344+
], $routes->getRoutes(Method::GET));
345+
$this->assertSame(['csrf', 'honeypot'], $routes->getFiltersForRoute('admin/profile/show'));
346+
$this->assertSame(['csrf'], $routes->getFiltersForRoute('admin/dashboard'));
347+
$this->assertFalse($routes->isFiltered('home'));
348+
}
349+
317350
public function testGroupGetsSanitized(): void
318351
{
319352
$routes = $this->getCollector();

0 commit comments

Comments
 (0)