1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-07 05:36:45 +02:00

Fix PHP 5.3 support for BrowserConsoleHandler

This commit is contained in:
Olivier Poitrey
2014-01-16 19:01:42 -08:00
parent 577b7fe96c
commit f13900b7f1

View File

@@ -107,13 +107,14 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
return "(function(c){if (console && console.groupCollapsed) {\n" . implode("\n", $script) . "\n}})(console);"; return "(function(c){if (console && console.groupCollapsed) {\n" . implode("\n", $script) . "\n}})(console);";
} }
static protected function handleStyles($formatted) static public function handleStyles($formatted)
{ {
$args = array(self::quote('font-weight: normal')); $args = array(self::quote('font-weight: normal'));
$format = '%c' . $formatted; $format = '%c' . $formatted;
$format = preg_replace_callback('/\[(.*?)\]\{(.*?)\}/', function($m) use(&$args) { $self = 'Monolog\Handler\BrowserConsoleHandler';
$args[] = self::quote(self::handleCustomStyles($m[2], $m[1])); $format = preg_replace_callback('/\[(.*?)\]\{(.*?)\}/', function($m) use(&$args, $self) {
$args[] = self::quote('font-weight: normal'); $args[] = $self::quote($self::handleCustomStyles($m[2], $m[1]));
$args[] = $self::quote('font-weight: normal');
return '%c' . $m[1] . '%c'; return '%c' . $m[1] . '%c';
}, $format); }, $format);
@@ -121,12 +122,13 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
return $args; return $args;
} }
static protected function handleCustomStyles($style, $string) static public function handleCustomStyles($style, $string)
{ {
return preg_replace_callback('/macro\s*:(.*?)(?:;|$)/', function($m) use($string) { $self = 'Monolog\Handler\BrowserConsoleHandler';
return preg_replace_callback('/macro\s*:(.*?)(?:;|$)/', function($m) use($string, $self) {
switch (trim($m[1])) { switch (trim($m[1])) {
case 'autolabel': case 'autolabel':
return self::macroAutolabel($string); return $self::macroAutolabel($string);
break; break;
default: default:
return $m[1]; return $m[1];
@@ -137,7 +139,7 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
/** /**
* Format the string as a label with consistent auto assigned background color * Format the string as a label with consistent auto assigned background color
*/ */
static protected function macroAutolabel($string) static public function macroAutolabel($string)
{ {
static $colors = array('blue', 'green', 'red', 'magenta', 'orange', 'black', 'grey'); static $colors = array('blue', 'green', 'red', 'magenta', 'orange', 'black', 'grey');
static $labels = array(); static $labels = array();
@@ -150,7 +152,7 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
return "background-color: $color; color: white; border-radius: 3px; padding: 0 2px 0 2px"; return "background-color: $color; color: white; border-radius: 3px; padding: 0 2px 0 2px";
} }
static protected function dump($title, array $dict) static public function dump($title, array $dict)
{ {
$script = array(); $script = array();
$dict = array_filter($dict); $dict = array_filter($dict);
@@ -168,19 +170,19 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
return $script; return $script;
} }
static protected function quote($arg) static public function quote($arg)
{ {
return '"' . addcslashes($arg, '"') . '"'; return '"' . addcslashes($arg, '"') . '"';
} }
static protected function call() static public function call()
{ {
$args = func_get_args(); $args = func_get_args();
$method = array_shift($args); $method = array_shift($args);
return self::call_array($method, $args); return self::call_array($method, $args);
} }
static protected function call_array($method, array $args) static public function call_array($method, array $args)
{ {
return 'c.' . $method . '(' . implode(', ', $args) . ');'; return 'c.' . $method . '(' . implode(', ', $args) . ');';
} }