Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new interface FilterInterfaceWithArguments #3217

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 85 additions & 0 deletions system/Filters/FilterInterfaceWithArguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014-2019 British Columbia Institute of Technology
* Copyright (c) 2019-2020 CodeIgniter Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright 2019-2020 CodeIgniter Foundation
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 4.0.0
* @filesource
*/

namespace CodeIgniter\Filters;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

/**
* Filter interface
*/
interface FilterInterfaceWithArguments extends FilterInterface
{

/**
* Do whatever processing this filter needs to do.
* By default it should not return anything during
* normal execution. However, when an abnormal state
* is found, it should return an instance of
* CodeIgniter\HTTP\Response. If it does, script
* execution will end and that Response will be
* sent back to the client, allowing for error pages,
* redirects, etc.
*
* @param \CodeIgniter\HTTP\RequestInterface $request
* @param array $arguments
*
* @return mixed
*/
public function before(RequestInterface $request, array $arguments = []);

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

/**
* Allows After filters to inspect and modify the response
* object as needed. This method does not allow any way
* to stop execution of other after filters, short of
* throwing an Exception or Error.
*
* @param \CodeIgniter\HTTP\RequestInterface $request
* @param \CodeIgniter\HTTP\ResponseInterface $response
* @param array $arguments
*
* @return mixed
*/
public function after(RequestInterface $request, ResponseInterface $response, array $arguments = []);

//--------------------------------------------------------------------
}
25 changes: 21 additions & 4 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,21 @@ public function run(string $uri, string $position = 'before')
{
$class = new $className();

if (! $class instanceof FilterInterface)
if ((! $class instanceof FilterInterface) && (! $class instanceof FilterInterfaceWithArguments))
{
throw FilterException::forIncorrectInterface(get_class($class));
}

if ($position === 'before')
{
$result = $class->before($this->request, $this->arguments[$alias] ?? null);
if ($class instanceof FilterInterfaceWithArguments)
{
$result = $class->before($this->request, $this->arguments[$alias] ?? []);
}
else
{
$result = $class->before($this->request, $this->arguments[$alias] ?? null);
}

if ($result instanceof RequestInterface)
{
Expand All @@ -196,7 +203,14 @@ public function run(string $uri, string $position = 'before')
}
elseif ($position === 'after')
{
$result = $class->after($this->request, $this->response);
if ($class instanceof FilterInterfaceWithArguments)
{
$result = $class->after($this->request, $this->response, $this->arguments[$alias] ?? []);
}
else
{
$result = $class->after($this->request, $this->response);
}

if ($result instanceof ResponseInterface)
{
Expand Down Expand Up @@ -309,7 +323,10 @@ public function enableFilter(string $name, string $when = 'before')
// Get parameters and clean name
if (strpos($name, ':') !== false)
{
list($name, $params) = explode(':', $name);
[
$name,
$params,
] = explode(':', $name);

$params = explode(',', $params);
array_walk($params, function (&$item) {
Expand Down
96 changes: 96 additions & 0 deletions tests/system/Filters/FiltersWithArgumentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
namespace CodeIgniter\Filters;

use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Exceptions\FilterException;
use CodeIgniter\HTTP\ResponseInterface;

require_once __DIR__ . '/fixtures/ArgumentsFilter.php';

/**
* @backupGlobals enabled
*/
class FiltersWithArgumentsTest extends \CodeIgniter\Test\CIUnitTestCase
{

protected $request;
protected $response;

protected function setUp(): void
{
parent::setUp();

$this->request = Services::request();
$this->response = Services::response();
}

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

$config = [
'aliases' => ['args' => 'CodeIgniter\Filters\fixtures\ArgumentsFilter'],
'globals' => [
'before' => [],
'after' => [],
],
];

$filters = new Filters((object) $config, $this->request, $this->response);

$filters = $filters->initialize('admin/foo/bar');

$filters->enableFilter('args:admin , super', 'before');

$found = $filters->getFilters();

$this->assertTrue(in_array('args', $found['before']));
$this->assertEquals(['admin', 'super'], $filters->getArguments('args'));
$this->assertEquals(['args' => ['admin', 'super']], $filters->getArguments());
}

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

$config = [
'aliases' => ['args' => 'CodeIgniter\Filters\fixtures\ArgumentsFilter'],
'globals' => [
'before' => ['args'],
'after' => ['args'],
],
];

$filters = new Filters((object) $config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$response = $filters->run($uri, 'before');
$this->assertEquals('You gave before() no arguments', $response);
$response = $filters->run($uri, 'after');
$this->assertEquals('You gave after() no arguments', $response->getBody());
}

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

$config = [
'aliases' => ['args' => 'CodeIgniter\Filters\fixtures\ArgumentsFilter'],
'globals' => [
'before' => ['args'],
'after' => ['args'],
],
];

$filters = new Filters((object) $config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$filters = $filters->initialize('admin/foo/bar');
$filters->enableFilter('args:admin,root', 'before');
$response = $filters->run($uri, 'before');
$this->assertEquals('You gave before() arguments admin,root', $response);
$response = $filters->run($uri, 'after');
$this->assertEquals('You gave after() arguments admin,root', $response->getBody());
}

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

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

class ArgumentsFilter implements FilterInterfaceWithArguments
{

public function after(RequestInterface $request, ResponseInterface $response, $arguments = [])
{
if (is_null($arguments) || count($arguments) === 0)
{
$response->setBody('You gave after() no arguments');
}
else
{
$response->setBody('You gave after() arguments ' . join(',', $arguments));
}
return $response;
}

public function before(RequestInterface $request, $arguments = [])
{
if (is_null($arguments) || count($arguments) === 0)
{
return 'You gave before() no arguments';
}
else
{
return 'You gave before() arguments ' . join(',', $arguments);
}
}

}