From 5df587b5237c43456db1a51a0ba486bf13418802 Mon Sep 17 00:00:00 2001 From: Brad Kent Date: Thu, 26 Feb 2015 11:37:25 -0600 Subject: [PATCH] Many squashed commits --- Debug.php | 30 +++++--- ErrorHandler.php | 4 +- Output.php | 144 +++---------------------------------- OutputFirephp.php | 166 +++++++++++++++++++++++++++++++++++++++++++ README.md | 2 +- VarDump.php | 1 - composer.json | 2 +- tests/Debug_Test.php | 15 ++-- 8 files changed, 205 insertions(+), 159 deletions(-) create mode 100644 OutputFirephp.php diff --git a/Debug.php b/Debug.php index cec665c4..c56f50f5 100644 --- a/Debug.php +++ b/Debug.php @@ -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); @@ -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. * diff --git a/ErrorHandler.php b/ErrorHandler.php index 39fdd4bd..c1301eb7 100644 --- a/ErrorHandler.php +++ b/ErrorHandler.php @@ -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, @@ -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() { diff --git a/Output.php b/Output.php index 8d0bc9fa..7c282f59 100644 --- a/Output.php +++ b/Output.php @@ -18,7 +18,7 @@ class Output private $cfg = array(); private $data = array(); - private $debug = null; + private $debug; /** * Constructor @@ -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, @@ -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('
', ", \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 * @@ -628,7 +501,6 @@ protected function outputHtmlGroupMethod($method, $args = array()) */ protected function outputAsScript() { - $this->uncollapseErrors(); $str = '