feat: add closure support to RouteGroup::group()
This commit is contained in:
parent
a0d9ac75d6
commit
f29b424c00
|
|
@ -31,9 +31,7 @@ class RouteGroup
|
||||||
*/
|
*/
|
||||||
public static function create(array $options, Router $router): self
|
public static function create(array $options, Router $router): self
|
||||||
{
|
{
|
||||||
$self = new self($options);
|
return new self($options, $router);
|
||||||
$self->router = $router;
|
|
||||||
return $self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(string $path, mixed $handler, string|null $name = null): RouteDefinition
|
public function get(string $path, mixed $handler, string|null $name = null): RouteDefinition
|
||||||
|
|
@ -174,8 +172,13 @@ class RouteGroup
|
||||||
return $this->joinPaths($this->options['prefix'] ?? '', $path);
|
return $this->joinPaths($this->options['prefix'] ?? '', $path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function group(array $options): RouteGroup
|
public function group(array|callable $options): RouteGroup
|
||||||
{
|
{
|
||||||
|
if (is_callable($options)) {
|
||||||
|
$options($this);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
$prefix = $this->options['prefix'] ?? '';
|
$prefix = $this->options['prefix'] ?? '';
|
||||||
$newPrefix = $this->joinPaths($prefix, $options['prefix'] ?? '');
|
$newPrefix = $this->joinPaths($prefix, $options['prefix'] ?? '');
|
||||||
|
|
||||||
|
|
|
||||||
47
tests/Unit/RouteGroupClosureTest.php
Normal file
47
tests/Unit/RouteGroupClosureTest.php
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Atlas\Tests\Unit;
|
||||||
|
|
||||||
|
use Atlas\Router\Router;
|
||||||
|
use Atlas\Config\Config;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class RouteGroupClosureTest extends TestCase
|
||||||
|
{
|
||||||
|
private Router $router;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$config = new Config(['modules_path' => ['/path/to/modules']]);
|
||||||
|
$this->router = new Router($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGroupSupportsClosureRegistration(): void
|
||||||
|
{
|
||||||
|
$this->router->group(['prefix' => '/api'])->group(function($group) {
|
||||||
|
$group->get('/users', 'UserHandler');
|
||||||
|
$group->post('/users', 'UserCreateHandler');
|
||||||
|
});
|
||||||
|
|
||||||
|
$routes = iterator_to_array($this->router->getRoutes());
|
||||||
|
$this->assertCount(2, $routes);
|
||||||
|
$this->assertSame('/api/users', $routes[0]->getPath());
|
||||||
|
$this->assertSame('GET', $routes[0]->getMethod());
|
||||||
|
$this->assertSame('/api/users', $routes[1]->getPath());
|
||||||
|
$this->assertSame('POST', $routes[1]->getMethod());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNestedGroupClosureInheritance(): void
|
||||||
|
{
|
||||||
|
$this->router->group(['prefix' => '/api', 'middleware' => ['auth']])->group(function($group) {
|
||||||
|
$group->group(['prefix' => '/v1'])->group(function($v1) {
|
||||||
|
$v1->get('/profile', 'ProfileHandler');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$routes = iterator_to_array($this->router->getRoutes());
|
||||||
|
$this->assertCount(1, $routes);
|
||||||
|
$this->assertSame('/api/v1/profile', $routes[0]->getPath());
|
||||||
|
$this->assertContains('auth', $routes[0]->getMiddleware());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue