Skip to content

Commit b9dc8f2

Browse files
committed
Initial Lumen support
See barryvdh#334 and barryvdh#337
1 parent 9903b19 commit b9dc8f2

File tree

2 files changed

+367
-0
lines changed

2 files changed

+367
-0
lines changed

src/LumenDebugbar.php

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
<?php namespace Barryvdh\Debugbar;
2+
3+
use Barryvdh\Debugbar\DataCollector\AuthCollector;
4+
use Barryvdh\Debugbar\DataCollector\EventCollector;
5+
use Barryvdh\Debugbar\DataCollector\FilesCollector;
6+
use Barryvdh\Debugbar\DataCollector\LaravelCollector;
7+
use Barryvdh\Debugbar\DataCollector\LogsCollector;
8+
use Barryvdh\Debugbar\DataCollector\QueryCollector;
9+
use Barryvdh\Debugbar\DataCollector\SessionCollector;
10+
use Barryvdh\Debugbar\DataCollector\SymfonyRequestCollector;
11+
use Barryvdh\Debugbar\DataCollector\ViewCollector;
12+
use Barryvdh\Debugbar\Storage\FilesystemStorage;
13+
use DebugBar\Bridge\MonologCollector;
14+
use DebugBar\Bridge\SwiftMailer\SwiftLogCollector;
15+
use DebugBar\Bridge\SwiftMailer\SwiftMailCollector;
16+
use DebugBar\DataCollector\ConfigCollector;
17+
use DebugBar\DataCollector\ExceptionsCollector;
18+
use DebugBar\DataCollector\MemoryCollector;
19+
use DebugBar\DataCollector\MessagesCollector;
20+
use DebugBar\DataCollector\PhpInfoCollector;
21+
use DebugBar\DataCollector\RequestDataCollector;
22+
use DebugBar\DataCollector\TimeDataCollector;
23+
use DebugBar\DebugBar;
24+
use DebugBar\Storage\PdoStorage;
25+
use DebugBar\Storage\RedisStorage;
26+
use Exception;
27+
28+
use Laravel\Lumen\Application;
29+
30+
31+
/**
32+
* Debug bar subclass which adds all without Request and with LaravelCollector.
33+
* Rest is added in Service Provider
34+
*
35+
* @method void emergency($message)
36+
* @method void alert($message)
37+
* @method void critical($message)
38+
* @method void error($message)
39+
* @method void warning($message)
40+
* @method void notice($message)
41+
* @method void info($message)
42+
* @method void debug($message)
43+
* @method void log($message)
44+
*/
45+
class LumenDebugbar extends LaravelDebugbar
46+
{
47+
/**
48+
* The Laravel application instance.
49+
*
50+
* @var Application
51+
*/
52+
protected $app;
53+
54+
/**
55+
* Boot the debugbar (add collectors, renderer and listener)
56+
*/
57+
public function boot()
58+
{
59+
if ($this->booted) {
60+
return;
61+
}
62+
63+
if ($this->isDebugbarRequest()) {
64+
$this->app['session']->reflash();
65+
}
66+
67+
/** @var \Barryvdh\Debugbar\LaravelDebugbar $debugbar */
68+
$debugbar = $this;
69+
/** @var \Illuminate\Foundation\Application $app */
70+
$app = $this->app;
71+
72+
$this->selectStorage($debugbar);
73+
74+
if ($this->shouldCollect('phpinfo', true)) {
75+
$this->addCollector(new PhpInfoCollector());
76+
}
77+
if ($this->shouldCollect('messages', true)) {
78+
$this->addCollector(new MessagesCollector());
79+
}
80+
if ($this->shouldCollect('time', true)) {
81+
$startTime = defined('LARAVEL_START') ? LARAVEL_START : null;
82+
$this->addCollector(new TimeDataCollector($startTime));
83+
84+
$this->app->booted(
85+
function () use ($debugbar, $startTime) {
86+
if ($startTime) {
87+
$debugbar['time']->addMeasure('Booting', $startTime, microtime(true));
88+
}
89+
}
90+
);
91+
92+
93+
$debugbar->startMeasure('application', 'Application');
94+
95+
96+
}
97+
if ($this->shouldCollect('memory', true)) {
98+
$this->addCollector(new MemoryCollector());
99+
}
100+
if ($this->shouldCollect('exceptions', true)) {
101+
try {
102+
$exceptionCollector = new ExceptionsCollector();
103+
$exceptionCollector->setChainExceptions(
104+
$this->app['config']->get('debugbar.options.exceptions.chain', true)
105+
);
106+
$this->addCollector($exceptionCollector);
107+
} catch (\Exception $e) {
108+
}
109+
}
110+
if ($this->shouldCollect('laravel', false)) {
111+
$this->addCollector(new LaravelCollector($this->app));
112+
}
113+
if ($this->shouldCollect('default_request', false)) {
114+
$this->addCollector(new RequestDataCollector());
115+
}
116+
117+
if ($this->shouldCollect('events', false) && isset($this->app['events'])) {
118+
try {
119+
$startTime = defined('LARAVEL_START') ? LARAVEL_START : null;
120+
$eventCollector = new EventCollector($startTime);
121+
$this->addCollector($eventCollector);
122+
$this->app['events']->subscribe($eventCollector);
123+
124+
} catch (\Exception $e) {
125+
$this->addException(
126+
new Exception(
127+
'Cannot add EventCollector to Laravel Debugbar: ' . $e->getMessage(),
128+
$e->getCode(),
129+
$e
130+
)
131+
);
132+
}
133+
}
134+
135+
if ($this->shouldCollect('views', true) && isset($this->app['events'])) {
136+
try {
137+
$collectData = $this->app['config']->get('debugbar.options.views.data', true);
138+
$this->addCollector(new ViewCollector($collectData));
139+
$this->app['events']->listen(
140+
'composing:*',
141+
function ($view) use ($debugbar) {
142+
$debugbar['views']->addView($view);
143+
}
144+
);
145+
} catch (\Exception $e) {
146+
$this->addException(
147+
new Exception(
148+
'Cannot add ViewCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e
149+
)
150+
);
151+
}
152+
}
153+
154+
155+
if ($this->shouldCollect('db', true) && isset($this->app['db'])) {
156+
$db = $this->app['db'];
157+
if ($debugbar->hasCollector('time') && $this->app['config']->get(
158+
'debugbar.options.db.timeline',
159+
false
160+
)
161+
) {
162+
$timeCollector = $debugbar->getCollector('time');
163+
} else {
164+
$timeCollector = null;
165+
}
166+
$queryCollector = new QueryCollector($timeCollector);
167+
168+
if ($this->app['config']->get('debugbar.options.db.with_params')) {
169+
$queryCollector->setRenderSqlWithParams(true);
170+
}
171+
172+
if ($this->app['config']->get('debugbar.options.db.backtrace')) {
173+
$queryCollector->setFindSource(true);
174+
}
175+
176+
if ($this->app['config']->get('debugbar.options.db.explain.enabled')) {
177+
$types = $this->app['config']->get('debugbar.options.db.explain.types');
178+
$queryCollector->setExplainSource(true, $types);
179+
}
180+
181+
if ($this->app['config']->get('debugbar.options.db.hints', true)) {
182+
$queryCollector->setShowHints(true);
183+
}
184+
185+
$this->addCollector($queryCollector);
186+
187+
try {
188+
$db->listen(
189+
function ($query, $bindings, $time, $connectionName) use ($db, $queryCollector) {
190+
$connection = $db->connection($connectionName);
191+
$queryCollector->addQuery((string) $query, $bindings, $time, $connection);
192+
}
193+
);
194+
} catch (\Exception $e) {
195+
$this->addException(
196+
new Exception(
197+
'Cannot add listen to Queries for Laravel Debugbar: ' . $e->getMessage(),
198+
$e->getCode(),
199+
$e
200+
)
201+
);
202+
}
203+
}
204+
205+
206+
if ($this->shouldCollect('logs', false)) {
207+
try {
208+
$file = $this->app['config']->get('debugbar.options.logs.file');
209+
$this->addCollector(new LogsCollector($file));
210+
} catch (\Exception $e) {
211+
$this->addException(
212+
new Exception(
213+
'Cannot add LogsCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e
214+
)
215+
);
216+
}
217+
}
218+
if ($this->shouldCollect('files', false)) {
219+
$this->addCollector(new FilesCollector($app));
220+
}
221+
222+
if ($this->shouldCollect('auth', false)) {
223+
try {
224+
$authCollector = new AuthCollector($app['auth']);
225+
$authCollector->setShowName(
226+
$this->app['config']->get('debugbar.options.auth.show_name')
227+
);
228+
$this->addCollector($authCollector);
229+
} catch (\Exception $e) {
230+
$this->addException(
231+
new Exception(
232+
'Cannot add AuthCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e
233+
)
234+
);
235+
}
236+
}
237+
238+
$renderer = $this->getJavascriptRenderer();
239+
$renderer->setIncludeVendors($this->app['config']->get('debugbar.include_vendors', true));
240+
$renderer->setBindAjaxHandlerToXHR($app['config']->get('debugbar.capture_ajax', true));
241+
242+
$this->booted = true;
243+
}
244+
245+
246+
}

src/LumenServiceProvider.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php namespace Barryvdh\Debugbar;
2+
3+
use Laravel\Lumen\Application;
4+
5+
class LumenServiceProvider extends \Illuminate\Support\ServiceProvider
6+
{
7+
/** @var Application */
8+
protected $app;
9+
10+
/**
11+
* Indicates if loading of the provider is deferred.
12+
*
13+
* @var bool
14+
*/
15+
protected $defer = false;
16+
17+
/**
18+
* Register the service provider.
19+
*
20+
* @return void
21+
*/
22+
public function register()
23+
{
24+
$configPath = __DIR__ . '/../config/debugbar.php';
25+
$this->mergeConfigFrom($configPath, 'debugbar');
26+
27+
$this->app->alias(
28+
'DebugBar\DataFormatter\DataFormatter',
29+
'DebugBar\DataFormatter\DataFormatterInterface'
30+
);
31+
32+
$this->app['debugbar'] = $this->app->share(
33+
function ($app) {
34+
$debugbar = new LumenDebugbar($app);
35+
36+
$sessionManager = $app['session'];
37+
$httpDriver = new SymfonyHttpDriver($sessionManager);
38+
$debugbar->setHttpDriver($httpDriver);
39+
40+
return $debugbar;
41+
}
42+
);
43+
44+
$this->app->alias('debugbar', 'Barryvdh\Debugbar\LaravelDebugbar');
45+
46+
$this->app['command.debugbar.clear'] = $this->app->share(
47+
function ($app) {
48+
return new Console\ClearCommand($app['debugbar']);
49+
}
50+
);
51+
52+
$this->commands(array('command.debugbar.clear'));
53+
}
54+
55+
/**
56+
* Bootstrap the application events.
57+
*
58+
* @return void
59+
*/
60+
public function boot()
61+
{
62+
$app = $this->app;
63+
64+
if ($app->runningInConsole()) {
65+
$this->app['config']->set('debugbar.enabled', false);
66+
}
67+
68+
$routeConfig = [
69+
'namespace' => 'Barryvdh\Debugbar\Controllers',
70+
'prefix' => $this->app['config']->get('debugbar.route_prefix'),
71+
];
72+
73+
$this->app->group($routeConfig, function($router) {
74+
$router->get('open', [
75+
'uses' => 'OpenHandlerController@handle',
76+
'as' => 'debugbar.openhandler',
77+
]);
78+
79+
$router->get('assets/stylesheets', [
80+
'uses' => 'AssetController@css',
81+
'as' => 'debugbar.assets.css',
82+
]);
83+
84+
$router->get('assets/javascript', [
85+
'uses' => 'AssetController@js',
86+
'as' => 'debugbar.assets.js',
87+
]);
88+
});
89+
90+
$enabled = $this->app['config']->get('debugbar.enabled');
91+
92+
// If enabled is null, set from the app.debug value
93+
if (is_null($enabled)) {
94+
$enabled = env('APP_DEBUG');
95+
$this->app['config']->set('debugbar.enabled', $enabled);
96+
}
97+
98+
99+
if ( ! $enabled) {
100+
return;
101+
}
102+
103+
/** @var LaravelDebugbar $debugbar */
104+
$debugbar = $this->app['debugbar'];
105+
$debugbar->boot();
106+
107+
$app->middleware(['Barryvdh\Debugbar\Middleware\Debugbar']);
108+
109+
110+
}
111+
112+
/**
113+
* Get the services provided by the provider.
114+
*
115+
* @return array
116+
*/
117+
public function provides()
118+
{
119+
return array('debugbar', 'command.debugbar.clear');
120+
}
121+
}

0 commit comments

Comments
 (0)