Skip to content

Commit

Permalink
Allow easier extend of BrowserConsoleHandler.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Warxcell committed Sep 17, 2021
1 parent 437e7a1 commit 3d55ecf
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/Monolog/Handler/BrowserConsoleHandler.php
Expand Up @@ -11,10 +11,14 @@

namespace Monolog\Handler;

use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Utils;

use function count;
use function headers_list;
use function stripos;

/**
* Handler sending logs to browser's javascript console with no browser extension required
*
Expand All @@ -29,6 +33,9 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
/** @var FormattedRecord[] */
protected static $records = [];

public const FORMAT_HTML = 'html';
public const FORMAT_JS = 'js';

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -65,7 +72,7 @@ protected function write(array $record): void
public static function send(): void
{
$format = static::getResponseFormat();
if ($format === 'unknown') {
if (null === $format) {
return;
}

Expand Down Expand Up @@ -131,21 +138,27 @@ protected static function getResponseFormat(): string
// Check content type
foreach (headers_list() as $header) {
if (stripos($header, 'content-type:') === 0) {
// This handler only works with HTML and javascript outputs
// 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 static::getResponseFormatFromContentType($header);
}
}

return 'html';
}

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 static::FORMAT_JS;
}
if (stripos($contentType, 'text/html') === false) {
return null;
}

return self::FORMAT_HTML;
}

private static function generateScript(): string
{
$script = [];
Expand Down

0 comments on commit 3d55ecf

Please sign in to comment.