From 2bfcd88bfd53c3096fad0359234484b6f158e37a Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Mon, 13 Oct 2014 22:23:13 -0600 Subject: [PATCH] Initial commit with PHP Debug Bar instead of Tracy --- .htaccess | 2 +- composer.json | 1 + index.php | 5 +- system/src/Grav/Common/Assets.php | 21 ++- system/src/Grav/Common/Debugger.php | 202 +++++++++++++++++++++------- system/src/Grav/Common/Grav.php | 40 ++++-- 6 files changed, 199 insertions(+), 72 deletions(-) diff --git a/.htaccess b/.htaccess index 82978114e..0419abea4 100755 --- a/.htaccess +++ b/.htaccess @@ -31,7 +31,7 @@ RewriteRule ^bin/(.*)$ error [R=301,L] RewriteRule ^system/(.*)$ error [R=301,L] # Block vendor/ -RewriteRule ^vendor/(.*)$ error [R=301,L] +# RewriteRule ^vendor/(.*)$ error [R=301,L] diff --git a/composer.json b/composer.json index 814430144..69bdbafd0 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "symfony/event-dispatcher": "~2.5", "doctrine/cache": "~1.3", "tracy/tracy": "2.3.*@dev", + "maximebf/debugbar": ">=1.0.0", "gregwar/image": "~2.0", "ircmaxell/password-compat": "1.0.*", "mrclay/minify": "dev-master", diff --git a/index.php b/index.php index 2133f6219..bc1c6b310 100644 --- a/index.php +++ b/index.php @@ -22,13 +22,12 @@ if (!ini_get('date.timezone')) { $grav = Grav::instance( array( - 'loader' => $loader, - 'debugger' => new Debugger(Debugger::PRODUCTION) + 'loader' => $loader ) ); try { - $grav['debugger']->init(); + $grav['debugger']; $grav->process(); } catch (\Exception $e) { diff --git a/system/src/Grav/Common/Assets.php b/system/src/Grav/Common/Assets.php index 81de19219..44aa8a83c 100644 --- a/system/src/Grav/Common/Assets.php +++ b/system/src/Grav/Common/Assets.php @@ -249,7 +249,7 @@ class Assets } if( !array_key_exists($asset, $this->css)) { - $this->css[$asset] = ['asset'=>$asset, 'priority'=>$priority, 'pipeline'=>$pipeline]; + $this->css[$asset] = ['asset'=>$asset, 'priority'=>$priority, 'order' => count($this->css), 'pipeline'=>$pipeline]; } return $this; @@ -300,7 +300,8 @@ class Assets } if( !array_key_exists($asset, $this->js)) { - $this->js[$asset] = ['asset' => $asset, 'priority' => $priority, 'pipeline' => $pipeline]; + + $this->js[$asset] = ['asset' => $asset, 'priority' => $priority, 'order' => count($this->js), 'pipeline' => $pipeline]; } return $this; @@ -317,8 +318,15 @@ class Assets if( ! $this->css) return null; + if (self::$grav) + // Sort array by priorities (larger priority first) - usort($this->css, function ($a, $b) {return $a['priority'] - $b['priority'];}); + usort($this->css, function ($a, $b) { + if ($a['priority'] == $b['priority']) { + return $b['order'] - $a['order']; + } + return $a['priority'] - $b['priority']; + }); $this->css = array_reverse($this->css); $attributes = $this->attributes(array_merge([ 'type' => 'text/css', 'rel' => 'stylesheet' ], $attributes)); @@ -363,7 +371,12 @@ class Assets return null; // Sort array by priorities (larger priority first) - usort($this->js, function ($a, $b) {return $a['priority'] - $b['priority'];}); + usort($this->js, function ($a, $b) { + if ($a['priority'] == $b['priority']) { + return $b['order'] - $a['order']; + } + return $a['priority'] - $b['priority']; + }); $this->js = array_reverse($this->js); $attributes = $this->attributes(array_merge([ 'type' => 'text/javascript' ], $attributes)); diff --git a/system/src/Grav/Common/Debugger.php b/system/src/Grav/Common/Debugger.php index 93aafee67..f29c63a7a 100644 --- a/system/src/Grav/Common/Debugger.php +++ b/system/src/Grav/Common/Debugger.php @@ -1,7 +1,10 @@ grav = Grav::instance(); + $this->debugbar = new StandardDebugBar(); } public function init() { - $grav = Grav::instance(); - - /** @var Config $config */ - $config = $grav['config']; - - TracyDebugger::$logDirectory = $config->get('system.debugger.log.enabled') ? LOG_DIR : null; - TracyDebugger::$maxDepth = $config->get('system.debugger.max_depth'); - - // Switch debugger into development mode if configured + $config = $this->grav['config']; if ($config->get('system.debugger.enabled')) { - if ($config->get('system.debugger.strict')) { - TracyDebugger::$strictMode = true; - } - - $mode = $config->get('system.debugger.mode'); - - if (function_exists('ini_set')) { - ini_set('display_errors', !($mode === 'production')); - } - - if ($mode === 'detect') { - TracyDebugger::$productionMode = self::DETECT; - } elseif ($mode === 'production') { - TracyDebugger::$productionMode = self::PRODUCTION; - } else { - TracyDebugger::$productionMode = self::DEVELOPMENT; - } - + $this->enabled = true; + $this->debugbar->addCollector(new \DebugBar\DataCollector\ConfigCollector((array)$config->get('system'))); } - } - - /** - * Log a message. - * - * @param string $message - */ - public function log($message) - { - if (TracyDebugger::$logDirectory) { - TracyDebugger::log(sprintf($message, TracyDebugger::timer() * 1000)); + if ($config->get('system.debugger.timing')) { + $this->timing = true; } + if ($config->get('system.debugger.twig')) { + $this->twig = true; + } + return $this; } - public static function dump($var) + public function addAssets() { - TracyDebugger::dump($var); + if ($this->enabled) { + + $assets = $this->grav['assets']; + + $this->renderer = $this->debugbar->getJavascriptRenderer(); + $this->renderer->setIncludeVendors(false); + + // Get the required CSS files + list($css_files, $js_files) = $this->renderer->getAssets(null, JavascriptRenderer::RELATIVE_URL); + foreach ($css_files as $css) { + $assets->addCss($css); + } + + foreach ($js_files as $js) { + $assets->addJs($js); + } + } + return $this; } - public static function barDump($var, $title = NULL, array $options = NULL) + public function addCollector($collector) { - TracyDebugger::barDump($var, $title, $options); + $this->debugbar->addCollector($collector); + return $this; } + + public function getCollector($collector) + { + return $this->debugbar->getCollector($collector); + } + + public function render() + { + if ($this->enabled) { + echo $this->renderer->render(); + } + return $this; + } + + public function startTimer($name, $desription = null) + { + if ($this->enabled || $name == 'config' || $name == 'debugger') { + $this->debugbar['time']->startMeasure($name, $desription); + } + return $this; + } + + public function stopTimer($name) + { + if ($this->enabled || $name == 'config' || $name == 'debugger') { + $this->debugbar['time']->stopMeasure($name); + } + return $this; + } + + + public function addMessage($message) + { + if ($this->enabled) { + $this->debugbar['messages']->addMessage($message); + } + return $this; + } + + +// const PRODUCTION = TracyDebugger::PRODUCTION; +// const DEVELOPMENT = TracyDebugger::DEVELOPMENT; +// const DETECT = TracyDebugger::DETECT; + +// public function __construct($mode = self::PRODUCTION) +// { +// // Start the timer and enable debugger in production mode as we do not have system configuration yet. +// // Debugger catches all errors and logs them, for example if the script doesn't have write permissions. +//// TracyDebugger::timer(); +//// TracyDebugger::enable($mode, is_dir(LOG_DIR) ? LOG_DIR : null); +// } +// +// public function init() +// { +// +// +// /** @var Config $config */ +// $config = $grav['config']; +// +// TracyDebugger::$logDirectory = $config->get('system.debugger.log.enabled') ? LOG_DIR : null; +// TracyDebugger::$maxDepth = $config->get('system.debugger.max_depth'); +// +// // Switch debugger into development mode if configured +// if ($config->get('system.debugger.enabled')) { +// if ($config->get('system.debugger.strict')) { +// TracyDebugger::$strictMode = true; +// } +// +// $mode = $config->get('system.debugger.mode'); +// +// if (function_exists('ini_set')) { +// ini_set('display_errors', !($mode === 'production')); +// } +// +// if ($mode === 'detect') { +// TracyDebugger::$productionMode = self::DETECT; +// } elseif ($mode === 'production') { +// TracyDebugger::$productionMode = self::PRODUCTION; +// } else { +// TracyDebugger::$productionMode = self::DEVELOPMENT; +// } +// +// } +// } +// +// /** +// * Log a message. +// * +// * @param string $message +// */ +// public function log($message) +// { +// if (TracyDebugger::$logDirectory) { +// TracyDebugger::log(sprintf($message, TracyDebugger::timer() * 1000)); +// } +// } +// +// public static function dump($var) +// { +// TracyDebugger::dump($var); +// } +// +// public static function barDump($var, $title = NULL, array $options = NULL) +// { +// TracyDebugger::barDump($var, $title, $options); +// } } diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index c04d4f784..d24e05cd3 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -15,9 +15,7 @@ use Grav\Common\Page\Medium; * @author Andy Miller * @link http://www.rockettheme.com * @license http://opensource.org/licenses/MIT - * @version 0.8.0 * - * Originally based on Pico by Gilbert Pellegrom - http://pico.dev7studios.com * Influenced by Pico, Stacey, Kirby, PieCrust and other great platforms... */ class Grav extends Container @@ -55,6 +53,10 @@ class Grav extends Container $container['grav'] = $container; + $container['debugger'] = function ($c) { + return new Debugger($c); + }; + $container['uri'] = function ($c) { return new Uri($c); }; @@ -147,15 +149,23 @@ class Grav extends Container ob_start(); // Initialize configuration. + $this['debugger']->startTimer('config', 'Configuration'); $this['config']->init(); + $this['debugger']->stopTimer('config'); + $this['debugger']->startTimer('debugger', 'Debugger'); + $this['debugger']->init(); + $this['debugger']->stopTimer('debugger'); + + $this['debugger']->startTimer('plugins', 'Plugins'); $this['plugins']->init(); - $this->fireEvent('onPluginsInitialized'); + $this['debugger']->stopTimer('plugins'); + $this['debugger']->startTimer('themes', 'Themes'); $this['themes']->init(); - $this->fireEvent('onThemeInitialized'); + $this['debugger']->stopTimer('themes'); $task = $this['task']; if ($task) { @@ -163,28 +173,36 @@ class Grav extends Container } $this['assets']->init(); - + $this['debugger']->addAssets(); $this->fireEvent('onAssetsInitialized'); + $this['debugger']->startTimer('twig', 'Twig'); $this['twig']->init(); - $this['pages']->init(); + $this['debugger']->stopTimer('twig'); + $this['debugger']->startTimer('pages', 'Pages'); + $this['pages']->init(); $this->fireEvent('onPagesInitialized'); + $this['debugger']->stopTimer('pages'); $this->fireEvent('onPageInitialized'); - // Process whole page as required - $this->output = $this['output']; + + // Process whole page as required + $this['debugger']->startTimer('render', 'Render'); + $this->output = $this['output']; $this->fireEvent('onOutputGenerated'); + $this['debugger']->stopTimer('render'); // Set the header type $this->header(); - echo $this->output; + $this['debugger']->render(); $this->fireEvent('onOutputRendered'); + register_shutdown_function([$this, 'shutdown']); } @@ -235,9 +253,7 @@ class Grav extends Container */ public function header() { - /** @var Uri $uri */ - $uri = $this['uri']; - header('Content-type: ' . $this->mime($uri->extension())); + header('Content-type: ' . $this->mime($this['uri']->extension())); } /**