Skip to content

Commit

Permalink
Add support for Symfony 7 (#50)
Browse files Browse the repository at this point in the history
This bumps the min PHP version to 7.2 because Symfony 7 requires adding
a return type and this requires support for variance rules to stay
compatible with older Symfony versions, which are available only in PHP
7.2+.
  • Loading branch information
stof authored Dec 14, 2023
1 parent 74ce456 commit 41b585f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 45 deletions.
12 changes: 2 additions & 10 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,11 @@
*/
class Configuration implements ConfigurationInterface
{
/**
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('nelmio_js_logger');

if (method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
// BC layer for symfony/config 4.1 and older
$rootNode = $treeBuilder->root('nelmio_js_logger');
}
$rootNode = $treeBuilder->getRootNode();

$levels = array('DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY');
$levelsCI = array_merge($levels, array_map('strtolower', $levels));
Expand Down
53 changes: 25 additions & 28 deletions TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ public function __construct(UrlGeneratorInterface $router, $stackTracePath = nul
$this->stackTracePath = $stackTracePath;
}

/**
* @return array
*/
public function getFunctions()
public function getFunctions(): array
{
return array(
new TwigFunction('nelmio_js_error_logger', array($this, 'initErrorLogger'), array('is_safe' => array('html', 'js'))),
Expand All @@ -39,14 +36,14 @@ public function initErrorLogger($level = 'error', $includeScriptTag = true)
$addStackTraceJs = <<<JS
var stackTraceJsModule = (function (basicModule) {
var stackTraceJs = {};
stackTraceJs.appended = false;
stackTraceJs.queue = [];
stackTraceJs.isScriptPresent = function isScriptPresent() {
return ((typeof StackTrace !== 'undefined') && (typeof StackTrace.fromError === 'function'));
};
stackTraceJs.sendLogData = function sendLogData(errorMsg, file, line, col, error) {
StackTrace.fromError(error).then(
function(stackframes) {
Expand Down Expand Up @@ -75,14 +72,14 @@ function(stackframes) {
req.setRequestHeader('Content-Type', 'application/json');
req.send(JSON.stringify(
{
stack: stackframes,
msg: errorMsg,
level: '$level',
stack: stackframes,
msg: errorMsg,
level: '$level',
context: {
file: file,
line: line,
column: col,
userAgent: navigator.userAgent,
file: file,
line: line,
column: col,
userAgent: navigator.userAgent,
platform: navigator.platform,
customContext: basicModule.fetchCustomContext()
}
Expand All @@ -96,8 +93,8 @@ function(stackframes) {
basicModule.callSimpleLogger(errorMsg, file, line, col, error);
});
};
stackTraceJs.callStackTraceJs = function callStackTraceJs(errorMsg, file, line, col, error) {
if (stackTraceJs.isScriptPresent()) {
stackTraceJs.sendLogData(errorMsg, file, line, col, error);
Expand All @@ -109,7 +106,7 @@ function(stackframes) {
script.src = '$this->stackTracePath';
document.documentElement.appendChild(script);
stackTraceJs.appended = true;
script.onload = function() {
if (stackTraceJs.isScriptPresent()) {
if (!this.executed) {
Expand All @@ -121,20 +118,20 @@ function(stackframes) {
continue;
}
var entry = queue[i];
stackTraceJs.sendLogData(entry[0], entry[1], entry[2], entry[3], entry[4]);
stackTraceJs.sendLogData(entry[0], entry[1], entry[2], entry[3], entry[4]);
}
}
}
} else {
console.log(script);
this.onerror();
}
};
script.onerror = function() {
console.log("StackTrace loading has failed, call default logger");
basicModule.callSimpleLogger(errorMsg, file, line, col, error)
};
script.onreadystatechange = function() {
var self = this;
if (this.readyState == "complete" || this.readyState == "loaded") {
Expand Down Expand Up @@ -162,15 +159,15 @@ function(stackframes) {
if (oldErrorHandler) {
oldErrorHandler(errorMsg, file, line, col, error);
}
if (typeof stackTraceJsModule !== 'undefined') {
stackTraceJsModule.callStackTraceJs(errorMsg, file, line, col, error);
return;
}
basic.callSimpleLogger(errorMsg, file, line, col, error);
};
basic.callSimpleLogger = function callSimpleLogger(errorMsg, file, line, col, error) {
var e = encodeURIComponent;
Expand All @@ -182,22 +179,22 @@ function(stackframes) {
'&context[browser]=' + e(navigator.userAgent) +
'&context[page]=' + e(document.location.href) + basic.fetchCustomContext();
};
basic.fetchCustomContext = function fetchCustomContext() {
var key,
e = encodeURIComponent,
customContext = window.nelmio_js_logger_custom_context,
customContextStr = '';
if ('object' === typeof customContext) {
for (key in customContext) {
customContextStr += '&context[' + e(key) + ']=' + e(customContext[key]);
}
}
}
return customContextStr;
};
return basic;
}());
JS;
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
}
],
"require": {
"php": ">=7",
"php": ">=7.2.5",
"ext-json": "*",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"symfony/config": "^4.4 || ^5.3 || ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.3 || ^6.0",
"symfony/http-foundation": "^4.4 || ^5.3 || ^6.0",
"symfony/http-kernel": "^4.4 || ^5.3 || ^6.0",
"symfony/routing": "^4.4 || ^5.3 || ^6.0"
"symfony/config": "^4.4 || ^5.3 || ^6.0 || ^7.0",
"symfony/dependency-injection": "^4.4 || ^5.3 || ^6.0 || ^7.0",
"symfony/http-foundation": "^4.4 || ^5.3 || ^6.0 || ^7.0",
"symfony/http-kernel": "^4.4 || ^5.3 || ^6.0 || ^7.0",
"symfony/routing": "^4.4 || ^5.3 || ^6.0 || ^7.0"
},
"require-dev": {
"twig/twig": "^1.40 || ^2.10"
"twig/twig": "^1.40 || ^2.10 || ^3.0"
},
"autoload": {
"psr-4": { "Nelmio\\JsLoggerBundle\\": "" }
Expand Down

0 comments on commit 41b585f

Please sign in to comment.