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

feat: add method to disable controller filters #6652

Merged
merged 3 commits into from
Oct 14, 2022
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
91 changes: 56 additions & 35 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ class CodeIgniter
*/
protected ?string $context = null;

/**
* Whether to enable Control Filters.
*/
protected bool $enableFilters = true;

/**
* Constructor.
*/
Expand Down Expand Up @@ -399,6 +404,14 @@ private function isWeb(): bool
return $this->context === 'web';
}

/**
* Disables Controller Filters.
*/
public function disableFilters(): void
{
$this->enableFilters = false;
}

/**
* Handles the main request logic and fires the controller.
*
Expand All @@ -413,35 +426,37 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache

$uri = $this->determinePath();

// Start up the filters
$filters = Services::filters();

// If any filters were specified within the routes file,
// we need to ensure it's active for the current request
if ($routeFilter !== null) {
$multipleFiltersEnabled = config('Feature')->multipleFilters ?? false;
if ($multipleFiltersEnabled) {
$filters->enableFilters($routeFilter, 'before');
$filters->enableFilters($routeFilter, 'after');
} else {
// for backward compatibility
$filters->enableFilter($routeFilter, 'before');
$filters->enableFilter($routeFilter, 'after');
if ($this->enableFilters) {
// Start up the filters
$filters = Services::filters();

// If any filters were specified within the routes file,
// we need to ensure it's active for the current request
if ($routeFilter !== null) {
$multipleFiltersEnabled = config('Feature')->multipleFilters ?? false;
if ($multipleFiltersEnabled) {
$filters->enableFilters($routeFilter, 'before');
$filters->enableFilters($routeFilter, 'after');
} else {
// for backward compatibility
$filters->enableFilter($routeFilter, 'before');
$filters->enableFilter($routeFilter, 'after');
}
}
}

// Run "before" filters
$this->benchmark->start('before_filters');
$possibleResponse = $filters->run($uri, 'before');
$this->benchmark->stop('before_filters');
// Run "before" filters
$this->benchmark->start('before_filters');
$possibleResponse = $filters->run($uri, 'before');
$this->benchmark->stop('before_filters');

// If a ResponseInterface instance is returned then send it back to the client and stop
if ($possibleResponse instanceof ResponseInterface) {
return $returnResponse ? $possibleResponse : $possibleResponse->send();
}
// If a ResponseInterface instance is returned then send it back to the client and stop
if ($possibleResponse instanceof ResponseInterface) {
return $returnResponse ? $possibleResponse : $possibleResponse->send();
}

if ($possibleResponse instanceof Request) {
$this->request = $possibleResponse;
if ($possibleResponse instanceof Request) {
$this->request = $possibleResponse;
}
}

$returned = $this->startController();
Expand All @@ -468,22 +483,28 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
// so it can be used with the output.
$this->gatherOutput($cacheConfig, $returned);

$filters->setResponse($this->response);
if ($this->enableFilters) {
$filters = Services::filters();
$filters->setResponse($this->response);

// After filter debug toolbar requires 'total_execution'.
$this->totalTime = $this->benchmark->getElapsedTime('total_execution');
// After filter debug toolbar requires 'total_execution'.
$this->totalTime = $this->benchmark->getElapsedTime('total_execution');

// Run "after" filters
$this->benchmark->start('after_filters');
$response = $filters->run($uri, 'after');
$this->benchmark->stop('after_filters');
// Run "after" filters
$this->benchmark->start('after_filters');
$response = $filters->run($uri, 'after');
$this->benchmark->stop('after_filters');

if ($response instanceof ResponseInterface) {
$this->response = $response;
if ($response instanceof ResponseInterface) {
$this->response = $response;
}
}

// Skip unnecessary processing for special Responses.
if (! $response instanceof DownloadResponse && ! $response instanceof RedirectResponse) {
if (
! $this->response instanceof DownloadResponse
&& ! $this->response instanceof RedirectResponse
) {
// Cache it without the performance metrics replaced
// so that we can have live speed updates along the way.
// Must be run after filters to preserve the Response headers.
Expand Down
27 changes: 27 additions & 0 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,33 @@ public function testControllersRunFilterByClassName()
$this->resetServices();
}

public function testDisableControllerFilters()
{
$_SERVER['argv'] = ['index.php', 'pages/about'];
$_SERVER['argc'] = 2;

$_SERVER['REQUEST_URI'] = '/pages/about';

// Inject mock router.
$routes = Services::routes();
$routes->add(
'pages/about',
static fn () => Services::incomingrequest()->getBody(),
['filter' => Customfilter::class]
);
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);

ob_start();
$this->codeigniter->disableFilters();
$this->codeigniter->run();
$output = ob_get_clean();

$this->assertStringContainsString('', $output);

$this->resetServices();
}

public function testResponseConfigEmpty()
{
$_SERVER['argv'] = ['index.php', '/'];
Expand Down