Skip to content

Commit

Permalink
Many squashed commits
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdotcom committed Feb 26, 2015
1 parent 51f2b2a commit 5df587b
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 159 deletions.
30 changes: 19 additions & 11 deletions Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,7 @@ public function __construct($cfg = array(), $errorHandler = null)
if (!isset(self::$instance)) {
self::$instance = $this;
}
$files = array(
'ErrorHandler.php',
'Output.php',
'Utilities.php',
'VarDump.php',
'VarDumpArray.php',
'VarDumpObject.php',
);
foreach ($files as $file) {
require_once dirname(__FILE__).'/'.$file;
}
spl_autoload_register(array($this, 'autoloader'));
$this->utilities = new Utilities();
$this->output = new Output(array(), $this->data);
$this->varDump = new VarDump(array(), $this->utilities);
Expand All @@ -109,6 +99,24 @@ public function __construct($cfg = array(), $errorHandler = null)
return;
}

/**
* Debug class autoloader
*
* @param string $className classname to attempt to load
*
* @return void
*/
protected function autoloader($className)
{
$className = ltrim($className, '\\'); // leading backslash _shouldn't_ have been passed
if (preg_match('/^(.*?)\\\\([^\\\\]+)$/', $className, $matches) && $matches[1] === __NAMESPACE__) {
$filePath = __DIR__.'/'.$matches[2].'.php';
if (file_exists($filePath)) {
require_once $filePath;
}
}
}

/**
* Log a message and stack trace to console if first argument is false.
*
Expand Down
4 changes: 2 additions & 2 deletions ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function __construct($cfg = array())
'emailMask' => E_ERROR | E_PARSE | E_COMPILE_ERROR | E_WARNING | E_USER_ERROR | E_USER_NOTICE,
'emailTraceMask' => E_WARNING | E_USER_ERROR | E_USER_NOTICE,
'continueToPrevHandler' => true, // if there was a prev error handler
'emailThrottleFile' => dirname(__FILE__).'/error_emails.json',
'emailThrottleFile' => __DIR__.'/error_emails.json',
// set onError to something callable, will receive error array
// shortcut for registerOnErrorFunction()
'onError' => null,
Expand Down Expand Up @@ -527,7 +527,7 @@ public function setErrorCaller($caller = 'notPassed', $offset = 1)
* Catch Fatal Error ( if PHP >= 5.2 )
*
* @return void
* @requires PHP 5.2.0
* @requires PHP >= 5.2.0 / should be met as class requires PHP >= 5.3.0 (namespaces)
*/
public function shutdownFunction()
{
Expand Down
144 changes: 8 additions & 136 deletions Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Output

private $cfg = array();
private $data = array();
private $debug = null;
private $debug;

/**
* Constructor
Expand All @@ -31,9 +31,9 @@ public function __construct($cfg = array(), &$data = array())
$this->debug = Debug::getInstance();
$this->cfg = array(
'css' => '', // additional "override" css
'filepathCss' => dirname(__FILE__).'/css/Debug.css',
'filepathScript' => dirname(__FILE__).'/js/Debug.jquery.min.js',
'firephpInc' => dirname(__FILE__).'/FirePHP.class.php',
'filepathCss' => __DIR__.'/css/Debug.css',
'filepathScript' => __DIR__.'/js/Debug.jquery.min.js',
'firephpInc' => __DIR__.'/FirePHP.class.php',
'firephpOptions' => array(
'useNativeJsonEncode' => true,
'includeLineNumbers' => false,
Expand Down Expand Up @@ -292,144 +292,17 @@ public function output()
if ($outputAs == 'html') {
$return = $this->outputAsHtml();
} elseif ($outputAs == 'firephp') {
$this->outputAsFirephp();
$this->uncollapseErrors();
$outputFirephp = new OutputFirephp($this->data);
$outputFirephp->output();
$return = null;
} elseif ($outputAs == 'script') {
$this->uncollapseErrors();
$return = $this->outputAsScript();
}
return $return;
}

/**
* Pass the log to FirePHP methods
*
* @return void
*/
protected function outputAsFirephp()
{
if (!file_exists($this->cfg['firephpInc'])) {
return;
}
require_once $this->cfg['firephpInc'];
$this->firephp = \FirePHP::getInstance(true);
$this->firephp->setOptions($this->cfg['firephpOptions']);
$this->firephpMethods = get_class_methods($this->firephp);
if (!empty($this->data['alert'])) {
$alert = str_replace('<br />', ", \n", $this->data['alert']);
array_unshift($this->data['log'], array('error', $alert));
}
$this->uncollapseErrors();
foreach ($this->data['log'] as $args) {
$method = array_shift($args);
$this->outputFirephpLogEntry($method, $args);
}
while ($this->data['groupDepth'] > 0) {
$this->firephp->groupEnd();
$this->data['groupDepth']--;
}
return;
}

/**
* output a log entry to Firephp
*
* @param string $method method
* @param array $args args
*
* @return void
*/
protected function outputFirephpLogEntry($method, $args)
{
$opts = array();
if (in_array($method, array('error','warn'))) {
$meta = $this->getMetaArg($args);
if ($meta && isset($meta['file'])) {
$opts = array(
'File' => $end['file'],
'Line' => $end['line'],
);
}
}
foreach ($args as $k => $arg) {
$args[$k] = $this->debug->varDump->dump($arg, 'firephp');
}
if (in_array($method, array('group','groupCollapsed','groupEnd'))) {
list($method, $args, $opts) = $this->outputFirephpGroupMethod($method, $args);
} elseif ($method == 'table' && is_array($args[0])) {
$label = isset($args[1])
? $args[1]
: 'table';
$keys = $this->debug->utilities->arrayColkeys($args[0]);
$table = array();
$table[] = $keys;
array_unshift($table[0], '');
foreach ($args[0] as $k => $row) {
$values = array($k);
foreach ($keys as $key) {
$values[] = isset($row[$key])
? $row[$key]
: null;
}
$table[] = $values;
}
$args = array($label, $table);
} elseif ($method == 'table') {
$method = 'log';
} else {
if (count($args) > 1) {
$label = array_shift($args);
if (count($args) > 1) {
$args = array( implode(', ', $args) );
}
$args[] = $label;
} elseif (is_string($args[0])) {
$args[0] = strip_tags($args[0]);
}
}
if (!in_array($method, $this->firephpMethods)) {
$method = 'log';
}
if ($opts) {
// opts array needs to be 2nd arg for group method, 3rd arg for all others
if ($method !== 'group' && count($args) == 1) {
$args[] = null;
}
$args[] = $opts;
}
call_user_func_array(array($this->firephp,$method), $args);
return;
}

/**
* handle firephp output of group, groupCollapsed, & groupEnd
*
* @param string $method method
* @param array $args args passed to method
*
* @return array [$method, $args, $opts]
*/
protected function outputFirephpGroupMethod($method, $args = array())
{
$opts = array();
if (in_array($method, array('group','groupCollapsed'))) {
$firephpMethod = 'group';
$this->data['groupDepth']++;
$opts = array(
'Collapsed' => $method == 'groupCollapsed', // collapse both group and groupCollapsed
);
if (empty($args)) {
$args[] = 'group';
} elseif (count($args) > 1) {
$more = array_splice($args, 1);
$args[0] .= ' - '.implode(', ', $more);
}
} elseif ($method == 'groupEnd') {
$firephpMethod = 'groupEnd';
$this->data['groupDepth']--;
}
return array($firephpMethod, $args, $opts);
}

/**
* return log entry for writing to file
*
Expand Down Expand Up @@ -628,7 +501,6 @@ protected function outputHtmlGroupMethod($method, $args = array())
*/
protected function outputAsScript()
{
$this->uncollapseErrors();
$str = '<script type="text/javascript">'."\n";
$str .= 'console.groupCollapsed("PHP");'."\n";
foreach ($this->data['log'] as $args) {
Expand Down
Loading

0 comments on commit 5df587b

Please sign in to comment.