Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,18 @@ private function discoverFilters(): void

$files = $locator->search('Config/Filters.php');

// Our main Filter configs are never included again, see below. Skip them
// by path, as reading and tokenizing them to find that out is wasted work.
$mainFiles = [
APPPATH . 'Config' . DIRECTORY_SEPARATOR . 'Filters.php',
SYSTEMPATH . 'Config' . DIRECTORY_SEPARATOR . 'Filters.php',
];

foreach ($files as $file) {
if (in_array($file, $mainFiles, true)) {
continue;
}

// The $file may not be a class file.
$className = $locator->getClassname($file);

Expand Down
47 changes: 47 additions & 0 deletions tests/system/Filters/FiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Filters;

use CodeIgniter\Autoloader\FileLocator;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Exceptions\FilterException;
use CodeIgniter\Filters\fixtures\GoogleCurious;
Expand Down Expand Up @@ -488,6 +489,52 @@ public function testAllCustomFiltersAreDiscoveredInConstructor(): void
$this->assertContains('test-customfilter', array_keys($configFilters->aliases));
}

public function testDiscoveryDoesNotTokenizeTheDefaultFiltersConfigs(): void
{
service('superglobals')->setServer('REQUEST_METHOD', 'GET');

$locator = new class (service('autoloader')) extends FileLocator {
/**
* @var list<string>
*/
public array $tokenized = [];

public function getClassname(string $file): string
{
$this->tokenized[] = $file;

return parent::getClassname($file);
}
};
Services::injectMock('locator', $locator);

$appConfig = APPPATH . 'Config' . DIRECTORY_SEPARATOR . 'Filters.php';
$systemConfig = SYSTEMPATH . 'Config' . DIRECTORY_SEPARATOR . 'Filters.php';
$testConfig = realpath(TESTPATH . '_support/Config/Filters.php');

// Discovery does find them all, so the checks below are not vacuous.
$found = $locator->search('Config/Filters.php');
$this->assertContains($appConfig, $found);
$this->assertContains($systemConfig, $found);
$this->assertContains($testConfig, $found);

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

$this->assertNotContains($appConfig, $locator->tokenized);
$this->assertNotContains($systemConfig, $locator->tokenized);

// Any other file is still checked and included.
$this->assertContains($testConfig, $locator->tokenized);

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

public function testRunThrowsWithInvalidClassType(): void
{
service('superglobals')->setServer('REQUEST_METHOD', 'GET');
Expand Down
Loading