Skip to content

Commit

Permalink
Initial commit with PHP Debug Bar instead of Tracy
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Oct 14, 2014
1 parent 6c912d1 commit 2bfcd88
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -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]

</IfModule>

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@

$grav = Grav::instance(
array(
'loader' => $loader,
'debugger' => new Debugger(Debugger::PRODUCTION)
'loader' => $loader
)
);

try {
$grav['debugger']->init();
$grav['debugger'];
$grav->process();

} catch (\Exception $e) {
Expand Down
21 changes: 17 additions & 4 deletions system/src/Grav/Common/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function addCss($asset, $priority = 10, $pipeline = true)
}

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;
Expand Down Expand Up @@ -300,7 +300,8 @@ public function addJs($asset, $priority = 10, $pipeline = true)
}

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;
Expand All @@ -317,8 +318,15 @@ public function css($attributes = [])
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));
Expand Down Expand Up @@ -363,7 +371,12 @@ public function js($attributes = [])
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));
Expand Down
182 changes: 140 additions & 42 deletions system/src/Grav/Common/Debugger.php
Original file line number Diff line number Diff line change
@@ -1,78 +1,176 @@
<?php
namespace Grav\Common;

use \Tracy\Debugger as TracyDebugger;
use DebugBar\Bridge\Twig\TraceableTwigEnvironment;
use DebugBar\JavascriptRenderer;
use DebugBar\StandardDebugBar;
//use \Tracy\Debugger as TracyDebugger;

/**
* Class Debugger
* @package Grav\Common
*/
class Debugger
{
const PRODUCTION = TracyDebugger::PRODUCTION;
const DEVELOPMENT = TracyDebugger::DEVELOPMENT;
const DETECT = TracyDebugger::DETECT;
protected $grav;
protected $enabled = false;
protected $timing = false;
protected $twig = false;
protected $debugbar;
protected $renderer;

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 __construct() {
$this->grav = Grav::instance();
$this->debugbar = new StandardDebugBar();
}

public function init()
{
$grav = Grav::instance();

/** @var Config $config */
$config = $grav['config'];
$config = $this->grav['config'];
if ($config->get('system.debugger.enabled')) {
$this->enabled = true;
$this->debugbar->addCollector(new \DebugBar\DataCollector\ConfigCollector((array)$config->get('system')));
}
if ($config->get('system.debugger.timing')) {
$this->timing = true;
}
if ($config->get('system.debugger.twig')) {
$this->twig = true;
}
return $this;
}

TracyDebugger::$logDirectory = $config->get('system.debugger.log.enabled') ? LOG_DIR : null;
TracyDebugger::$maxDepth = $config->get('system.debugger.max_depth');
public function addAssets()
{
if ($this->enabled) {

// Switch debugger into development mode if configured
if ($config->get('system.debugger.enabled')) {
if ($config->get('system.debugger.strict')) {
TracyDebugger::$strictMode = true;
}
$assets = $this->grav['assets'];

$mode = $config->get('system.debugger.mode');
$this->renderer = $this->debugbar->getJavascriptRenderer();
$this->renderer->setIncludeVendors(false);

if (function_exists('ini_set')) {
ini_set('display_errors', !($mode === 'production'));
// 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);
}

if ($mode === 'detect') {
TracyDebugger::$productionMode = self::DETECT;
} elseif ($mode === 'production') {
TracyDebugger::$productionMode = self::PRODUCTION;
} else {
TracyDebugger::$productionMode = self::DEVELOPMENT;
foreach ($js_files as $js) {
$assets->addJs($js);
}
}
return $this;
}

public function addCollector($collector)
{
$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;
}

/**
* Log a message.
*
* @param string $message
*/
public function log($message)
public function startTimer($name, $desription = null)
{
if (TracyDebugger::$logDirectory) {
TracyDebugger::log(sprintf($message, TracyDebugger::timer() * 1000));
if ($this->enabled || $name == 'config' || $name == 'debugger') {
$this->debugbar['time']->startMeasure($name, $desription);
}
return $this;
}

public static function dump($var)
public function stopTimer($name)
{
TracyDebugger::dump($var);
if ($this->enabled || $name == 'config' || $name == 'debugger') {
$this->debugbar['time']->stopMeasure($name);
}
return $this;
}

public static function barDump($var, $title = NULL, array $options = NULL)

public function addMessage($message)
{
TracyDebugger::barDump($var, $title, $options);
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);
// }
}
Loading

0 comments on commit 2bfcd88

Please sign in to comment.