/**
  * @param callable $handler
  *
  * @return callable
  */
 public function __invoke(callable $handler)
 {
     return function (RequestInterface $request, array $options) use($handler) {
         return $handler($request, $options)->then(function (ResponseInterface $response) {
             return $response;
         }, function (GuzzleException $exception) {
             $this->collector->addException($exception);
             throw $exception;
         });
     };
 }
 /**
  * Method to handle error events.
  *
  * @param ErrorEvent $event
  */
 public function onError(ErrorEvent $event)
 {
     // Stop measurement and add exception information.
     $this->stopMeasure($event->getRequest(), $event->getResponse(), $event->getException());
     if ($this->exceptions) {
         $this->exceptions->addException($event->getException());
     }
 }
Example #3
0
 /**
  * @param Container $container
  * @param array $settings
  */
 public function __construct(Container $container, $settings)
 {
     $this->container = $container;
     $this->settings = $settings;
     if ($settings['storage']['enabled']) {
         if (!is_dir($settings['storage']['path'])) {
             mkdir($settings['storage']['path']);
         }
         $this->setStorage(new FileStorage($settings['storage']['path']));
     }
     $collectorsSettings = $settings['collectors'];
     if ($collectorsSettings['phpinfo']) {
         $this->addCollector(new PhpInfoCollector());
     }
     if ($collectorsSettings['messages']) {
         $this->addCollector(new MessagesCollector());
     }
     if ($collectorsSettings['time']) {
         $this->addCollector(new TimeDataCollector());
         $this->startMeasure('app', 'App');
     }
     if ($collectorsSettings['memory']) {
         $this->addCollector(new MemoryCollector());
     }
     if ($collectorsSettings['exceptions']) {
         $exceptionCollector = new ExceptionsCollector();
         $exceptionCollector->setChainExceptions(true);
         $this->addCollector($exceptionCollector);
     }
     if ($collectorsSettings['route']) {
         $this->addCollector(new SlimRouteCollector($container->router, $container->request));
     }
     if ($collectorsSettings['request']) {
         $this->addCollector(new RequestDataCollector());
     }
     $renderer = $this->getJavascriptRenderer();
     $renderer->setBindAjaxHandlerToXHR($settings['capture_ajax']);
 }
 /**
  * Boot the debugbar (add collectors, renderer and listener)
  */
 public function boot()
 {
     if ($this->booted) {
         return;
     }
     if ($this->isDebugbarRequest()) {
         $this->app['session']->reflash();
     }
     /** @var \Barryvdh\Debugbar\LaravelDebugbar $debugbar */
     $debugbar = $this;
     /** @var Application $app */
     $app = $this->app;
     $this->selectStorage($debugbar);
     if ($this->shouldCollect('phpinfo', true)) {
         $this->addCollector(new PhpInfoCollector());
     }
     if ($this->shouldCollect('messages', true)) {
         $this->addCollector(new MessagesCollector());
     }
     if ($this->shouldCollect('time', true)) {
         $startTime = defined('LARAVEL_START') ? LARAVEL_START : null;
         $this->addCollector(new TimeDataCollector($startTime));
         if ($this->isLumen()) {
             $debugbar->startMeasure('application', 'Application');
         } else {
             $this->app->booted(function () use($debugbar, $startTime) {
                 if ($startTime) {
                     $debugbar['time']->addMeasure('Booting', $startTime, microtime(true));
                 }
             });
             //Check if App::before is already called..
             if ($this->app->isBooted()) {
                 $debugbar->startMeasure('application', 'Application');
             } else {
                 $this->app['router']->before(function () use($debugbar) {
                     $debugbar->startMeasure('application', 'Application');
                 });
             }
             $this->app['router']->after(function () use($debugbar) {
                 $debugbar->stopMeasure('application');
                 $debugbar->startMeasure('after', 'After application');
             });
         }
     }
     if ($this->shouldCollect('memory', true)) {
         $this->addCollector(new MemoryCollector());
     }
     if ($this->shouldCollect('exceptions', true)) {
         try {
             $exceptionCollector = new ExceptionsCollector();
             $exceptionCollector->setChainExceptions($this->app['config']->get('debugbar.options.exceptions.chain', true));
             $this->addCollector($exceptionCollector);
         } catch (\Exception $e) {
         }
     }
     if ($this->shouldCollect('laravel', false)) {
         $this->addCollector(new LaravelCollector($this->app));
     }
     if ($this->shouldCollect('default_request', false)) {
         $this->addCollector(new RequestDataCollector());
     }
     if ($this->shouldCollect('events', false) && isset($this->app['events'])) {
         try {
             $startTime = defined('LARAVEL_START') ? LARAVEL_START : null;
             $eventCollector = new EventCollector($startTime);
             $this->addCollector($eventCollector);
             $this->app['events']->subscribe($eventCollector);
         } catch (\Exception $e) {
             $this->addException(new Exception('Cannot add EventCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('views', true) && isset($this->app['events'])) {
         try {
             $collectData = $this->app['config']->get('debugbar.options.views.data', true);
             $this->addCollector(new ViewCollector($collectData));
             $this->app['events']->listen('composing:*', function ($view) use($debugbar) {
                 $debugbar['views']->addView($view);
             });
         } catch (\Exception $e) {
             $this->addException(new Exception('Cannot add ViewCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if (!$this->isLumen() && $this->shouldCollect('route')) {
         try {
             $this->addCollector($this->app->make('Barryvdh\\Debugbar\\DataCollector\\IlluminateRouteCollector'));
         } catch (\Exception $e) {
             $this->addException(new Exception('Cannot add RouteCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if (!$this->isLumen() && $this->shouldCollect('log', true)) {
         try {
             if ($this->hasCollector('messages')) {
                 $logger = new MessagesCollector('log');
                 $this['messages']->aggregate($logger);
                 $this->app['log']->listen(function ($level, $message, $context) use($logger) {
                     try {
                         $logMessage = (string) $message;
                         if (mb_check_encoding($logMessage, 'UTF-8')) {
                             $logMessage .= !empty($context) ? ' ' . json_encode($context) : '';
                         } else {
                             $logMessage = "[INVALID UTF-8 DATA]";
                         }
                     } catch (\Exception $e) {
                         $logMessage = "[Exception: " . $e->getMessage() . "]";
                     }
                     $logger->addMessage('[' . date('H:i:s') . '] ' . "LOG.{$level}: " . $logMessage, $level, false);
                 });
             } else {
                 $this->addCollector(new MonologCollector($this->app['log']->getMonolog()));
             }
         } catch (\Exception $e) {
             $this->addException(new Exception('Cannot add LogsCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('db', true) && isset($this->app['db'])) {
         $db = $this->app['db'];
         if ($debugbar->hasCollector('time') && $this->app['config']->get('debugbar.options.db.timeline', false)) {
             $timeCollector = $debugbar->getCollector('time');
         } else {
             $timeCollector = null;
         }
         $queryCollector = new QueryCollector($timeCollector);
         if ($this->app['config']->get('debugbar.options.db.with_params')) {
             $queryCollector->setRenderSqlWithParams(true);
         }
         if ($this->app['config']->get('debugbar.options.db.backtrace')) {
             $queryCollector->setFindSource(true);
         }
         if ($this->app['config']->get('debugbar.options.db.explain.enabled')) {
             $types = $this->app['config']->get('debugbar.options.db.explain.types');
             $queryCollector->setExplainSource(true, $types);
         }
         if ($this->app['config']->get('debugbar.options.db.hints', true)) {
             $queryCollector->setShowHints(true);
         }
         $this->addCollector($queryCollector);
         try {
             $db->listen(function ($query, $bindings, $time, $connectionName) use($db, $queryCollector) {
                 $connection = $db->connection($connectionName);
                 $queryCollector->addQuery((string) $query, $bindings, $time, $connection);
             });
         } catch (\Exception $e) {
             $this->addException(new Exception('Cannot add listen to Queries for Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('mail', true) && class_exists('Illuminate\\Mail\\MailServiceProvider')) {
         try {
             $mailer = $this->app['mailer']->getSwiftMailer();
             $this->addCollector(new SwiftMailCollector($mailer));
             if ($this->app['config']->get('debugbar.options.mail.full_log') && $this->hasCollector('messages')) {
                 $this['messages']->aggregate(new SwiftLogCollector($mailer));
             }
         } catch (\Exception $e) {
             $this->addException(new Exception('Cannot add MailCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('logs', false)) {
         try {
             $file = $this->app['config']->get('debugbar.options.logs.file');
             $this->addCollector(new LogsCollector($file));
         } catch (\Exception $e) {
             $this->addException(new Exception('Cannot add LogsCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('files', false)) {
         $this->addCollector(new FilesCollector($app));
     }
     if ($this->shouldCollect('auth', false)) {
         try {
             $authCollector = new AuthCollector($app['auth']);
             $authCollector->setShowName($this->app['config']->get('debugbar.options.auth.show_name'));
             $this->addCollector($authCollector);
         } catch (\Exception $e) {
             $this->addException(new Exception('Cannot add AuthCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('gate', false)) {
         try {
             $gateCollector = $this->app->make(GateCollector::class);
             $this->addCollector($gateCollector);
         } catch (\Exception $e) {
             // No Gate collector
         }
     }
     $renderer = $this->getJavascriptRenderer();
     $renderer->setIncludeVendors($this->app['config']->get('debugbar.include_vendors', true));
     $renderer->setBindAjaxHandlerToXHR($app['config']->get('debugbar.capture_ajax', true));
     $this->booted = true;
 }
 /**
  * Boot the debugbar (add collectors, renderer and listener)
  */
 public function boot()
 {
     if ($this->booted) {
         return;
     }
     /** @var \Barryvdh\Debugbar\LaravelDebugbar $debugbar */
     $debugbar = $this;
     /** @var Application $app */
     $app = $this->app;
     $this->selectStorage($debugbar);
     if ($this->shouldCollect('phpinfo', true)) {
         $this->addCollector(new PhpInfoCollector());
     }
     if ($this->shouldCollect('messages', true)) {
         $this->addCollector(new MessagesCollector());
     }
     if ($this->shouldCollect('time', true)) {
         $this->addCollector(new TimeDataCollector());
         if (!$this->isLumen()) {
             $this->app->booted(function () use($debugbar) {
                 $startTime = $this->app['request']->server('REQUEST_TIME_FLOAT');
                 if ($startTime) {
                     $debugbar['time']->addMeasure('Booting', $startTime, microtime(true));
                 }
             });
         }
         $debugbar->startMeasure('application', 'Application');
     }
     if ($this->shouldCollect('memory', true)) {
         $this->addCollector(new MemoryCollector());
     }
     if ($this->shouldCollect('exceptions', true)) {
         try {
             $exceptionCollector = new ExceptionsCollector();
             $exceptionCollector->setChainExceptions($this->app['config']->get('debugbar.options.exceptions.chain', true));
             $this->addCollector($exceptionCollector);
         } catch (\Exception $e) {
         }
     }
     if ($this->shouldCollect('laravel', false)) {
         $this->addCollector(new LaravelCollector($this->app));
     }
     if ($this->shouldCollect('default_request', false)) {
         $this->addCollector(new RequestDataCollector());
     }
     if ($this->shouldCollect('events', false) && isset($this->app['events'])) {
         try {
             $startTime = $this->app['request']->server('REQUEST_TIME_FLOAT');
             $eventCollector = new EventCollector($startTime);
             $this->addCollector($eventCollector);
             $this->app['events']->subscribe($eventCollector);
         } catch (\Exception $e) {
             $this->addThrowable(new Exception('Cannot add EventCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('views', true) && isset($this->app['events'])) {
         try {
             $collectData = $this->app['config']->get('debugbar.options.views.data', true);
             $this->addCollector(new ViewCollector($collectData));
             $this->app['events']->listen('composing:*', function ($view) use($debugbar) {
                 $debugbar['views']->addView($view);
             });
         } catch (\Exception $e) {
             $this->addThrowable(new Exception('Cannot add ViewCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if (!$this->isLumen() && $this->shouldCollect('route')) {
         try {
             $this->addCollector($this->app->make('Barryvdh\\Debugbar\\DataCollector\\IlluminateRouteCollector'));
         } catch (\Exception $e) {
             $this->addThrowable(new Exception('Cannot add RouteCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if (!$this->isLumen() && $this->shouldCollect('log', true)) {
         try {
             if ($this->hasCollector('messages')) {
                 $logger = new MessagesCollector('log');
                 $this['messages']->aggregate($logger);
                 $this->app['log']->listen(function ($level, $message, $context) use($logger) {
                     try {
                         $logMessage = (string) $message;
                         if (mb_check_encoding($logMessage, 'UTF-8')) {
                             $logMessage .= !empty($context) ? ' ' . json_encode($context) : '';
                         } else {
                             $logMessage = "[INVALID UTF-8 DATA]";
                         }
                     } catch (\Exception $e) {
                         $logMessage = "[Exception: " . $e->getMessage() . "]";
                     }
                     $logger->addMessage('[' . date('H:i:s') . '] ' . "LOG.{$level}: " . $logMessage, $level, false);
                 });
             } else {
                 $this->addCollector(new MonologCollector($this->app['log']->getMonolog()));
             }
         } catch (\Exception $e) {
             $this->addThrowable(new Exception('Cannot add LogsCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('db', true) && isset($this->app['db'])) {
         $db = $this->app['db'];
         if ($debugbar->hasCollector('time') && $this->app['config']->get('debugbar.options.db.timeline', false)) {
             $timeCollector = $debugbar->getCollector('time');
         } else {
             $timeCollector = null;
         }
         $queryCollector = new QueryCollector($timeCollector);
         if ($this->app['config']->get('debugbar.options.db.with_params')) {
             $queryCollector->setRenderSqlWithParams(true);
         }
         if ($this->app['config']->get('debugbar.options.db.backtrace')) {
             $queryCollector->setFindSource(true);
         }
         if ($this->app['config']->get('debugbar.options.db.explain.enabled')) {
             $types = $this->app['config']->get('debugbar.options.db.explain.types');
             $queryCollector->setExplainSource(true, $types);
         }
         if ($this->app['config']->get('debugbar.options.db.hints', true)) {
             $queryCollector->setShowHints(true);
         }
         $this->addCollector($queryCollector);
         try {
             $db->listen(function ($query, $bindings = null, $time = null, $connectionName = null) use($db, $queryCollector) {
                 // Laravel 5.2 changed the way some core events worked. We must account for
                 // the first argument being an "event object", where arguments are passed
                 // via object properties, instead of individual arguments.
                 if ($query instanceof \Illuminate\Database\Events\QueryExecuted) {
                     $bindings = $query->bindings;
                     $time = $query->time;
                     $connection = $query->connection;
                     $query = $query->sql;
                 } else {
                     $connection = $db->connection($connectionName);
                 }
                 $queryCollector->addQuery((string) $query, $bindings, $time, $connection);
             });
         } catch (\Exception $e) {
             $this->addThrowable(new Exception('Cannot add listen to Queries for Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('mail', true) && class_exists('Illuminate\\Mail\\MailServiceProvider')) {
         try {
             $mailer = $this->app['mailer']->getSwiftMailer();
             $this->addCollector(new SwiftMailCollector($mailer));
             if ($this->app['config']->get('debugbar.options.mail.full_log') && $this->hasCollector('messages')) {
                 $this['messages']->aggregate(new SwiftLogCollector($mailer));
             }
         } catch (\Exception $e) {
             $this->addThrowable(new Exception('Cannot add MailCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('logs', false)) {
         try {
             $file = $this->app['config']->get('debugbar.options.logs.file');
             $this->addCollector(new LogsCollector($file));
         } catch (\Exception $e) {
             $this->addThrowable(new Exception('Cannot add LogsCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('files', false)) {
         $this->addCollector(new FilesCollector($app));
     }
     if ($this->shouldCollect('auth', false)) {
         try {
             if ($this->checkVersion('5.2')) {
                 // fix for compatibility with Laravel 5.2.*
                 $guards = array_keys($this->app['config']->get('auth.guards'));
                 $authCollector = new MultiAuthCollector($app['auth'], $guards);
             } else {
                 $authCollector = new AuthCollector($app['auth']);
             }
             $authCollector->setShowName($this->app['config']->get('debugbar.options.auth.show_name'));
             $this->addCollector($authCollector);
         } catch (\Exception $e) {
             $this->addThrowable(new Exception('Cannot add AuthCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('gate', false)) {
         try {
             $gateCollector = $this->app->make('Barryvdh\\Debugbar\\DataCollector\\GateCollector');
             $this->addCollector($gateCollector);
         } catch (\Exception $e) {
             // No Gate collector
         }
     }
     $renderer = $this->getJavascriptRenderer();
     $renderer->setIncludeVendors($this->app['config']->get('debugbar.include_vendors', true));
     $renderer->setBindAjaxHandlerToXHR($app['config']->get('debugbar.capture_ajax', true));
     $this->booted = true;
 }
 /**
  * Add an exception to the collector.
  *
  * @param \Exception $exception The exception.
  *
  * @return void
  */
 protected static function addException($exception)
 {
     self::$collector->addException($exception);
 }
 /**
  * 启动debugbar: 设置collector
  */
 public function boot()
 {
     if (!$this->isEnabled()) {
         return;
     }
     $debugbar = $this;
     if (!$this->isDataPersisted()) {
         $this->selectStorage($debugbar);
         // for normal request and debugbar request both
     }
     if ($this->booted) {
         return;
     }
     $this->booted = true;
     if (isset($_REQUEST['_url']) and $_REQUEST['_url'] == '/favicon.ico' || isset($_SERVER['REQUEST_URI']) and $_SERVER['REQUEST_URI'] == '/favicon.ico' || !$this->isEnabled()) {
         return;
     }
     // only for normal request
     if ($this->shouldCollect('phpinfo', true)) {
         $this->addCollector(new PhpInfoCollector());
     }
     if ($this->shouldCollect('messages', true)) {
         $this->addCollector(new MessagesCollector());
     }
     if ($this->shouldCollect('memory', true)) {
         $this->addCollector(new MemoryCollector());
     }
     if ($this->shouldCollect('default_request', false)) {
         $this->addCollector(new RequestDataCollector());
     }
     if ($this->shouldCollect('exceptions', true)) {
         try {
             $exceptionCollector = new ExceptionsCollector();
             $exceptionCollector->setChainExceptions($this->config->options->exceptions->get('chain', true));
             $this->addCollector($exceptionCollector);
         } catch (\Exception $e) {
             $this->addException($e);
         }
     }
     if ($this->shouldCollect('time', true)) {
         if (defined('PHALCON_START')) {
             $startTime = PHALCON_START;
         } else {
             $startTime = isset($_SERVER["REQUEST_TIME_FLOAT"]) ? $_SERVER["REQUEST_TIME_FLOAT"] : $_SERVER["REQUEST_TIME"];
         }
         $this->addCollector(new TimeDataCollector($startTime));
     }
     if ($this->shouldCollect('route') && $this->di->has('router') && $this->di->has('dispatcher')) {
         try {
             $this->addCollector(new RouteCollector($this->di));
         } catch (\Exception $e) {
             $this->addException(new Exception('Cannot add RouteCollector to Phalcon Debugbar: ' . $e->getMessage(), $e->getCode(), $e));
         }
     }
     if ($this->shouldCollect('log', false) && $this->di->has('log')) {
         $this->addCollector(new LogsCollector($this->di, $this->config->options->log->get('aggregate', false), $this->config->options->log->get('formatter', 'line')));
     }
     $this->attachServices();
     $renderer = $this->getJavascriptRenderer();
     $renderer->setIncludeVendors($this->config->get('include_vendors', true));
     $renderer->setBindAjaxHandlerToXHR($this->config->get('capture_ajax', true));
 }