Skip to content

Commit 1189ebc

Browse files
aik099barryvdh
authored andcommitted
Allow enabling/disabling DebugBar from middlewares (barryvdh#713)
1 parent 322bafa commit 1189ebc

File tree

6 files changed

+64
-41
lines changed

6 files changed

+64
-41
lines changed

src/Controllers/BaseController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ public function __construct(Request $request, LaravelDebugbar $debugbar)
3535
}
3636
}
3737
}
38-
}
38+
}

src/Controllers/OpenHandlerController.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,10 @@
66

77
class OpenHandlerController extends BaseController
88
{
9-
9+
1010
public function handle()
1111
{
12-
$debugbar = $this->debugbar;
13-
14-
if (!$debugbar->isEnabled()) {
15-
$this->app->abort('500', 'Debugbar is not enabled');
16-
}
17-
18-
$openHandler = new OpenHandler($debugbar);
19-
12+
$openHandler = new OpenHandler($this->debugbar);
2013
$data = $openHandler->handle(null, false, false);
2114

2215
return new Response(
@@ -40,14 +33,7 @@ public function clockwork($id)
4033
'id' => $id,
4134
];
4235

43-
$debugbar = $this->debugbar;
44-
45-
if (!$debugbar->isEnabled()) {
46-
$this->app->abort('500', 'Debugbar is not enabled');
47-
}
48-
49-
$openHandler = new OpenHandler($debugbar);
50-
36+
$openHandler = new OpenHandler($this->debugbar);
5137
$data = $openHandler->handle($request, false, false);
5238

5339
// Convert to Clockwork

src/LaravelDebugbar.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function boot()
126126

127127
/** @var Application $app */
128128
$app = $this->app;
129-
129+
130130
// Set custom error handler
131131
if ($app['config']->get('debugbar.error_handler' , false)) {
132132
set_error_handler([$this, 'handleError']);
@@ -615,7 +615,7 @@ public function getJavascriptRenderer($baseUrl = null, $basePath = null)
615615
public function modifyResponse(Request $request, Response $response)
616616
{
617617
$app = $this->app;
618-
if ($app->runningInConsole() || !$this->isEnabled() || $this->isDebugbarRequest()) {
618+
if (!$this->isEnabled() || $this->isDebugbarRequest()) {
619619
return $response;
620620
}
621621

@@ -747,7 +747,14 @@ public function modifyResponse(Request $request, Response $response)
747747
public function isEnabled()
748748
{
749749
if ($this->enabled === null) {
750-
$this->enabled = value($this->app['config']->get('debugbar.enabled'));
750+
$config = $this->app['config'];
751+
$configEnabled = value($config->get('debugbar.enabled'));
752+
753+
if ($configEnabled === null) {
754+
$configEnabled = $config->get('app.debug');
755+
}
756+
757+
$this->enabled = $configEnabled && !$this->app->runningInConsole() && !$this->app->environment('testing');
751758
}
752759

753760
return $this->enabled;

src/Middleware/DebugbarEnabled.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php namespace Barryvdh\Debugbar\Middleware;
2+
3+
use Closure;
4+
use Illuminate\Http\Request;
5+
use Barryvdh\Debugbar\LaravelDebugbar;
6+
7+
class DebugbarEnabled
8+
{
9+
/**
10+
* The DebugBar instance
11+
*
12+
* @var LaravelDebugbar
13+
*/
14+
protected $debugbar;
15+
16+
/**
17+
* Create a new middleware instance.
18+
*
19+
* @param LaravelDebugbar $debugbar
20+
*/
21+
public function __construct(LaravelDebugbar $debugbar)
22+
{
23+
$this->debugbar = $debugbar;
24+
}
25+
26+
/**
27+
* Handle an incoming request.
28+
*
29+
* @param Request $request
30+
* @param Closure $next
31+
* @return mixed
32+
*/
33+
public function handle($request, Closure $next)
34+
{
35+
if (!$this->debugbar->isEnabled()) {
36+
abort(404);
37+
}
38+
39+
return $next($request);
40+
41+
}
42+
}

src/Middleware/InjectDebugbar.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public function __construct(Container $container, LaravelDebugbar $debugbar)
4646
*/
4747
public function handle($request, Closure $next)
4848
{
49+
if (!$this->debugbar->isEnabled()) {
50+
return $next($request);
51+
}
52+
53+
$this->debugbar->boot();
54+
4955
try {
5056
/** @var \Illuminate\Http\Response $response */
5157
$response = $next($request);

src/ServiceProvider.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Barryvdh\Debugbar;
22

3+
use Barryvdh\Debugbar\Middleware\DebugbarEnabled;
34
use Barryvdh\Debugbar\Middleware\InjectDebugbar;
45
use DebugBar\DataFormatter\DataFormatter;
56
use DebugBar\DataFormatter\DataFormatterInterface;
@@ -62,24 +63,14 @@ function ($app) {
6263
*/
6364
public function boot()
6465
{
65-
$app = $this->app;
66-
6766
$configPath = __DIR__ . '/../config/debugbar.php';
6867
$this->publishes([$configPath => $this->getConfigPath()], 'config');
6968

70-
$enabled = $this->app['config']->get('debugbar.enabled');
71-
if ($enabled === null) {
72-
$enabled = $this->app['config']->get('app.debug');
73-
}
74-
75-
if (! $enabled) {
76-
return;
77-
}
78-
7969
$routeConfig = [
8070
'namespace' => 'Barryvdh\Debugbar\Controllers',
8171
'prefix' => $this->app['config']->get('debugbar.route_prefix'),
8272
'domain' => $this->app['config']->get('debugbar.route_domain'),
73+
'middleware' => [DebugbarEnabled::class],
8374
];
8475

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

107-
if ($app->runningInConsole() || $app->environment('testing')) {
108-
return;
109-
}
110-
111-
/** @var LaravelDebugbar $debugbar */
112-
$debugbar = $this->app['debugbar'];
113-
$debugbar->enable();
114-
$debugbar->boot();
115-
11698
$this->registerMiddleware(InjectDebugbar::class);
11799
}
118100

0 commit comments

Comments
 (0)