diff --git a/docs/en/index.rst b/docs/en/index.rst index c771a8756..aab4ab73f 100644 --- a/docs/en/index.rst +++ b/docs/en/index.rst @@ -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 diff --git a/src/ToolbarService.php b/src/ToolbarService.php index c5e9131df..fe5c24b6c 100644 --- a/src/ToolbarService.php +++ b/src/ToolbarService.php @@ -67,6 +67,7 @@ class ToolbarService ], 'forceEnable' => false, 'safeTld' => [], + 'ignorePathsPattern' => null, ]; /** @@ -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'), diff --git a/tests/TestCase/ToolbarServiceTest.php b/tests/TestCase/ToolbarServiceTest.php index d12a30742..a2b6755e4 100644 --- a/tests/TestCase/ToolbarServiceTest.php +++ b/tests/TestCase/ToolbarServiceTest.php @@ -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' => 'test

some text

', + ]); + + $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' => 'test

some text

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