upgrade debugbar to avoid php 8.1 warnings

This commit is contained in:
Frédéric FRANCE
2023-03-14 23:58:03 +01:00
parent 68a2131ae6
commit 7df3b849d2
44 changed files with 3350 additions and 254 deletions

View File

@@ -82,6 +82,8 @@ class JavascriptRenderer
protected $openHandlerUrl;
protected $cspNonce;
/**
* @param \DebugBar\DebugBar $debugBar
* @param string $baseUrl
@@ -183,6 +185,9 @@ class JavascriptRenderer
if (array_key_exists('open_handler_url', $options)) {
$this->setOpenHandlerUrl($options['open_handler_url']);
}
if (array_key_exists('csp_nonce', $options)) {
$this->setCspNonce($options['csp_nonce']);
}
}
/**
@@ -606,6 +611,28 @@ class JavascriptRenderer
return $this->openHandlerUrl;
}
/**
* Sets the CSP Nonce (or remove it by setting to null)
*
* @param string|null $nonce
* @return $this
*/
public function setCspNonce($nonce = null)
{
$this->cspNonce = $nonce;
return $this;
}
/**
* Get the CSP Nonce
*
* @return string|null
*/
public function getCspNonce()
{
return $this->cspNonce;
}
/**
* Add assets stored in files to render in the head
*
@@ -692,8 +719,8 @@ class JavascriptRenderer
}
foreach ($additionalAssets as $assets) {
$basePath = isset($assets['base_path']) ? $assets['base_path'] : null;
$baseUrl = isset($assets['base_url']) ? $assets['base_url'] : null;
$basePath = isset($assets['base_path']) ? $assets['base_path'] : '';
$baseUrl = isset($assets['base_url']) ? $assets['base_url'] : '';
$root = $this->getRelativeRoot($relativeTo,
$this->makeUriRelativeTo($basePath, $this->basePath),
$this->makeUriRelativeTo($baseUrl, $this->baseUrl));
@@ -719,7 +746,7 @@ class JavascriptRenderer
$cssFiles = array_unique($cssFiles);
$jsFiles = array_unique($jsFiles);
return $this->filterAssetArray(array($cssFiles, $jsFiles, $inlineCss, $inlineJs, $inlineHead), $type);
return $this->filterAssetArray(array($cssFiles, $jsFiles, $inlineCss, $inlineJs, $inlineHead), $type ?? '');
}
/**
@@ -762,7 +789,9 @@ class JavascriptRenderer
return $uris;
}
if ($uri && (substr($uri, 0, 1) === '/' || preg_match('/^([a-zA-Z]+:\/\/|[a-zA-Z]:\/|[a-zA-Z]:\\\)/', $uri))) {
$uri = $uri ?? '';
if (substr($uri, 0, 1) === '/' || preg_match('/^([a-zA-Z]+:\/\/|[a-zA-Z]:\/|[a-zA-Z]:\\\)/', $uri)) {
return $uri;
}
return rtrim($root, '/') . "/$uri";
@@ -775,10 +804,10 @@ class JavascriptRenderer
* @param string $type 'css', 'js', 'inline_css', 'inline_js', 'inline_head', or null for all
* @return array
*/
protected function filterAssetArray($array, $type = null)
protected function filterAssetArray($array, $type = '')
{
$types = array('css', 'js', 'inline_css', 'inline_js', 'inline_head');
$typeIndex = is_null($type) ? false : array_search(strtolower($type), $types);
$typeIndex = array_search(strtolower($type ?? ''), $types);
return $typeIndex !== false ? $array[$typeIndex] : $array;
}
@@ -905,6 +934,8 @@ class JavascriptRenderer
list($cssFiles, $jsFiles, $inlineCss, $inlineJs, $inlineHead) = $this->getAssets(null, self::RELATIVE_URL);
$html = '';
$nonce = $this->getNonceAttribute();
foreach ($cssFiles as $file) {
$html .= sprintf('<link rel="stylesheet" type="text/css" href="%s">' . "\n", $file);
}
@@ -918,7 +949,7 @@ class JavascriptRenderer
}
foreach ($inlineJs as $content) {
$html .= sprintf('<script type="text/javascript">%s</script>' . "\n", $content);
$html .= sprintf('<script type="text/javascript"%s>%s</script>' . "\n", $nonce, $content);
}
foreach ($inlineHead as $content) {
@@ -926,7 +957,7 @@ class JavascriptRenderer
}
if ($this->enableJqueryNoConflict && !$this->useRequireJs) {
$html .= '<script type="text/javascript">jQuery.noConflict(true);</script>' . "\n";
$html .= '<script type="text/javascript"' . $nonce . '>jQuery.noConflict(true);</script>' . "\n";
}
return $html;
@@ -1013,10 +1044,12 @@ class JavascriptRenderer
$suffix = !$initialize ? '(ajax)' : null;
$js .= $this->getAddDatasetCode($this->debugBar->getCurrentRequestId(), $this->debugBar->getData(), $suffix);
$nonce = $this->getNonceAttribute();
if ($this->useRequireJs){
return "<script type=\"text/javascript\">\nrequire(['debugbar'], function(PhpDebugBar){ $js });\n</script>\n";
return "<script type=\"text/javascript\"{$nonce}>\nrequire(['debugbar'], function(PhpDebugBar){ $js });\n</script>\n";
} else {
return "<script type=\"text/javascript\">\n$js\n</script>\n";
return "<script type=\"text/javascript\"{$nonce}>\n$js\n</script>\n";
}
}
@@ -1149,4 +1182,17 @@ class JavascriptRenderer
);
return $js;
}
/**
* If a nonce it set, create the correct attribute
* @return string
*/
protected function getNonceAttribute()
{
if ($nonce = $this->getCspNonce()) {
return ' nonce="' . $nonce .'"';
}
return '';
}
}