Skip to content

Add on demand disabling of debugger output #30

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion src/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class Debugger
*/
protected $event;

/**
* @var bool
*/
protected $disable = false;

/**
* Create a new Debugger service.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions tests/DebuggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
}
}