Skip to content

Commit

Permalink
Extracting methods from nasty Debugger::output().
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Aug 23, 2011
1 parent ea3b38e commit 35fc835
Showing 1 changed file with 47 additions and 21 deletions.
68 changes: 47 additions & 21 deletions lib/Cake/Utility/Debugger.php
Expand Up @@ -554,14 +554,24 @@ protected static function _object($var) {
}

/**
* Switches output format, updates format strings.
* Can be used to switch the active output format:
* Get/Set the output format for Debugger error rendering.
*
* `Debugger::output('js');`
*
* Can be used to add new output types to debugger.
* @param string $format The format you want errors to be output as.
* Leave null to get the current format.
* @return void
*/
public static function outputAs($format = null) {
$self = Debugger::getInstance();
if ($format === null) {
return $self->_outputFormat;
}
$self->_outputFormat = $format;
}

/**
* Add an output format or update a format in Debugger.
*
* `Debugger::output('custom', $data);`
* `Debugger::addFormat('custom', $data);`
*
* Where $data is an array of strings that use String::insert() variable
* replacement. The template vars should be in a `{:id}` style.
Expand All @@ -584,37 +594,53 @@ protected static function _object($var) {
* @param string $format Format to use, including 'js' for JavaScript-enhanced HTML, 'html' for
* straight HTML output, or 'txt' for unformatted text.
* @param array $strings Template strings to be used for the output format.
* @return The resulting format string set.
*/
public static function addFormat($format, array $strings) {
$self = Debugger::getInstance();
if (isset($self->_templates[$format])) {
if (isset($strings['links'])) {
$self->_templates[$format]['links'] = array_merge(
$self->_templates[$format]['links'],
$strings['links']
);
unset($strings['links']);
}
$self->_templates[$format] = array_merge($self->_templates[$format], $strings);
} else {
$self->_templates[$format] = $strings;
}
return $self->_templates[$format];
}

/**
* Switches output format, updates format strings.
* Can be used to switch the active output format:
*
* @param string $format Format to use, including 'js' for JavaScript-enhanced HTML, 'html' for
* straight HTML output, or 'txt' for unformatted text.
* @param array $strings Template strings to be used for the output format.
* @deprecated Use Debugger::outputFormat() and Debugger::addFormat(). Will be removed
* in 3.0
*/
public function output($format = null, $strings = array()) {
$_this = Debugger::getInstance();
$data = null;

if (is_null($format)) {
return $_this->_outputFormat;
return Debugger::outputAs();
}

if (!empty($strings)) {
if (isset($_this->_templates[$format])) {
if (isset($strings['links'])) {
$_this->_templates[$format]['links'] = array_merge(
$_this->_templates[$format]['links'],
$strings['links']
);
unset($strings['links']);
}
$_this->_templates[$format] = array_merge($_this->_templates[$format], $strings);
} else {
$_this->_templates[$format] = $strings;
}
return $_this->_templates[$format];
return Debugger::addFormat($format, $strings);
}

if ($format === true && !empty($_this->_data)) {
$data = $_this->_data;
$_this->_data = array();
$format = false;
}
$_this->_outputFormat = $format;
Debugger::outputAs($format);

return $data;
}
Expand Down

0 comments on commit 35fc835

Please sign in to comment.