Skip to content

Commit

Permalink
New discoverLocal method on RouteCollection to auto-scan code module …
Browse files Browse the repository at this point in the history
…route files.
  • Loading branch information
lonnieezell committed Jun 29, 2017
1 parent 074850b commit 9a1ffb5
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 67 deletions.
1 change: 1 addition & 0 deletions application/Config/Routes.php
Expand Up @@ -64,6 +64,7 @@
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);
$routes->discoverLocal(false);

/**
* --------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion system/Config/Services.php
Expand Up @@ -457,7 +457,7 @@ public static function routes($getShared = true)
return self::getSharedInstance('routes');
}

return new \CodeIgniter\Router\RouteCollection();
return new \CodeIgniter\Router\RouteCollection(self::locator());
}

//--------------------------------------------------------------------
Expand Down
83 changes: 82 additions & 1 deletion system/Router/RouteCollection.php
Expand Up @@ -35,6 +35,7 @@
* @since Version 3.0.0
* @filesource
*/
use CodeIgniter\Autoloader\FileLocator;

/**
* Class RouteCollection
Expand Down Expand Up @@ -166,15 +167,34 @@ class RouteCollection implements RouteCollectionInterface
*/
protected $currentOptions = null;

/**
* Determines whether locally specified, PSR4
* compatible code is automatically scanned
* for addition routes in a {namespace}/Config/Routes.php file.
*
* @var bool
*/
protected $discoverLocal = false;

/**
* A little performance booster.
* @var bool
*/
protected $didDiscover = false;

protected $fileLocator;

//--------------------------------------------------------------------

/**
* Constructor
*/
public function __construct()
public function __construct(FileLocator $locator)
{
// Get HTTP verb
$this->HTTPVerb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';

$this->fileLocator = $locator;
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -332,6 +352,62 @@ public function get404Override()

//--------------------------------------------------------------------

/**
* If true, will attempt to auto-discover new route files
* based on any PSR4 namespaces that have been set
* in Config/Autoload.php.
*
* @param bool $discover
*
* @return $this
*/
public function discoverLocal(bool $discover)
{
$this->discoverLocal = $discover;

return $this;
}

//--------------------------------------------------------------------

/**
* Will attempt to discover any additional routes, either through
* the local PSR4 namespaces, or through selected Composer packages.
* (Composer coming soon...)
*/
protected function discoverRoutes()
{
if ($this->didDiscover) return;

// We need this var in local scope
// so route files can access it.
$routes = $this;

/*
* Discover Local Files
*/
if ($this->discoverLocal === true)
{
$files = $this->fileLocator->search('Config/Routes.php');

foreach ($files as $file)
{
// Don't include our main file again...
if ($file == APPPATH.'Config/Routes.php') continue;

include $file;
}
}

/*
* Discover Composer files (coming soon)
*/

$this->didDiscover = true;
}

//--------------------------------------------------------------------

/**
* Sets the default constraint to be used in the system. Typically
* for use with the 'resources' method.
Expand Down Expand Up @@ -418,6 +494,11 @@ public function shouldAutoRoute(): bool
*/
public function getRoutes(): array
{
// Since this is the entry point for the Router,
// take a moment to do any route discovery
// we might need to do.
$this->discoverRoutes();

$routes = [];

foreach ($this->routes as $r)
Expand Down
7 changes: 7 additions & 0 deletions tests/_support/Config/Routes.php
@@ -0,0 +1,7 @@
<?php

/**
* This is a simple file to include for testing the RouteCollection class.
*/

$routes->add('testing', 'TestController::index');
3 changes: 2 additions & 1 deletion tests/system/CodeIgniterTest.php
@@ -1,5 +1,6 @@
<?php namespace CodeIgniter;

use CodeIgniter\Autoloader\MockFileLocator;
use CodeIgniter\Router\RouteCollection;
use Config\App;

Expand Down Expand Up @@ -145,7 +146,7 @@ public function testRun404OverrideByClosure()
$_SERVER['argc'] = 2;

// Inject mock router.
$routes = new RouteCollection();
$routes = new RouteCollection(new MockFileLocator(new \Config\Autoload()));
$routes->setAutoRoute(false);
$routes->set404Override(function() {
echo '404 Override by Closure.';
Expand Down
6 changes: 3 additions & 3 deletions tests/system/CommonFunctionsTest.php
Expand Up @@ -70,7 +70,7 @@ public function testRedirectReturnsNamedRouteFirst()
$_SERVER['REQUEST_METHOD'] = 'GET';

$response = $this->createMock(\CodeIgniter\HTTP\Response::class);
$routes = new \CodeIgniter\Router\RouteCollection();
$routes = new \CodeIgniter\Router\RouteCollection(new \CodeIgniter\Autoloader\MockFileLocator(new \Config\Autoload()));
\CodeIgniter\Services::injectMock('response', $response);
\CodeIgniter\Services::injectMock('routes', $routes);

Expand All @@ -87,7 +87,7 @@ public function testRedirectReverseRouting()
$_SERVER['REQUEST_METHOD'] = 'GET';

$response = $this->createMock(\CodeIgniter\HTTP\Response::class);
$routes = new \CodeIgniter\Router\RouteCollection();
$routes = new \CodeIgniter\Router\RouteCollection(new \CodeIgniter\Autoloader\MockFileLocator(new \Config\Autoload()));
\CodeIgniter\Services::injectMock('response', $response);
\CodeIgniter\Services::injectMock('routes', $routes);

Expand All @@ -104,7 +104,7 @@ public function testRedirectNormalRouting()
$_SERVER['REQUEST_METHOD'] = 'GET';

$response = $this->createMock(\CodeIgniter\HTTP\Response::class);
$routes = new \CodeIgniter\Router\RouteCollection();
$routes = new \CodeIgniter\Router\RouteCollection(new \CodeIgniter\Autoloader\MockFileLocator(new \Config\Autoload()));
\CodeIgniter\Services::injectMock('response', $response);
\CodeIgniter\Services::injectMock('routes', $routes);

Expand Down

0 comments on commit 9a1ffb5

Please sign in to comment.