diff --git a/src/Debugger.php b/src/Debugger.php index 418d022..7cc2853 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -33,6 +33,11 @@ class Debugger */ protected $event; + /** + * @var bool + */ + protected $disable = false; + /** * Create a new Debugger service. * @@ -87,6 +92,16 @@ public function stopProfiling($name) $this->event->dispatch(new StopProfiling($name)); } + /** + * Disable debugger output on demand for a specific response + * + * @param bool $disable + */ + public function disableOutput($disable) + { + $this->disable = $disable; + } + /** * Profile action. * @@ -137,7 +152,7 @@ protected function needToUpdateResponse(Response $response) $isJsonResponse = $response instanceof JsonResponse || $response->headers->contains('content-type', 'application/json'); - return $isJsonResponse && !$this->storage->isEmpty(); + return ! $this->disable && $isJsonResponse && !$this->storage->isEmpty(); } /** diff --git a/src/Support/helpers.php b/src/Support/helpers.php index b3c9d79..004d27d 100644 --- a/src/Support/helpers.php +++ b/src/Support/helpers.php @@ -15,6 +15,19 @@ function lad() } } +if (! function_exists('lad_disable_output')) { + /** + * Disable output for a single response. + * + * @param bool $disavle + * @return void + */ + function lad_disable_output($disable) + { + app(Debugger::class)->disableOutput($disable); + } +} + if (! function_exists('lad_pr_start')) { /** * Start profiling event. diff --git a/tests/DebuggerTest.php b/tests/DebuggerTest.php index 6594ab5..070b917 100644 --- a/tests/DebuggerTest.php +++ b/tests/DebuggerTest.php @@ -245,4 +245,23 @@ public function it_can_set_a_new_response_key() $this->assertEquals($new_response_key, $debugger->getResponseKey(), 'Response key was not changed from "'.$debugger->getResponseKey().'" to "'.$new_response_key.'"'); } + + /** @test */ + public function it_can_be_disabled_for_a_response() + { + $this->app['router']->get('foo', function () { + lad_disable_output(true); + + return response()->json(['foo' => 'bar']); + }); + + $this->json('get', '/foo') + ->assertStatus(200) + ->assertJsonMissing([ + 'debug', + ]) + ->assertJsonFragment([ + 'event' => 'test', + ]); + } }