-
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
Merged
kenjis
merged 8 commits into
codeigniter4:4.4
from
kenjis:refactor-extract-DefinedRouteCollector
Jul 4, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d19a393
feat: add DefinedRouteCollector
kenjis d1224cc
refactor: use DefinedRouteCollector in `spark routes`
kenjis 06faf73
refactor: use DefinedRouteCollector in DebugBar
kenjis 6f0d950
refactor: use DefinedRouteCollector in `spark routes`
kenjis 453511c
docs: add comment
kenjis ea62e0b
fix: add final keyword to DefinedRouteCollector
kenjis f6e6362
test: fix test method name
kenjis f5a2841
docs: add @phpstan-return
kenjis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <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; | ||
|
||
/** | ||
* 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 <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 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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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 comment
The 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 comment
The 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.