Skip to content

Commit 940a021

Browse files
authored
Merge pull request #764 from cakephp/ignore-paths
Allow ignoring paths.
2 parents 1858160 + cb391ad commit 940a021

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

docs/en/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Configuration
5050
// Before loading DebugKit
5151
Configure::write('DebugKit.forceEnable', true);
5252

53+
* ``DebugKit.ignorePathsPattern`` - Regex pattern (including delimiter) to ignore paths.
54+
DebugKit won't save data for request URLs that match this regex. Defaults to ``null``::
55+
56+
// Ignore image paths
57+
Configure::write('DebugKit.ignorePathsPattern', '/\.(jpg|png|gif)$/');
58+
5359
* ``DebugKit.ignoreAuthorization`` - Set to true to ignore Cake Authorization plugin for DebugKit requests. Disabled by default.
5460

5561
Database Configuration

src/ToolbarService.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class ToolbarService
6767
],
6868
'forceEnable' => false,
6969
'safeTld' => [],
70+
'ignorePathsPattern' => null,
7071
];
7172

7273
/**
@@ -213,15 +214,23 @@ public function initializePanels()
213214
*/
214215
public function saveData(ServerRequest $request, ResponseInterface $response)
215216
{
216-
// Skip debugkit requests and requestAction()
217+
$ignorePathsPattern = $this->getConfig('ignorePathsPattern');
217218
$path = $request->getUri()->getPath();
219+
$statusCode = $response->getStatusCode();
220+
218221
if (
219222
strpos($path, 'debug_kit') !== false ||
220223
strpos($path, 'debug-kit') !== false ||
221-
$request->is('requested')
224+
(
225+
$ignorePathsPattern &&
226+
$statusCode >= 200 &&
227+
$statusCode <= 299 &&
228+
preg_match($ignorePathsPattern, $path)
229+
)
222230
) {
223231
return false;
224232
}
233+
225234
$data = [
226235
'url' => $request->getUri()->getPath(),
227236
'content_type' => $response->getHeaderLine('Content-Type'),

tests/TestCase/ToolbarServiceTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,40 @@ public function testSaveDataIgnoreDebugKitDashedUrl()
164164
$this->assertFalse($bar->saveData($request, $response));
165165
}
166166

167+
/**
168+
* Test that saveData ignores path that matches "ignorePaths" regex.
169+
*
170+
* @return void
171+
*/
172+
public function testSaveDataIgnorePaths()
173+
{
174+
$request = new Request([
175+
'url' => '/foo.jpg',
176+
'params' => [],
177+
]);
178+
$response = new Response([
179+
'status' => 200,
180+
'type' => 'text/html',
181+
'body' => '<html><title>test</title><body><p>some text</p></body>',
182+
]);
183+
184+
$bar = new ToolbarService($this->events, ['ignorePathsPattern' => '/\.(jpg|png|gif)$/']);
185+
$this->assertFalse($bar->saveData($request, $response));
186+
187+
$request = new Request([
188+
'url' => '/foo.jpg',
189+
'params' => [],
190+
]);
191+
$response = new Response([
192+
'status' => 404,
193+
'type' => 'text/html',
194+
'body' => '<html><title>test</title><body><p>some text</p></body>',
195+
]);
196+
197+
$bar = new ToolbarService($this->events, ['ignorePathsPattern' => '/\.(jpg|png|gif)$/']);
198+
$this->assertNotEmpty($bar->saveData($request, $response));
199+
}
200+
167201
/**
168202
* Test that saveData works
169203
*

0 commit comments

Comments
 (0)