Skip to content

Instantly share code, notes, and snippets.

@calvinjuarez
Created December 5, 2017 01:36
Show Gist options
  • Save calvinjuarez/68e0ca3e1482756b082e5c2d3d07d576 to your computer and use it in GitHub Desktop.
Save calvinjuarez/68e0ca3e1482756b082e5c2d3d07d576 to your computer and use it in GitHub Desktop.
A function to output a PHP variable in a `<pre>` with trace info in a data attribute.
<?php
/**
* @file A function to output a PHP variable in a `<pre>` with a class and trace info.
* @author Calvin Juárez <[email protected]>
* @copyright 2017 Calvin Juárez
* @license MIT {@link https://spdx.org/licenses/MIT.html MIT License on SPDX.org}
*/
/**
* An inline <pre> log function that attempts to provide caller file and function information
*
* @function pre_log
*
* @param {mixed} $subject The variable to export.
* @param {array} $meta=[] (Optional) Any HTML attributes to add to the <pre>.
* @param {array} $affix=[] (Optional) Strings to insert in the <pre> before and after the
* $subject.
* @param {array} $echo=true (Optional) A boolean indicating whether to echo the result. Set to
* `false` to return the markup as a string instead.
* @return {string} If `$echo` is `false`: The result of `var_export()` wrapped in a nice `<pre>`.
*/
function pre_log($subject, $meta=array(), $affix=array('',''), $echo=true) {
$attributes = '';
$classes = '';
// Get origin information.
if (! isset($meta['data-origin'])) { // we only need to do any of this if `$meta['origin']` isn't already set (allows user override)
$origin = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; // get the last 2 backtrace items
if (isset($trace[$i]['file']))
$attributes .= ' data-origin="' . basename($origin['file']) . '@ln' . $origin['line'] . '"';
}
// Process `$meta`.
if (! empty($meta)) { // we only need to do any of this if $meta has been specified and isn't `''` or something
if (is_array($meta)) { // …then we should use attributes
if (isset($meta['class'])) {
$classes .= $meta['class']; // merges $meta['class'] with the $classes string (allows for classes & attributes)
unset($meta['class']); // excludes it from the foreach below
}
foreach ($meta as $attribute => $value)
$attributes .= ' ' . $attribute . '="' . $value . '"';
} elseif (strval($meta)) // …then we should use classes
$classes = strval($meta);
else // $meta is empty or malformed, so we're just gonna ignore it.
$meta = '';
}
// Return a <pre> with a class and with info about where it's coming from.
if (! empty($classes)) $classes = ' ' . trim($classes);
if (! empty($attributes)) $attributes = ' ' . trim($attributes);
$output = '<pre class="php-log' . $classes . '"' . $attributes . '>' . $affix[0] . htmlentities(var_export($subject, true)) . $affix[1] . '</pre>';
// Echo or return, depending on the value of the `$echo` flag.
if ($echo)
echo $output; // echo if we should
else
return $output; // otherwise, give it back as a string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment