Skip to content

Commit

Permalink
Merge pull request #764 from cakephp/ignore-paths
Browse files Browse the repository at this point in the history
Allow ignoring paths.
  • Loading branch information
markstory authored Jun 10, 2020
2 parents 1858160 + cb391ad commit 940a021
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/en/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ Configuration
// Before loading DebugKit
Configure::write('DebugKit.forceEnable', true);

* ``DebugKit.ignorePathsPattern`` - Regex pattern (including delimiter) to ignore paths.
DebugKit won't save data for request URLs that match this regex. Defaults to ``null``::

// Ignore image paths
Configure::write('DebugKit.ignorePathsPattern', '/\.(jpg|png|gif)$/');

* ``DebugKit.ignoreAuthorization`` - Set to true to ignore Cake Authorization plugin for DebugKit requests. Disabled by default.

Database Configuration
Expand Down
13 changes: 11 additions & 2 deletions src/ToolbarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class ToolbarService
],
'forceEnable' => false,
'safeTld' => [],
'ignorePathsPattern' => null,
];

/**
Expand Down Expand Up @@ -213,15 +214,23 @@ public function initializePanels()
*/
public function saveData(ServerRequest $request, ResponseInterface $response)
{
// Skip debugkit requests and requestAction()
$ignorePathsPattern = $this->getConfig('ignorePathsPattern');
$path = $request->getUri()->getPath();
$statusCode = $response->getStatusCode();

if (
strpos($path, 'debug_kit') !== false ||
strpos($path, 'debug-kit') !== false ||
$request->is('requested')
(
$ignorePathsPattern &&
$statusCode >= 200 &&
$statusCode <= 299 &&
preg_match($ignorePathsPattern, $path)
)
) {
return false;
}

$data = [
'url' => $request->getUri()->getPath(),
'content_type' => $response->getHeaderLine('Content-Type'),
Expand Down
34 changes: 34 additions & 0 deletions tests/TestCase/ToolbarServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,40 @@ public function testSaveDataIgnoreDebugKitDashedUrl()
$this->assertFalse($bar->saveData($request, $response));
}

/**
* Test that saveData ignores path that matches "ignorePaths" regex.
*
* @return void
*/
public function testSaveDataIgnorePaths()
{
$request = new Request([
'url' => '/foo.jpg',
'params' => [],
]);
$response = new Response([
'status' => 200,
'type' => 'text/html',
'body' => '<html><title>test</title><body><p>some text</p></body>',
]);

$bar = new ToolbarService($this->events, ['ignorePathsPattern' => '/\.(jpg|png|gif)$/']);
$this->assertFalse($bar->saveData($request, $response));

$request = new Request([
'url' => '/foo.jpg',
'params' => [],
]);
$response = new Response([
'status' => 404,
'type' => 'text/html',
'body' => '<html><title>test</title><body><p>some text</p></body>',
]);

$bar = new ToolbarService($this->events, ['ignorePathsPattern' => '/\.(jpg|png|gif)$/']);
$this->assertNotEmpty($bar->saveData($request, $response));
}

/**
* Test that saveData works
*
Expand Down

0 comments on commit 940a021

Please sign in to comment.