Skip to content

Instantly share code, notes, and snippets.

@nfreear
Last active September 30, 2015 03:17
Show Gist options
  • Save nfreear/1712707 to your computer and use it in GitHub Desktop.
Save nfreear/1712707 to your computer and use it in GitHub Desktop.
If a debug parameter variable is set, output PHP expressions in HTTP headers / Drupal.
<?php
<?php
ini_set( 'display_errors', 1);
error_reporting( E_ALL );
//header( 'Content-Type: text/plain');
function my_test() {
_ispot_theme_debug(array( 'some data' ));
}
my_test();
echo 'Hello world! ';
flush();
my_test();
// ===================================================================
/** DEBUG: _ispot_theme_debug( $arg1, ... $argN )
*/
function _ispot_theme_debug( $arg_1, /* $... */ $arg_N = null ) {
static $count;
$debug = TRUE;
if ($debug) {
$count++;
$caller = _ispot_theme_caller();
$data = 'X-'. __FUNCTION__ .'-'. sprintf( '%02d', $count ) .': '.
$caller->fn .'; '. json_encode( func_num_args() > 1 ? func_get_args() : $arg_1 );
if (headers_sent()): /* ?>
<script>window.console && console.log('<?php echo $data ?>')</script>
<?php else: */
header( $data );
endif;
}
}
function _ispot_theme_caller( $level = 2 ) {
$stack = debug_backtrace();
$result = $stack[ $level ];
$result[ 'original_file'] = $result[ 'file' ];
$result[ 'file' ] = basename($result[ 'file' ]);
$result[ 'fn' ] = $result[ 'function' ];
return (object) $result;
}
// ===================================================================
// themes/custom/ispot_2013/template.php
/** DEBUG: _ispot_theme_debug( $arg1, ... $argN )
*/
function _ispot_theme_debug( $arg_1, /* $... */ $arg_N = null ) {
static $count;
$count++;
@header( 'X-'. __FUNCTION__ .'-'. sprintf( '%02d', $count ) .': '.
json_encode( func_num_args() > 1 ? func_get_args() : $arg_1 ));
}
// ===================================================================
/**If a debug parameter or variable is set, output PHP expressions in HTTP headers / Drupal.
*
* Inspired by OUVLE/Moodle ouauth, and FirePHP.
*
* @copyright 2012-01-31 N.D.Freear.
* @param mixed $exp... One or more PHP strings and expressions.
*/
function _MY_debug($exp) {
static $where, $count = 0;
if (isset($_GET['debug']) || variable_get('debug', NULL)) {
# $where could be based on __FUNCTION__ or debug_stacktrace().
if(!$where) $where = str_replace(array('_', '.'), '-', basename(__FILE__));
header("X-D-$where-".sprintf('%02d', $count).': '.json_encode($exp));
foreach (func_get_args() as $c => $arg) {
if($c > 0) _MY_debug($arg); #Recurse.
}
$count++;
}
}
/*Drupal*/ if(!function_exists('variable_get')){function variable_get(){return 1;}}
_MY_debug('Hello world!', array('key'=>'value'), 12345);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment