Skip to content

Commit

Permalink
Run the toolbar filter as the last one. Fixes #3470
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Aug 10, 2020
1 parent e8ead29 commit 38d295b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
10 changes: 10 additions & 0 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ public function initialize(string $uri = null)
$this->processMethods();
$this->processFilters($uri);

// Set the toolbar filter to the last position to be executed
if (in_array('toolbar', $this->filters['after']) &&
($count = count($this->filters['after'])) > 1 &&
$this->filters['after'][$count - 1] !== 'toolbar'
)
{
array_splice($this->filters['after'], array_search('toolbar', $this->filters['after']), 1);
$this->filters['after'][] = 'toolbar';
}

$this->initialized = true;

return $this;
Expand Down
37 changes: 37 additions & 0 deletions tests/system/Filters/FiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,43 @@ public function testProcessMethodProcessesCombined()

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

public function testProcessMethodProcessesCombinedAfterForToolbar()
{
$_SERVER['REQUEST_METHOD'] = 'GET';

$config = [
'globals' => [
'after' => [
'toolbar',
'bazg',
],
],
'methods' => [
'get' => ['bar'],
],
'filters' => [
'foof' => [
'after' => ['admin/*'],
],
],
];
$filters = new Filters((object) $config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$expected = [
'before' => ['bar'],
'after' => [
'bazg',
'foof',
'toolbar',
],
];

$this->assertEquals($expected, $filters->initialize($uri)->getFilters());
}

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

public function testRunThrowsWithInvalidAlias()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
Expand Down
4 changes: 3 additions & 1 deletion user_guide_src/source/incoming/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,6 @@ In this example, the array ``['dual', 'noreturn']`` will be passed in ``$argumen
Provided Filters
****************

Three filters are bundled with CodeIgniter4: Honeypot, Security, and DebugToolbar.
Three filters are bundled with CodeIgniter4: ``Honeypot``, ``Security``, and ``DebugToolbar``.

.. note:: The filters are executed in the declared order that is defined in the config file, but there is one exception to this and it concerns the ``DebugToolbar``, which is always executed last. This is because ``DebugToolbar`` should be able to register everything that happens in other filters.

0 comments on commit 38d295b

Please sign in to comment.