-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7653 from kenjis/refactor-extract-DefinedRouteCol…
…lector refactor: extract DefinedRouteCollector
- Loading branch information
Showing
4 changed files
with
187 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* 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; | ||
|
||
/** | ||
* Collect all defined routes for display. | ||
*/ | ||
final class DefinedRouteCollector | ||
{ | ||
private RouteCollection $routeCollection; | ||
|
||
public function __construct(RouteCollection $routes) | ||
{ | ||
$this->routeCollection = $routes; | ||
} | ||
|
||
/** | ||
* @phpstan-return Generator<array{method: string, route: string, name: string, handler: string}> | ||
*/ | ||
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, | ||
]; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* 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 testCollect() | ||
{ | ||
$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); | ||
} | ||
} |