Skip to content

Commit 4288efe

Browse files
committed
refactor: change the method order
loadRoutes() and discoverRoutes() are initialization.
1 parent 125deac commit 4288efe

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

system/Router/RouteCollection.php

Lines changed: 56 additions & 56 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.
@@ -1491,25 +1512,4 @@ public function shouldUseSupportedLocalesOnly(): bool
14911512
{
14921513
return $this->useSupportedLocalesOnly;
14931514
}
1494-
1495-
/**
1496-
* Loads main routes file and discover routes.
1497-
*
1498-
* Loads only once unless reset.
1499-
*
1500-
* @return $this
1501-
*/
1502-
public function loadRoutes(string $routesFile = APPPATH . 'Config/Routes.php')
1503-
{
1504-
if ($this->didDiscover) {
1505-
return $this;
1506-
}
1507-
1508-
$routes = $this;
1509-
require $routesFile;
1510-
1511-
$this->discoverRoutes();
1512-
1513-
return $this;
1514-
}
15151515
}

0 commit comments

Comments
 (0)