Skip to content

Commit f79c6a8

Browse files
committed
RouteCollection revised to use new centralized auto-discovery mechanism.
1 parent 2ae947c commit f79c6a8

7 files changed

Lines changed: 50 additions & 54 deletions

File tree

application/Config/Modules.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
// Cannot extend BaseConfig or looping resources occurs.
44
class Modules
55
{
6+
/*
7+
|--------------------------------------------------------------------------
8+
| Auto-Discovery Enabled?
9+
|--------------------------------------------------------------------------
10+
|
11+
| If true, then auto-discovery will happen across all elements listed in
12+
| $activeExplorers below. If false, no auto-discovery will happen at all,
13+
| giving a slight performance boost.
14+
*/
15+
public $enabled = true;
16+
617
/*
718
|--------------------------------------------------------------------------
819
| Auto-discover Rules
@@ -12,9 +23,8 @@ class Modules
1223
| and used during the current application request. If it is not
1324
| listed here, only the base application elements will be used.
1425
*/
15-
public $enabled = [
26+
public $activeExplorers = [
1627
'events',
17-
'helpers',
1828
'registrars',
1929
'routes',
2030
'services',
@@ -39,7 +49,6 @@ class Modules
3949
*
4050
* Valid values are:
4151
* - events
42-
* - helpers
4352
* - registrars
4453
* - routes
4554
* - services
@@ -51,8 +60,10 @@ class Modules
5160
*/
5261
public function shouldDiscover(string $alias)
5362
{
63+
if (! $this->enabled) return false;
64+
5465
$alias = strtolower($alias);
5566

56-
return in_array($alias, $this->enabled);
67+
return in_array($alias, $this->activeExplorers);
5768
}
5869
}

application/Config/Routes.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
$routes->setTranslateURIDashes(false);
6565
$routes->set404Override();
6666
$routes->setAutoRoute(true);
67-
$routes->discoverLocal(false);
6867

6968
/**
7069
* --------------------------------------------------------------------

system/Config/Services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ public static function routes($getShared = true)
598598
return self::getSharedInstance('routes');
599599
}
600600

601-
return new \CodeIgniter\Router\RouteCollection(self::locator());
601+
return new \CodeIgniter\Router\RouteCollection(self::locator(), config('Modules'));
602602
}
603603

604604
//--------------------------------------------------------------------

system/Router/RouteCollection.php

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -188,34 +188,37 @@ class RouteCollection implements RouteCollectionInterface
188188
protected $currentOptions = null;
189189

190190
/**
191-
* Determines whether locally specified, PSR4
192-
* compatible code is automatically scanned
193-
* for addition routes in a {namespace}/Config/Routes.php file.
194-
*
191+
* A little performance booster.
195192
* @var bool
196193
*/
197-
protected $discoverLocal = false;
194+
protected $didDiscover = false;
198195

199196
/**
200-
* A little performance booster.
201-
* @var bool
197+
* @var \CodeIgniter\Autoloader\FileLocator
202198
*/
203-
protected $didDiscover = false;
204199
protected $fileLocator;
205200

201+
/**
202+
* @var \Config\Modules
203+
*/
204+
protected $moduleConfig;
205+
206206
//--------------------------------------------------------------------
207207

208208
/**
209209
* Constructor
210210
*
211-
* @param FileLocator $locator
211+
* @param FileLocator $locator
212+
* @param Config/Modules $moduleConfig
212213
*/
213-
public function __construct(FileLocator $locator)
214+
public function __construct(FileLocator $locator, $moduleConfig)
214215
{
215216
// Get HTTP verb
216217
$this->HTTPVerb = strtolower($_SERVER['REQUEST_METHOD'] ?? 'cli');
217218

218219
$this->fileLocator = $locator;
220+
221+
$this->moduleConfig = $moduleConfig;
219222
}
220223

221224
//--------------------------------------------------------------------
@@ -373,33 +376,14 @@ public function get404Override()
373376

374377
//--------------------------------------------------------------------
375378

376-
/**
377-
* If true, will attempt to auto-discover new route files
378-
* based on any PSR4 namespaces that have been set
379-
* in Config/Autoload.php.
380-
*
381-
* @param bool $discover
382-
*
383-
* @return $this
384-
*/
385-
public function discoverLocal(bool $discover)
386-
{
387-
$this->discoverLocal = $discover;
388-
389-
return $this;
390-
}
391-
392-
//--------------------------------------------------------------------
393-
394379
/**
395380
* Will attempt to discover any additional routes, either through
396381
* the local PSR4 namespaces, or through selected Composer packages.
397382
* (Composer coming soon...)
398383
*/
399384
protected function discoverRoutes()
400385
{
401-
if ($this->didDiscover)
402-
return;
386+
if ($this->didDiscover) return;
403387

404388
// We need this var in local scope
405389
// so route files can access it.
@@ -408,7 +392,7 @@ protected function discoverRoutes()
408392
/*
409393
* Discover Local Files
410394
*/
411-
if ($this->discoverLocal === true)
395+
if ($this->moduleConfig->shouldDiscover('routes'))
412396
{
413397
$files = $this->fileLocator->search('Config/Routes.php');
414398

tests/system/Router/RouteCollectionTest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function tearDown()
1414

1515
//--------------------------------------------------------------------
1616

17-
protected function getCollector(array $config=[], array $files=[])
17+
protected function getCollector(array $config=[], array $files=[], $moduleConfig = null)
1818
{
1919
$defaults = [
2020
'Config' => APPPATH.'Config',
@@ -28,7 +28,13 @@ protected function getCollector(array $config=[], array $files=[])
2828
$loader = new MockFileLocator($autoload);
2929
$loader->setFiles($files);
3030

31-
return new RouteCollection($loader);
31+
if ($moduleConfig === null)
32+
{
33+
$moduleConfig = new \Config\Modules();
34+
$moduleConfig->enabled = false;
35+
}
36+
37+
return new RouteCollection($loader, $moduleConfig);
3238
}
3339

3440
public function testBasicAdd()
@@ -772,8 +778,10 @@ public function testWillDiscoverLocal()
772778
'SampleSpace' => TESTPATH .'_support'
773779
];
774780

775-
$routes = $this->getCollector($config);
776-
$routes->discoverLocal(true);
781+
$moduleConfig = new \Config\Modules();
782+
$moduleConfig->enabled = true;
783+
784+
$routes = $this->getCollector($config, [], $moduleConfig);
777785

778786
$match = $routes->getRoutes();
779787

@@ -789,8 +797,10 @@ public function testDiscoverLocalAllowsConfigToOverridePackages()
789797
'SampleSpace' => TESTPATH .'_support'
790798
];
791799

792-
$routes = $this->getCollector($config);
793-
$routes->discoverLocal(true);
800+
$moduleConfig = new \Config\Modules();
801+
$moduleConfig->enabled = true;
802+
803+
$routes = $this->getCollector($config, [], $moduleConfig);
794804

795805
$routes->add('testing', 'MainRoutes::index');
796806

tests/system/Router/RouterTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public function setUp()
2020
{
2121
parent::setUp();
2222

23-
$this->collection = new RouteCollection(new MockFileLocator(new \Config\Autoload()));
23+
$moduleConfig = new \Config\Modules;
24+
$moduleConfig->enabled = false;
25+
$this->collection = new RouteCollection(new MockFileLocator(new \Config\Autoload()), $moduleConfig);
2426

2527
$routes = [
2628
'users' => 'Users::index',

user_guide_src/source/general/routing.rst

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,3 @@ a valid class/method pair, just like you would show in any route, or a Closure::
540540
{
541541
echo view('my_errors/not_found.html');
542542
});
543-
544-
Discovering Module Routes
545-
-------------------------
546-
547-
If you are using :doc:`modular code </general/modules>`, then this setting will specify whether or not additional
548-
Routes files should be scanned for within each of the PSR4 namespaces defined in **/application/Config/Autoload.php**.
549-
550-
::
551-
552-
$routes->discoverLocal(false);

0 commit comments

Comments
 (0)