Skip to content

Commit

Permalink
Merge pull request #2857 from Instrye/alias-filter-multiple
Browse files Browse the repository at this point in the history
fix. filters alias multiple
  • Loading branch information
lonnieezell authored Apr 21, 2020
2 parents 3e6b851 + 0b838b8 commit e90126f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 28 deletions.
68 changes: 40 additions & 28 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,47 +150,59 @@ public function run(string $uri, string $position = 'before')
throw FilterException::forNoAlias($alias);
}

$class = new $this->config->aliases[$alias]();

if (! $class instanceof FilterInterface)
if (is_array($this->config->aliases[$alias]))
{
$classNames = $this->config->aliases[$alias];
}
else
{
throw FilterException::forIncorrectInterface(get_class($class));
$classNames = [$this->config->aliases[$alias]];
}

if ($position === 'before')
foreach ($classNames as $className)
{
$result = $class->before($this->request, $this->arguments[$alias] ?? null);
$class = new $className();

if ($result instanceof RequestInterface)
if (! $class instanceof FilterInterface)
{
$this->request = $result;
continue;
throw FilterException::forIncorrectInterface(get_class($class));
}

// If the response object was sent back,
// then send it and quit.
if ($result instanceof ResponseInterface)
if ($position === 'before')
{
// short circuit - bypass any other filters
return $result;
}
$result = $class->before($this->request, $this->arguments[$alias] ?? null);

// Ignore an empty result
if (empty($result))
{
continue;
}
if ($result instanceof RequestInterface)
{
$this->request = $result;
continue;
}

return $result;
}
elseif ($position === 'after')
{
$result = $class->after($this->request, $this->response);
// If the response object was sent back,
// then send it and quit.
if ($result instanceof ResponseInterface)
{
// short circuit - bypass any other filters
return $result;
}

// Ignore an empty result
if (empty($result))
{
continue;
}

if ($result instanceof ResponseInterface)
return $result;
}
elseif ($position === 'after')
{
$this->response = $result;
continue;
$result = $class->after($this->request, $this->response);

if ($result instanceof ResponseInterface)
{
$this->response = $result;
continue;
}
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions tests/system/Filters/FiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
require_once __DIR__ . '/fixtures/GoogleEmpty.php';
require_once __DIR__ . '/fixtures/GoogleCurious.php';
require_once __DIR__ . '/fixtures/InvalidClass.php';
require_once __DIR__ . '/fixtures/Multiple1.php';
require_once __DIR__ . '/fixtures/Multiple2.php';

/**
* @backupGlobals enabled
Expand Down Expand Up @@ -832,4 +834,30 @@ public function testSegmentedFilterMatching()
$this->assertEquals($expected, $filters->initialize($uri)->getFilters());
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/2831
*/
public function testFilterAlitasMultiple()
{
$config = [
'aliases' => [
'multipeTest' => [
'CodeIgniter\Filters\fixtures\Multiple1',
'CodeIgniter\Filters\fixtures\Multiple2',
],
],
'globals' => [
'before' => [
'multipeTest',
],
],
];
$filters = new Filters((object) $config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$request = $filters->run($uri, 'before');
$this->assertEquals('http://exampleMultipleURL.com', $request->url);
$this->assertEquals('http://exampleMultipleCSP.com', $request->csp);
}

}
20 changes: 20 additions & 0 deletions tests/system/Filters/fixtures/Multiple1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php namespace CodeIgniter\Filters\fixtures;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

class Multiple1 implements FilterInterface
{

public function before(RequestInterface $request)
{
$request->csp = 'http://exampleMultipleCSP.com';
return $request;
}

public function after(RequestInterface $request, ResponseInterface $response)
{
}

}
20 changes: 20 additions & 0 deletions tests/system/Filters/fixtures/Multiple2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php namespace CodeIgniter\Filters\fixtures;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

class Multiple2 implements FilterInterface
{

public function before(RequestInterface $request)
{
$request->url = 'http://exampleMultipleURL.com';
return $request;
}

public function after(RequestInterface $request, ResponseInterface $response)
{
}

}

0 comments on commit e90126f

Please sign in to comment.