Skip to content

Commit

Permalink
Merge pull request #5280 from kenjis/fix-module-route-filter
Browse files Browse the repository at this point in the history
fix: module filters are not discovered when using route filters
  • Loading branch information
kenjis committed Nov 7, 2021
2 parents 16c40a4 + a8890e1 commit 6b882e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
4 changes: 2 additions & 2 deletions system/CodeIgniter.php
Expand Up @@ -358,6 +358,8 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
{
$routeFilter = $this->tryToRouteIt($routes);

$uri = $this->determinePath();

// Start up the filters
$filters = Services::filters();

Expand All @@ -375,8 +377,6 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
}
}

$uri = $this->determinePath();

// Never run filters when running through Spark cli
if (! defined('SPARKED')) {
// Run "before" filters
Expand Down
14 changes: 6 additions & 8 deletions system/Filters/Filters.php
Expand Up @@ -107,12 +107,16 @@ public function __construct($config, RequestInterface $request, ResponseInterfac
$this->setResponse($response);

$this->modules = $modules ?? config('Modules');

if ($this->modules->shouldDiscover('filters')) {
$this->discoverFilters();
}
}

/**
* If discoverFilters is enabled in Config then system will try to
* auto-discover custom filters files in Namespaces and allow access to
* the config object via the variable $customfilters as with the routes file
* the config object via the variable $filters as with the routes file
*
* Sample :
* $filters->aliases['custom-auth'] = \Acme\Blob\Filters\BlobAuth::class;
Expand Down Expand Up @@ -211,12 +215,10 @@ public function run(string $uri, string $position = 'before')
* The resulting $this->filters is an array of only filters
* that should be applied to this request.
*
* We go ahead an process the entire tree because we'll need to
* We go ahead and process the entire tree because we'll need to
* run through both a before and after and don't want to double
* process the rows.
*
* @param string $uri
*
* @return Filters
*/
public function initialize(?string $uri = null)
Expand All @@ -225,10 +227,6 @@ public function initialize(?string $uri = null)
return $this;
}

if ($this->modules->shouldDiscover('filters')) {
$this->discoverFilters();
}

$this->processGlobals($uri);
$this->processMethods();
$this->processFilters($uri);
Expand Down
18 changes: 18 additions & 0 deletions tests/system/Filters/FiltersTest.php
Expand Up @@ -458,6 +458,24 @@ public function testCustomFiltersLoad()
$this->assertSame('http://hellowworld.com', $request->url);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4720
*/
public function testAllCustomFiltersAreDiscoveredInConstructor()
{
$_SERVER['REQUEST_METHOD'] = 'GET';

$config = [
'aliases' => [],
'globals' => [],
];
$filtersConfig = $this->createConfigFromArray(FiltersConfig::class, $config);
$filters = $this->createFilters($filtersConfig);

$configFilters = $this->getPrivateProperty($filters, 'config');
$this->assertContains('test-customfilter', array_keys($configFilters->aliases));
}

public function testRunThrowsWithInvalidClassType()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
Expand Down

0 comments on commit 6b882e9

Please sign in to comment.