Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global enable / disable, very basic FirePHP compatibility #55

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 79 additions & 4 deletions ChromePhp.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2010-2013 Craig Campbell
* Copyright 2010-2013 Craig Campbell, 2016 Erik Krause
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,13 +20,14 @@
*
* @package ChromePhp
* @author Craig Campbell <[email protected]>
* @author Erik Krause <[email protected]>
*/
class ChromePhp
{
/**
* @var string
* @var string (ek)
*/
const VERSION = '4.1.0';
const VERSION = '4.3.0';

/**
* @var string
Expand All @@ -38,6 +39,11 @@ class ChromePhp
*/
const BACKTRACE_LEVEL = 'backtrace_level';

/**
* @var string (ek)
*/
const LOG_STYLE = 'log_style';

/**
* @var string
*/
Expand Down Expand Up @@ -126,6 +132,13 @@ class ChromePhp
*/
protected $_processed = array();

/**
* provide enabled / disabled state
*
* @var bool
*/
protected $_enabled = true;

/**
* constructor
*/
Expand All @@ -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
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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)));
}

Expand Down Expand Up @@ -444,3 +518,4 @@ public function getSetting($key)
return $this->_settings[$key];
}
}

58 changes: 54 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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" : "[email protected]:ErikKrause/chromephp.git"
}
]

```