Skip to content
Merged
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
1 change: 0 additions & 1 deletion app/Config/Modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class Modules extends BaseModules
*/
public $aliases = [
'events',
'filters', // @deprecated 4.4.2 Use Registrar instead.
'registrars',
'routes',
'services',
Expand Down
2 changes: 2 additions & 0 deletions docs/changelogs/v4.8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ Release Date: Unreleased
- `CodeIgniter\Filters\Filters::$arguments` (deprecated since v4.6.0)
- `CodeIgniter\Filters\Filters::$argumentsClass` (deprecated since v4.6.0)
- `CodeIgniter\Filters\Filters::getArguments()` (deprecated since v4.6.0)
- `CodeIgniter\Filters\Filters::discoverFilters()` and the auto-discovery of **Config/Filters.php** files (deprecated since v4.4.2)
- `CodeIgniter\Filters\Filters::$modules` and the `$modules` constructor parameter, which were only used by the auto-discovery

- **HTTP:** Removed the following deprecated properties and methods:
- `CodeIgniter\HTTP\Message::getHeaders()` (deprecated since v4.0.5)
Expand Down
61 changes: 21 additions & 40 deletions docs/general/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class Autoload extends AutoloadConfig
Many times, you will need to specify the full namespace to files you want to include, but CodeIgniter can be configured to make integrating modules into your applications simpler by automatically discovering many different file types, including:

- [Events](../extending/events.md)
- [Filters](../incoming/filters.md)
- [Registrars](configuration.md#registrars)
- [Route files](../incoming/routing.md)
- [Services](../concepts/services.md)
Expand Down Expand Up @@ -211,51 +210,33 @@ When working with modules, it can be a problem if the routes in the application

### Filters

!!! danger "Deprecated since version 4.4.2"
Modules register their [filters](../incoming/filters.md) through a [Registrar](configuration.md#registrars), like the following:

!!! note "Note"
This feature is deprecated. Use [Registrars](configuration.md#registrars) instead, like the following:

```php
<?php
```php
<?php

namespace CodeIgniter\Shield\Config;
namespace CodeIgniter\Shield\Config;

use CodeIgniter\Shield\Filters\SessionAuth;
use CodeIgniter\Shield\Filters\TokenAuth;
use CodeIgniter\Shield\Filters\SessionAuth;
use CodeIgniter\Shield\Filters\TokenAuth;

class Registrar
class Registrar
{
/**
* Registers the Shield filters.
*
* @return array<string, array<string, string>>
*/
public static function Filters(): array
{
/**
* Registers the Shield filters.
*
* @return array<string, array<string, string>>
*/
public static function Filters(): array
{
return [
'aliases' => [
'session' => SessionAuth::class,
'tokens' => TokenAuth::class,
],
];
}
return [
'aliases' => [
'session' => SessionAuth::class,
'tokens' => TokenAuth::class,
],
];
}
```

By default, [filters](../incoming/filters.md) are automatically scanned for within modules. This can be turned off in the **Modules** config file, described above.

!!! note "Note"
Since the files are being included into the current scope, the `$filters` instance is already defined for you. It will cause errors if you attempt to redefine that class.

In the module's **Config/Filters.php** file, you need to define the aliases of the filters you use:

```php
<?php

use App\Filters\MenusFilter;

$filters->aliases['menus'] = MenusFilter::class;
}
```

### Controllers
Expand Down
3 changes: 3 additions & 0 deletions docs/installation/upgrade_480.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ The following files received significant changes (including deprecations or visu
- **app/Config/Mimes.php**
- `Config\Mimes::$mimes` added a new key `md` for Markdown files.

- **app/Config/Modules.php**
- `Config\Modules::$aliases` no longer lists `filters`, since the auto-discovery of Filters was removed.

- **app/Config/Routing.php**
- `Config\Routing::$placeholderSamples` was added to provide sample values for custom route placeholders so the `spark routes` command can resolve their filters.

Expand Down
1 change: 0 additions & 1 deletion psalm-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
'tests/_support/View/Views',
];
$excludeFiles = [
'tests/_support/Config/Filters.php',
'tests/_support/Config/Routes.php',
];

Expand Down
5 changes: 0 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@
<code><![CDATA[$count]]></code>
</LoopInvalidation>
</file>
<file src="tests/_support/Config/Filters.php">
<UndefinedGlobalVariable>
<code><![CDATA[$filters]]></code>
</UndefinedGlobalVariable>
</file>
<file src="tests/_support/Config/Routes.php">
<UndefinedGlobalVariable>
<code><![CDATA[$routes]]></code>
Expand Down
51 changes: 1 addition & 50 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use CodeIgniter\HTTP\ResponseInterface;
use Config\Feature;
use Config\Filters as FiltersConfig;
use Config\Modules;

class Filters
{
Expand All @@ -44,15 +43,6 @@ class Filters
*/
protected $response;

/**
* The Config\Modules instance
*
* @var Modules
*
* @deprecated 4.4.2 Only used by the deprecated auto-discovery of Filters.
*/
protected $modules;

/**
* Whether we've done initial processing on the filter lists.
*
Expand Down Expand Up @@ -123,51 +113,12 @@ class Filters

/**
* @param FiltersConfig $config
* @param Modules|null $modules Deprecated. Only used by the deprecated auto-discovery of Filters.
*/
public function __construct($config, RequestInterface $request, ResponseInterface $response, ?Modules $modules = null)
public function __construct($config, RequestInterface $request, ResponseInterface $response)
{
$this->config = $config;
$this->request = &$request;
$this->setResponse($response);

$this->modules = $modules instanceof Modules ? $modules : new 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 $filters as with the routes file.
*
* Sample:
* $filters->aliases['custom-auth'] = \Acme\Blob\Filters\BlobAuth::class;
*
* @deprecated 4.4.2 Use Registrar instead.
*/
private function discoverFilters(): void
{
$locator = service('locator');

// for access by custom filters
$filters = $this->config;

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

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

// Don't include our main Filter config again...
if ($className === FiltersConfig::class || $className === BaseFiltersConfig::class) {
continue;
}

include $file;
}
}

/**
Expand Down
3 changes: 1 addition & 2 deletions system/Test/FilterTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ trait FilterTestTrait

/**
* The Filters configuration to use.
* Extracted for access to aliases
* during Filters::discoverFilters().
* Extracted for access to aliases.
*
* @var FiltersConfig|null
*/
Expand Down
21 changes: 0 additions & 21 deletions tests/_support/Config/Filters.php

This file was deleted.

10 changes: 6 additions & 4 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,17 @@ public function testRegisterSameFilterTwiceWithDifferentArgument(): void
$router = service('router', $routes, service('incomingrequest'));
Services::injectMock('router', $router);

/** @var FiltersConfig $filterConfig */
$filterConfig = config('Filters');
$filterConfig->filters = [
$filters = config('Filters');

$filters->aliases['test-customfilter'] = Customfilter::class;

$filters->filters = [
// Set filter with argument.
'test-customfilter:arg1' => [
'before' => ['pages/*'],
],
];
service('filters', $filterConfig);
service('filters', $filters);

ob_start();
$this->codeigniter->run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private function createFilters(array $config = []): Filters
];
$filtersConfig = $this->createConfigFromArray(FiltersConfig::class, $config);

return new Filters($filtersConfig, $this->request, $this->response, $this->moduleConfig);
return new Filters($filtersConfig, $this->request, $this->response);
}

public function testFindGlobalsFilters(): void
Expand Down
40 changes: 0 additions & 40 deletions tests/system/Filters/FiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,46 +450,6 @@ public function testRunThrowsWithInvalidAlias(): void
$filters->run($uri);
}

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

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

$uri = 'admin/foo/bar';
$request = $filters->run($uri, 'before');

$this->assertSame('http://hellowworld.com', $request->getBody());

$this->resetServices();
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4720
*/
public function testAllCustomFiltersAreDiscoveredInConstructor(): void
{
service('superglobals')->setServer('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(): void
{
service('superglobals')->setServer('REQUEST_METHOD', 'GET');
Expand Down
3 changes: 2 additions & 1 deletion tests/system/Test/FeatureTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PHPUnit\Framework\Attributes\BackupGlobals;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Filters\RedirectFilter;

/**
* @internal
Expand Down Expand Up @@ -99,7 +100,7 @@ public function testCallGetAndFilterReturnsResponse(): void
'GET',
'admin',
static fn (): string => 'Admin Area',
['filter' => 'test-redirectfilter'],
['filter' => RedirectFilter::class],
],
]);
$response = $this->get('admin');
Expand Down
Loading