Skip to content

Commit

Permalink
Allow registering callback functions for the debug console
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Babker committed Aug 20, 2016
1 parent 36c37ea commit f942e74
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions plugins/system/debug/debug.php
Expand Up @@ -82,6 +82,14 @@ class PlgSystemDebug extends JPlugin
*/
protected $app;

/**
* Container for callback functions to be triggered when rendering the console.
*
* @var callable[]
* @since __DEPLOY_VERSION__
*/
private static $displayCallbacks = array();

/**
* Constructor.
*
Expand Down Expand Up @@ -306,11 +314,56 @@ public function onAfterRespond()
}
}

foreach (self::$displayCallbacks as $name => $callable)
{
$html[] = $this->displayCallback($name, $callable);
}

$html[] = '</div>';

echo str_replace('</body>', implode('', $html) . '</body>', $contents);
}

/**
* Add a display callback to be rendered with the debug console.
*
* @param string $name The name of the callable, this is used to generate the section title.
* @param callable $callable The callback function to be added.
*
* @return boolean
*
* @since __DEPLOY_VERSION__
* @throws InvalidArgumentException
*/
public static function addDisplayCallback($name, $callable)
{
// TODO - When PHP 5.4 is the minimum the parameter should be typehinted "callable" and this check removed
if (!is_callable($callable))
{
throw new InvalidArgumentException('A valid callback function must be given.');
}

self::$displayCallbacks[$name] = $callable;

return true;
}

/**
* Remove a registered display callback
*
* @param string $name The name of the callable.
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public static function removeDisplayCallback($name)
{
unset(self::$displayCallbacks[$name]);

return true;
}

/**
* Method to check if the current user is allowed to see the debug information or not.
*
Expand Down Expand Up @@ -393,6 +446,38 @@ protected function display($item, array $errors = array())
return implode('', $html);
}

/**
* Display method for callback functions.
*
* @param string $name The name of the callable.
* @param callable $callable The callable function.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
protected function displayCallback($name, $callable)
{
$title = JText::_('PLG_DEBUG_' . strtoupper($name));

$html = array();

$js = "toggleContainer('dbg_container_" . $name . "');";

$class = 'dbg-header';

$html[] = '<div class="' . $class . '" onclick="' . $js . '"><a href="javascript:void(0);"><h3>' . $title . '</h3></a></div>';

// @todo set with js.. ?
$style = ' style="display: none;"';

$html[] = '<div ' . $style . ' class="dbg-container" id="dbg_container_' . $name . '">';
$html[] = call_user_func($callable);
$html[] = '</div>';

return implode('', $html);
}

/**
* Display session information.
*
Expand Down

0 comments on commit f942e74

Please sign in to comment.