diff --git a/ChromePhp.php b/ChromePhp.php index 577b1ce..5e0fc7d 100755 --- a/ChromePhp.php +++ b/ChromePhp.php @@ -1,6 +1,6 @@ + * @author Erik Krause */ class ChromePhp { /** - * @var string + * @var string (ek) */ - const VERSION = '4.1.0'; + const VERSION = '4.3.0'; /** * @var string @@ -38,6 +39,11 @@ class ChromePhp */ const BACKTRACE_LEVEL = 'backtrace_level'; + /** + * @var string (ek) + */ + const LOG_STYLE = 'log_style'; + /** * @var string */ @@ -126,6 +132,13 @@ class ChromePhp */ protected $_processed = array(); + /** + * provide enabled / disabled state + * + * @var bool + */ + protected $_enabled = true; + /** * constructor */ @@ -136,6 +149,31 @@ private function __construct() $this->_json['request_uri'] = $_SERVER['REQUEST_URI']; } + /** + * Enable and disable logging (ek) + * + * @param boolean $Enabled TRUE to enable, FALSE to disable + * @param string $style 'FirePHP' to switch to FirePHP behaviour + * @return void + */ + public function setEnabled($Enabled, $style = '') + { + $this->_enabled = $Enabled; + if ($style) + $this->addSetting(self::LOG_STYLE, $style); + } + + /** + * Check if logging is enabled + * + * @return boolean TRUE if enabled + */ + public function getEnabled() + { + return $this->_enabled; + } + + /** * gets instance of this class * @@ -254,10 +292,22 @@ protected static function _log($type, array $args) return; } + $logger = self::getInstance(); + // not enabled, don't do anything (ek) + if (!($logger->_enabled)) + return; + $logger->_processed = array(); + // FirePHP passes the object name second but displays it first (ek) + if (($logger->getSetting(self::LOG_STYLE) == 'FirePHP') && (count($args) == 2)) { + $args = array_reverse($args); + $args[0] .= ":"; + } + + $logs = array(); foreach ($args as $arg) { $logs[] = $logger->_convert($arg); @@ -391,7 +441,27 @@ protected function _addRow(array $logs, $backtrace, $type) protected function _writeHeader($data) { - header(self::HEADER_NAME . ': ' . $this->_encode($data)); + $encodedData = $this->_encode($data); + if ($encodedData) + header(self::HEADER_NAME . ': ' . $encodedData); + } + + + + /** + * recursively converts data for json_encode (ek) + * + * @param mixed ref $dat + */ + + protected static function _filterArray(&$dat) + { + if (is_resource($dat)) + $dat = print_r($dat, true).", ".get_resource_type($dat); + elseif (is_numeric($dat) && !is_finite($dat)) + $dat = print_r($dat, true) . ", numeric"; // fixes issue #35 + elseif (!is_object($dat) && !is_null($dat) && !is_scalar($dat)) + $dat = print_r($dat, true); } /** @@ -400,8 +470,12 @@ protected function _writeHeader($data) * @param array $data * @return string */ + protected function _encode($data) { + + array_walk_recursive($data, 'self::_filterArray'); //(ek) + return base64_encode(utf8_encode(json_encode($data))); } @@ -444,3 +518,4 @@ public function getSetting($key) return $this->_settings[$key]; } } + diff --git a/README.md b/README.md index 3dd7dbe..c8d9200 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,11 @@ ## Overview -ChromePhp is a PHP library for the Chrome Logger Google Chrome extension. - -This library allows you to log variables to the Chrome console. +ChromePhp is a PHP library to log variables to the Chrome or Firefox devtools console. +For Google Chrome the Chrome Logger extension is needed. ## Requirements - PHP 5 or later -## Installation +## Installation Chrome 1. Install the Chrome extension from: https://chrome.google.com/extensions/detail/noaneddfkdjfnfdakjjmocngnfkfehhd 2. Click the extension icon in the browser to enable it for the current tab's domain 3. Put ChromePhp.php somewhere in your PHP include path @@ -19,5 +18,56 @@ This library allows you to log variables to the Chrome console. ChromePhp::warn('something went wrong!'); ``` +## Installation Firefox +1. Put ChromePhp.php somewhere in your PHP include path +2. Enable Server logging filter in the web console. If Server logging filter is not present (likely from FF 57 on), disable devtools.webconsole.new-frontend-enabled in about:config +or install the Firefox extension from https://addons.mozilla.org/en-US/firefox/addon/chromelogger/ to log to the new console +3. Initialize ChromePhp for FirePHP compatibility + + ```php + include 'ChromePhp.php'; + $firephp = ChromePhp::getInstance(); + $firephp->setEnabled(true, 'FirePHP'); + ``` + The second parameter 'FirePHP' is optional and can be omitted in subsequent calls to setEnabled. FirePHP compatibility mode can be changed by calling + ```php + // disable FirePHP mode + $firephp->addSetting('log_style', ''); + + //enable FirePHP mode + $firephp->addSetting('log_style', 'FirePHP'); + ``` + +4. Log some data + + ```php + $firephp->log($_GET, 'GET variables'); + $firephp->warn('Value out of range'); + ``` + More information can be found here: + http://www.chromelogger.com + +https://developer.mozilla.org/en-US/docs/Tools/Web_Console/Console_messages#Server + +## Use this repository with composer + +To use this repository, change your composer.json to add `ccampbell/chromephp` +in require-dev and add this in your repository list. For example: + +``` +"require-dev": { + "ccampbell/chromephp" : "dev-master" +}, + +"repositories": [ + { + "type" : "vcs", + "url" : "git@github.com:ErikKrause/chromephp.git" + } +] + +``` + +