-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: extract DefinedRouteCollector #7653
Changes from 5 commits
d19a393
d1224cc
06faf73
6f0d950
453511c
ea62e0b
f6e6362
f5a2841
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?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. | ||
*/ | ||
class DefinedRouteCollector | ||
{ | ||
private RouteCollection $routeCollection; | ||
|
||
public function __construct(RouteCollection $routes) | ||
{ | ||
$this->routeCollection = $routes; | ||
} | ||
|
||
public function collect(): Generator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add the signature of the Generator in the PHPDoc so that it can be used by IDEs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sig begs the question: is it worth introducing a value object at some point for better definition? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The data structure of a route is complex, so it would be easier to understand if it were a class. |
||
{ | ||
$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, | ||
]; | ||
} | ||
} | ||
} | ||
} | ||
} |
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 test() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make this test name descriptive. If using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forgot to rename it. Done. |
||
{ | ||
$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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this intended to be extended? can this be made
final
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.