|
| 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 | +} |
0 commit comments