mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-06 13:16:39 +02:00
Refactor the browser console handler to keep more methods private, refs #303
This commit is contained in:
@@ -21,8 +21,8 @@ use Monolog\Formatter\LineFormatter;
|
|||||||
*/
|
*/
|
||||||
class BrowserConsoleHandler extends AbstractProcessingHandler
|
class BrowserConsoleHandler extends AbstractProcessingHandler
|
||||||
{
|
{
|
||||||
static protected $initialized = false;
|
protected static $initialized = false;
|
||||||
static protected $records = array();
|
protected static $records = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
@@ -58,13 +58,13 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
|
|||||||
* Convert records to javascript console commands and send it to the browser.
|
* Convert records to javascript console commands and send it to the browser.
|
||||||
* This method is automatically called on PHP shutdown if output is HTML.
|
* This method is automatically called on PHP shutdown if output is HTML.
|
||||||
*/
|
*/
|
||||||
static public function send()
|
public static function send()
|
||||||
{
|
{
|
||||||
// Check content type
|
// Check content type
|
||||||
foreach (headers_list() as $header) {
|
foreach (headers_list() as $header) {
|
||||||
if (strpos(strtolower($header), 'content-type:') === 0) {
|
if (strpos(strtolower($header), 'content-type:') === 0) {
|
||||||
if (strpos(strtolower($header), 'text/html') === false) {
|
if (strpos(strtolower($header), 'text/html') === false) {
|
||||||
// This handler does only work with HTML outputs
|
// This handler only works with HTML outputs
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -72,7 +72,7 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (count(self::$records)) {
|
if (count(self::$records)) {
|
||||||
print '<script>' . self::generateScript() . '</script>';
|
echo '<script>' . self::generateScript() . '</script>';
|
||||||
self::reset();
|
self::reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -80,12 +80,12 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
|
|||||||
/**
|
/**
|
||||||
* Forget all logged records
|
* Forget all logged records
|
||||||
*/
|
*/
|
||||||
static public function reset()
|
public static function reset()
|
||||||
{
|
{
|
||||||
self::$records = array();
|
self::$records = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function generateScript()
|
private static function generateScript()
|
||||||
{
|
{
|
||||||
$script = array();
|
$script = array();
|
||||||
foreach (self::$records as $record) {
|
foreach (self::$records as $record) {
|
||||||
@@ -106,52 +106,48 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
|
|||||||
return "(function(c){if (c && c.groupCollapsed) {\n" . implode("\n", $script) . "\n}})(console);";
|
return "(function(c){if (c && c.groupCollapsed) {\n" . implode("\n", $script) . "\n}})(console);";
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function handleStyles($formatted)
|
private static function handleStyles($formatted)
|
||||||
{
|
{
|
||||||
$args = array(self::quote('font-weight: normal'));
|
$args = array(self::quote('font-weight: normal'));
|
||||||
$format = '%c' . $formatted;
|
$format = '%c' . $formatted;
|
||||||
$self = 'Monolog\Handler\BrowserConsoleHandler';
|
$self = 'Monolog\Handler\BrowserConsoleHandler';
|
||||||
$format = preg_replace_callback('/\[\[(.*?)\]\]\{([^}]*)\}/s', function($m) use(&$args, $self) {
|
preg_match_all('/\[\[(.*?)\]\]\{([^}]*)\}/s', $format, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
|
||||||
$args[] = $self::quote($self::handleCustomStyles($m[2], $m[1]));
|
|
||||||
$args[] = $self::quote('font-weight: normal');
|
foreach (array_reverse($matches) as $match) {
|
||||||
return '%c' . $m[1] . '%c';
|
$args[] = self::quote(self::handleCustomStyles($match[2][0], $match[1][0]));
|
||||||
}, $format);
|
$args[] = '"font-weight: normal"';
|
||||||
|
|
||||||
|
$pos = $match[0][1];
|
||||||
|
$format = substr($format, 0, $pos) . '%c' . $match[1][0] . '%c' . substr($format, $pos + strlen($match[0][0]));
|
||||||
|
}
|
||||||
|
|
||||||
array_unshift($args, self::quote($format));
|
array_unshift($args, self::quote($format));
|
||||||
|
|
||||||
return $args;
|
return $args;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function handleCustomStyles($style, $string)
|
private static function handleCustomStyles($style, $string)
|
||||||
{
|
|
||||||
$self = 'Monolog\Handler\BrowserConsoleHandler';
|
|
||||||
return preg_replace_callback('/macro\s*:(.*?)(?:;|$)/', function($m) use($string, $self) {
|
|
||||||
switch (trim($m[1])) {
|
|
||||||
case 'autolabel':
|
|
||||||
return $self::macroAutolabel($string);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return $m[1];
|
|
||||||
}
|
|
||||||
}, $style);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Format the string as a label with consistent auto assigned background color
|
|
||||||
*/
|
|
||||||
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();
|
||||||
|
|
||||||
if (!isset($labels[$string])) {
|
$self = 'Monolog\Handler\BrowserConsoleHandler';
|
||||||
$labels[$string] = $colors[count($labels) % count($colors)];
|
return preg_replace_callback('/macro\s*:(.*?)(?:;|$)/', function($m) use($string, $self, &$colors, &$labels) {
|
||||||
}
|
if (trim($m[1]) === 'autolabel') {
|
||||||
$color = $labels[$string];
|
// Format the string as a label with consistent auto assigned background color
|
||||||
|
if (!isset($labels[$string])) {
|
||||||
|
$labels[$string] = $colors[count($labels) % count($colors)];
|
||||||
|
}
|
||||||
|
$color = $labels[$string];
|
||||||
|
|
||||||
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";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $m[1];
|
||||||
|
}, $style);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function dump($title, array $dict)
|
private static function dump($title, array $dict)
|
||||||
{
|
{
|
||||||
$script = array();
|
$script = array();
|
||||||
$dict = array_filter($dict);
|
$dict = array_filter($dict);
|
||||||
@@ -169,19 +165,19 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
|
|||||||
return $script;
|
return $script;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function quote($arg)
|
private static function quote($arg)
|
||||||
{
|
{
|
||||||
return '"' . addcslashes($arg, "\"\n") . '"';
|
return '"' . addcslashes($arg, "\"\n") . '"';
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function call()
|
private static 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 public function call_array($method, array $args)
|
private static function call_array($method, array $args)
|
||||||
{
|
{
|
||||||
return 'c.' . $method . '(' . implode(', ', $args) . ');';
|
return 'c.' . $method . '(' . implode(', ', $args) . ');';
|
||||||
}
|
}
|
||||||
|
@@ -24,6 +24,14 @@ class BrowserConsoleHandlerTest extends TestCase
|
|||||||
BrowserConsoleHandler::reset();
|
BrowserConsoleHandler::reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function generateScript()
|
||||||
|
{
|
||||||
|
$reflMethod = new \ReflectionMethod('Monolog\Handler\BrowserConsoleHandler', 'generateScript');
|
||||||
|
$reflMethod->setAccessible(true);
|
||||||
|
|
||||||
|
return $reflMethod->invoke(null);
|
||||||
|
}
|
||||||
|
|
||||||
public function testStyling()
|
public function testStyling()
|
||||||
{
|
{
|
||||||
$handler = new BrowserConsoleHandler();
|
$handler = new BrowserConsoleHandler();
|
||||||
@@ -37,7 +45,7 @@ c.log("%cfoo%cbar%c", "font-weight: normal", "color: red", "font-weight: normal"
|
|||||||
}})(console);
|
}})(console);
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$this->assertEquals($expected, BrowserConsoleHandler::generateScript());
|
$this->assertEquals($expected, $this->generateScript());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEscaping()
|
public function testEscaping()
|
||||||
@@ -53,7 +61,7 @@ c.log("%c[foo] %c\"bar\\n[baz]\"%c", "font-weight: normal", "color: red", "font-
|
|||||||
}})(console);
|
}})(console);
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$this->assertEquals($expected, BrowserConsoleHandler::generateScript());
|
$this->assertEquals($expected, $this->generateScript());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -74,7 +82,7 @@ c.log("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white;
|
|||||||
}})(console);
|
}})(console);
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$this->assertEquals($expected, BrowserConsoleHandler::generateScript());
|
$this->assertEquals($expected, $this->generateScript());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testContext()
|
public function testContext()
|
||||||
@@ -93,7 +101,7 @@ c.groupEnd();
|
|||||||
}})(console);
|
}})(console);
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$this->assertEquals($expected, BrowserConsoleHandler::generateScript());
|
$this->assertEquals($expected, $this->generateScript());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConcurrentHandlers()
|
public function testConcurrentHandlers()
|
||||||
@@ -118,6 +126,6 @@ c.log("%ctest4", "font-weight: normal");
|
|||||||
}})(console);
|
}})(console);
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$this->assertEquals($expected, BrowserConsoleHandler::generateScript());
|
$this->assertEquals($expected, $this->generateScript());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user