1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 04:37:38 +02:00

Allow easier extend of BrowserConsoleHandler.php (#1593)

Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
This commit is contained in:
Yup
2022-03-13 22:19:51 +02:00
committed by GitHub
parent b39a394c05
commit 1c8f39a94d

View File

@@ -11,10 +11,17 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\FormatterInterface; use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Utils; use Monolog\Utils;
use function count;
use function headers_list;
use function stripos;
use function trigger_error;
use const E_USER_DEPRECATED;
/** /**
* Handler sending logs to browser's javascript console with no browser extension required * Handler sending logs to browser's javascript console with no browser extension required
* *
@@ -29,6 +36,10 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
/** @var FormattedRecord[] */ /** @var FormattedRecord[] */
protected static $records = []; protected static $records = [];
protected const FORMAT_HTML = 'html';
protected const FORMAT_JS = 'js';
protected const FORMAT_UNKNOWN = 'unknown';
/** /**
* {@inheritDoc} * {@inheritDoc}
* *
@@ -65,14 +76,14 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
public static function send(): void public static function send(): void
{ {
$format = static::getResponseFormat(); $format = static::getResponseFormat();
if ($format === 'unknown') { if ($format === self::FORMAT_UNKNOWN) {
return; return;
} }
if (count(static::$records)) { if (count(static::$records)) {
if ($format === 'html') { if ($format === self::FORMAT_HTML) {
static::writeOutput('<script>' . static::generateScript() . '</script>'); static::writeOutput('<script>' . static::generateScript() . '</script>');
} elseif ($format === 'js') { } elseif ($format === self::FORMAT_JS) {
static::writeOutput(static::generateScript()); static::writeOutput(static::generateScript());
} }
static::resetStatic(); static::resetStatic();
@@ -125,25 +136,37 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
* If Content-Type is anything else -> unknown * If Content-Type is anything else -> unknown
* *
* @return string One of 'js', 'html' or 'unknown' * @return string One of 'js', 'html' or 'unknown'
* @phpstan-return self::FORMAT_*
*/ */
protected static function getResponseFormat(): string protected static function getResponseFormat(): string
{ {
// Check content type // Check content type
foreach (headers_list() as $header) { foreach (headers_list() as $header) {
if (stripos($header, 'content-type:') === 0) { if (stripos($header, 'content-type:') === 0) {
// This handler only works with HTML and javascript outputs return static::getResponseFormatFromContentType($header);
// text/javascript is obsolete in favour of application/javascript, but still used
if (stripos($header, 'application/javascript') !== false || stripos($header, 'text/javascript') !== false) {
return 'js';
}
if (stripos($header, 'text/html') === false) {
return 'unknown';
}
break;
} }
} }
return 'html'; return self::FORMAT_HTML;
}
/**
* @return string One of 'js', 'html' or 'unknown'
* @phpstan-return self::FORMAT_*
*/
protected static function getResponseFormatFromContentType(string $contentType): string
{
// This handler only works with HTML and javascript outputs
// text/javascript is obsolete in favour of application/javascript, but still used
if (stripos($contentType, 'application/javascript') !== false || stripos($contentType, 'text/javascript') !== false) {
return self::FORMAT_JS;
}
if (stripos($contentType, 'text/html') !== false) {
return self::FORMAT_HTML;
}
return self::FORMAT_UNKNOWN;
} }
private static function generateScript(): string private static function generateScript(): string