Skip to content

Commit 807a16b

Browse files
authored
Merge pull request #6293 from kenjis/refactor-Routes.php-loading
refactor: loading app/Config/routes.php
2 parents 4d5ca25 + 04cbdf1 commit 807a16b

File tree

9 files changed

+65
-68
lines changed

9 files changed

+65
-68
lines changed

system/CodeIgniter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ public function displayPerformanceMetrics(string $output): string
730730
protected function tryToRouteIt(?RouteCollectionInterface $routes = null)
731731
{
732732
if ($routes === null) {
733-
require APPPATH . 'Config/Routes.php';
733+
$routes = Services::routes()->loadRoutes();
734734
}
735735

736736
// $routes is defined in Config/Routes.php

system/Commands/Utilities/FilterCheck.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ public function run(array $params)
8585
$route = $params[1];
8686

8787
// Load Routes
88-
$routes = Services::routes();
89-
require APPPATH . 'Config/Routes.php';
90-
$routes->getRoutes('*'); // Triggers discovery
88+
Services::routes()->loadRoutes();
9189

9290
$filterCollector = new FilterCollector();
9391

system/Commands/Utilities/Routes.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ class Routes extends BaseCommand
7575
*/
7676
public function run(array $params)
7777
{
78-
$routes = Services::routes(true);
79-
require APPPATH . 'Config/Routes.php';
80-
81-
$collection = $routes;
78+
$collection = Services::routes()->loadRoutes();
8279
$methods = [
8380
'get',
8481
'head',

system/Router/RouteCollection.php

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,62 @@ public function __construct(FileLocator $locator, Modules $moduleConfig)
239239
$this->httpHost = Services::request()->getServer('HTTP_HOST');
240240
}
241241

242+
/**
243+
* Loads main routes file and discover routes.
244+
*
245+
* Loads only once unless reset.
246+
*
247+
* @return $this
248+
*/
249+
public function loadRoutes(string $routesFile = APPPATH . 'Config/Routes.php')
250+
{
251+
if ($this->didDiscover) {
252+
return $this;
253+
}
254+
255+
$routes = $this;
256+
require $routesFile;
257+
258+
$this->discoverRoutes();
259+
260+
return $this;
261+
}
262+
263+
/**
264+
* Will attempt to discover any additional routes, either through
265+
* the local PSR4 namespaces, or through selected Composer packages.
266+
*/
267+
protected function discoverRoutes()
268+
{
269+
if ($this->didDiscover) {
270+
return;
271+
}
272+
273+
// We need this var in local scope
274+
// so route files can access it.
275+
$routes = $this;
276+
277+
if ($this->moduleConfig->shouldDiscover('routes')) {
278+
$files = $this->fileLocator->search('Config/Routes.php');
279+
280+
$excludes = [
281+
APPPATH . 'Config' . DIRECTORY_SEPARATOR . 'Routes.php',
282+
SYSTEMPATH . 'Config' . DIRECTORY_SEPARATOR . 'Routes.php',
283+
];
284+
285+
foreach ($files as $file) {
286+
// Don't include our main file again...
287+
if (in_array($file, $excludes, true)) {
288+
continue;
289+
}
290+
291+
include $file;
292+
}
293+
}
294+
295+
$this->didDiscover = true;
296+
}
297+
242298
/**
243299
* Registers a new constraint with the system. Constraints are used
244300
* by the routes as placeholders for regular expressions to make defining
@@ -362,41 +418,6 @@ public function get404Override()
362418
return $this->override404;
363419
}
364420

365-
/**
366-
* Will attempt to discover any additional routes, either through
367-
* the local PSR4 namespaces, or through selected Composer packages.
368-
*/
369-
protected function discoverRoutes()
370-
{
371-
if ($this->didDiscover) {
372-
return;
373-
}
374-
375-
// We need this var in local scope
376-
// so route files can access it.
377-
$routes = $this;
378-
379-
if ($this->moduleConfig->shouldDiscover('routes')) {
380-
$files = $this->fileLocator->search('Config/Routes.php');
381-
382-
$excludes = [
383-
APPPATH . 'Config' . DIRECTORY_SEPARATOR . 'Routes.php',
384-
SYSTEMPATH . 'Config' . DIRECTORY_SEPARATOR . 'Routes.php',
385-
];
386-
387-
foreach ($files as $file) {
388-
// Don't include our main file again...
389-
if (in_array($file, $excludes, true)) {
390-
continue;
391-
}
392-
393-
include $file;
394-
}
395-
}
396-
397-
$this->didDiscover = true;
398-
}
399-
400421
/**
401422
* Sets the default constraint to be used in the system. Typically
402423
* for use with the 'resource' method.
@@ -1393,6 +1414,7 @@ public function resetRoutes()
13931414
}
13941415

13951416
$this->prioritizeDetected = false;
1417+
$this->didDiscover = false;
13961418
}
13971419

13981420
/**

system/Test/FeatureTestCase.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,7 @@ public function call(string $method, string $path, ?array $params = null)
176176

177177
// Initialize the RouteCollection
178178
if (! $routes = $this->routes) {
179-
require APPPATH . 'Config/Routes.php';
180-
181-
/**
182-
* @var RouteCollection $routes
183-
*/
184-
$routes->getRoutes('*');
179+
$routes = Services::routes()->loadRoutes();
185180
}
186181

187182
$routes->setHTTPVerb($method);

system/Test/FeatureTestTrait.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,7 @@ public function call(string $method, string $path, ?array $params = null)
166166

167167
// Initialize the RouteCollection
168168
if (! $routes = $this->routes) {
169-
require APPPATH . 'Config/Routes.php';
170-
171-
/**
172-
* @var RouteCollection $routes
173-
*/
174-
$routes->getRoutes('*');
169+
$routes = Services::routes()->loadRoutes();
175170
}
176171

177172
$routes->setHTTPVerb($method);

system/Test/FilterTestTrait.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,7 @@ protected function setUpFilterTestTrait(): void
102102
$this->filters ??= new Filters($this->filtersConfig, $this->request, $this->response);
103103

104104
if ($this->collection === null) {
105-
// Load the RouteCollection from Config to gather App route info
106-
// (creates $routes using the Service as a starting point)
107-
require APPPATH . 'Config/Routes.php';
108-
109-
$routes->getRoutes('*'); // Triggers discovery
110-
$this->collection = $routes;
105+
$this->collection = Services::routes()->loadRoutes();
111106
}
112107

113108
$this->doneFilterSetUp = true;

system/Test/bootstrap.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111

1212
use CodeIgniter\Config\DotEnv;
13-
use CodeIgniter\Router\RouteCollection;
1413
use Config\Autoload;
1514
use Config\Modules;
1615
use Config\Paths;
@@ -93,9 +92,4 @@
9392
// Always load the URL helper, it should be used in most of apps.
9493
helper('url');
9594

96-
require_once APPPATH . 'Config/Routes.php';
97-
98-
/**
99-
* @var RouteCollection $routes
100-
*/
101-
$routes->getRoutes('*');
95+
Services::routes()->loadRoutes();

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Others
2828

2929
- The ``spark`` file has been changed due to a change in the processing of Spark commands.
3030
- ``InvalidArgumentException`` that is a kind of ``LogicException`` in ``BaseBuilder::_whereIn()`` is not suppressed by the configuration. Previously if ``CI_DEBUG`` was false, the exception was suppressed.
31+
- ``RouteCollection::resetRoutes()`` resets Auto-Discovery of Routes. Previously once discovered, RouteCollection never discover Routes files again even if ``RouteCollection::resetRoutes()`` is called.
3132

3233
Enhancements
3334
************

0 commit comments

Comments
 (0)