feat: add DefinedRouteCollector

This commit is contained in:
kenjis 2023-07-04 11:16:46 +09:00
parent 6c09f352c6
commit d19a393062
No known key found for this signature in database
GPG Key ID: BD254878922AF198
2 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,63 @@
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Router;
use Closure;
use Generator;
class DefinedRouteCollector
{
private RouteCollection $routeCollection;
public function __construct(RouteCollection $routes)
{
$this->routeCollection = $routes;
}
public function collect(): Generator
{
$methods = [
'get',
'head',
'post',
'patch',
'put',
'delete',
'options',
'trace',
'connect',
'cli',
];
foreach ($methods as $method) {
$routes = $this->routeCollection->getRoutes($method);
foreach ($routes as $route => $handler) {
if (is_string($handler) || $handler instanceof Closure) {
if ($handler instanceof Closure) {
$handler = '(Closure)';
}
$routeName = $this->routeCollection->getRoutesOptions($route)['as'] ?? $route;
yield [
'method' => $method,
'route' => $route,
'name' => $routeName,
'handler' => $handler,
];
}
}
}
}
}

View File

@ -0,0 +1,90 @@
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Router;
use CodeIgniter\Config\Services;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Modules;
use Config\Routing;
/**
* @internal
*
* @group Others
*/
final class DefinedRouteCollectorTest extends CIUnitTestCase
{
private function createRouteCollection(array $config = [], $moduleConfig = null): RouteCollection
{
$defaults = [
'Config' => APPPATH . 'Config',
'App' => APPPATH,
];
$config = array_merge($config, $defaults);
Services::autoloader()->addNamespace($config);
$loader = Services::locator();
if ($moduleConfig === null) {
$moduleConfig = new Modules();
$moduleConfig->enabled = false;
}
return (new RouteCollection($loader, $moduleConfig, new Routing()))->setHTTPVerb('get');
}
public function test()
{
$routes = $this->createRouteCollection();
$routes->get('journals', 'Blogs');
$routes->get('product/(:num)', 'Catalog::productLookupByID/$1');
$routes->get('feed', static fn () => 'A Closure route.');
$routes->view('about', 'pages/about');
$collector = new DefinedRouteCollector($routes);
$definedRoutes = [];
foreach ($collector->collect() as $route) {
$definedRoutes[] = $route;
}
$expected = [
[
'method' => 'get',
'route' => 'journals',
'name' => 'journals',
'handler' => '\App\Controllers\Blogs',
],
[
'method' => 'get',
'route' => 'product/([0-9]+)',
'name' => 'product/([0-9]+)',
'handler' => '\App\Controllers\Catalog::productLookupByID/$1',
],
[
'method' => 'get',
'route' => 'feed',
'name' => 'feed',
'handler' => '(Closure)',
],
[
'method' => 'get',
'route' => 'about',
'name' => 'about',
'handler' => '(Closure)',
],
];
$this->assertSame($expected, $definedRoutes);
}
}