mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
feat: add DefinedRouteCollector
This commit is contained in:
parent
6c09f352c6
commit
d19a393062
63
system/Router/DefinedRouteCollector.php
Normal file
63
system/Router/DefinedRouteCollector.php
Normal 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,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
90
tests/system/Router/DefinedRouteCollectorTest.php
Normal file
90
tests/system/Router/DefinedRouteCollectorTest.php
Normal 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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user