Skip to content

Commit

Permalink
Allow enabling/disabling DebugBar from middlewares (barryvdh#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
aik099 authored and barryvdh committed Sep 7, 2017
1 parent 322bafa commit 1189ebc
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public function __construct(Request $request, LaravelDebugbar $debugbar)
}
}
}
}
}
20 changes: 3 additions & 17 deletions src/Controllers/OpenHandlerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@

class OpenHandlerController extends BaseController
{

public function handle()
{
$debugbar = $this->debugbar;

if (!$debugbar->isEnabled()) {
$this->app->abort('500', 'Debugbar is not enabled');
}

$openHandler = new OpenHandler($debugbar);

$openHandler = new OpenHandler($this->debugbar);
$data = $openHandler->handle(null, false, false);

return new Response(
Expand All @@ -40,14 +33,7 @@ public function clockwork($id)
'id' => $id,
];

$debugbar = $this->debugbar;

if (!$debugbar->isEnabled()) {
$this->app->abort('500', 'Debugbar is not enabled');
}

$openHandler = new OpenHandler($debugbar);

$openHandler = new OpenHandler($this->debugbar);
$data = $openHandler->handle($request, false, false);

// Convert to Clockwork
Expand Down
13 changes: 10 additions & 3 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function boot()

/** @var Application $app */
$app = $this->app;

// Set custom error handler
if ($app['config']->get('debugbar.error_handler' , false)) {
set_error_handler([$this, 'handleError']);
Expand Down Expand Up @@ -615,7 +615,7 @@ public function getJavascriptRenderer($baseUrl = null, $basePath = null)
public function modifyResponse(Request $request, Response $response)
{
$app = $this->app;
if ($app->runningInConsole() || !$this->isEnabled() || $this->isDebugbarRequest()) {
if (!$this->isEnabled() || $this->isDebugbarRequest()) {
return $response;
}

Expand Down Expand Up @@ -747,7 +747,14 @@ public function modifyResponse(Request $request, Response $response)
public function isEnabled()
{
if ($this->enabled === null) {
$this->enabled = value($this->app['config']->get('debugbar.enabled'));
$config = $this->app['config'];
$configEnabled = value($config->get('debugbar.enabled'));

if ($configEnabled === null) {
$configEnabled = $config->get('app.debug');
}

$this->enabled = $configEnabled && !$this->app->runningInConsole() && !$this->app->environment('testing');
}

return $this->enabled;
Expand Down
42 changes: 42 additions & 0 deletions src/Middleware/DebugbarEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php namespace Barryvdh\Debugbar\Middleware;

use Closure;
use Illuminate\Http\Request;
use Barryvdh\Debugbar\LaravelDebugbar;

class DebugbarEnabled
{
/**
* The DebugBar instance
*
* @var LaravelDebugbar
*/
protected $debugbar;

/**
* Create a new middleware instance.
*
* @param LaravelDebugbar $debugbar
*/
public function __construct(LaravelDebugbar $debugbar)
{
$this->debugbar = $debugbar;
}

/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!$this->debugbar->isEnabled()) {
abort(404);
}

return $next($request);

}
}
6 changes: 6 additions & 0 deletions src/Middleware/InjectDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public function __construct(Container $container, LaravelDebugbar $debugbar)
*/
public function handle($request, Closure $next)
{
if (!$this->debugbar->isEnabled()) {
return $next($request);
}

$this->debugbar->boot();

try {
/** @var \Illuminate\Http\Response $response */
$response = $next($request);
Expand Down
22 changes: 2 additions & 20 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Barryvdh\Debugbar;

use Barryvdh\Debugbar\Middleware\DebugbarEnabled;
use Barryvdh\Debugbar\Middleware\InjectDebugbar;
use DebugBar\DataFormatter\DataFormatter;
use DebugBar\DataFormatter\DataFormatterInterface;
Expand Down Expand Up @@ -62,24 +63,14 @@ function ($app) {
*/
public function boot()
{
$app = $this->app;

$configPath = __DIR__ . '/../config/debugbar.php';
$this->publishes([$configPath => $this->getConfigPath()], 'config');

$enabled = $this->app['config']->get('debugbar.enabled');
if ($enabled === null) {
$enabled = $this->app['config']->get('app.debug');
}

if (! $enabled) {
return;
}

$routeConfig = [
'namespace' => 'Barryvdh\Debugbar\Controllers',
'prefix' => $this->app['config']->get('debugbar.route_prefix'),
'domain' => $this->app['config']->get('debugbar.route_domain'),
'middleware' => [DebugbarEnabled::class],
];

$this->getRouter()->group($routeConfig, function($router) {
Expand All @@ -104,15 +95,6 @@ public function boot()
]);
});

if ($app->runningInConsole() || $app->environment('testing')) {
return;
}

/** @var LaravelDebugbar $debugbar */
$debugbar = $this->app['debugbar'];
$debugbar->enable();
$debugbar->boot();

$this->registerMiddleware(InjectDebugbar::class);
}

Expand Down

0 comments on commit 1189ebc

Please sign in to comment.