Created
September 30, 2011 16:01
-
-
Save SeanJA/1254212 to your computer and use it in GitHub Desktop.
debugging in the console from php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @method void error() error(string $name, mixed $var) | |
| * @method void exception() exception(string $name, mixed $var) | |
| * @method void info() info(string $name, mixed $var) | |
| * @method void log() log(string $name, mixed $var) | |
| * @method void trace() trace(string $name, mixed $var) | |
| * @method void warn() warn(string $name, mixed $var) | |
| */ | |
| class JSDebug { | |
| function __construct() { | |
| echo '<script type="text/javascript">' . PHP_EOL; | |
| /// this is for IE and other browsers w/o console | |
| echo 'if (!window.console) console = {};'; | |
| echo 'console.log = console.log || function(){};'; | |
| echo 'console.warn = console.warn || function(){};'; | |
| echo 'console.error = console.error || function(){};'; | |
| echo 'console.info = console.info || function(){};'; | |
| echo 'console.debug = console.debug || function(){};'; | |
| echo '</script>'; | |
| /// end of IE | |
| } | |
| /** | |
| * | |
| * @param type $name | |
| * @param type $arguments | |
| */ | |
| public function __call($name, $arguments) { | |
| $arguments[] = $name; | |
| call_user_method_array('debug', $this, $arguments); | |
| } | |
| /** | |
| * Spit out some debug info | |
| * @param type $name | |
| * @param type $var | |
| * @param type $type | |
| */ | |
| private function debug($name, $var = null, $type = 'log') { | |
| echo '<script type="text/javascript">' . PHP_EOL; | |
| echo 'console.log("' . $name . '");' . PHP_EOL; | |
| if (!empty($var)) { | |
| if (is_object($var) || is_array($var)) { | |
| $object = json_encode($var); | |
| echo 'var object' . preg_replace('~[^A-Z|0-9]~i', "_", $name) . ' = \'' . str_replace("'", "\'", $object) . '\';' . PHP_EOL; | |
| echo 'var val' . preg_replace('~[^A-Z|0-9]~i', "_", $name) . ' = eval("(" + object' . preg_replace('~[^A-Z|0-9]~i', "_", $name) . ' + ")" );' . PHP_EOL; | |
| echo 'console.' . $type . '(val' . preg_replace('~[^A-Z|0-9]~i', "_", $name) . ');' . PHP_EOL; | |
| } else { | |
| echo 'console.' . $type . '("' . self::escape_quotes($var) . '");' . PHP_EOL; | |
| } | |
| } | |
| echo '</script>' . PHP_EOL; | |
| } | |
| /** | |
| * Escape the quotes in a string | |
| * @param type $str | |
| */ | |
| private static function escape_quotes(&$str) { | |
| $str = str_replace('"', '\\"', $str); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require 'JSDebug.class.php'; | |
| $console = new JSDebug(); | |
| $console->error('error output', array('this', 'is', 'a', 'test')); | |
| $console->info('info output', (object)array('what the', 'heck?')); | |
| $console->log('log output', (object)array('what the', 'heck?')); | |
| $console->warn('warning output', array('what the', 'heck?')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment