🔊 Adds a debug log for invalid placeholders (#282)

This adds a debug log when a placeholder cannot be replaced.
Before the placeholder was just not replaced.
Now we can enable a debug log on the LogChannels::NOTIFICATION channel.
This commit is contained in:
Pierre Goiffon
2022-08-17 14:09:59 +02:00
committed by GitHub
parent ae021064a4
commit 33c2168af2

View File

@@ -7344,14 +7344,11 @@ abstract class MetaModel
$aSearches = array();
$aReplacements = array();
foreach ($aParams as $sSearch => $replace)
{
foreach ($aParams as $sSearch => $replace) {
// Some environment parameters are objects, we just need scalars
if (is_object($replace))
{
if (is_object($replace)) {
$iPos = strpos($sSearch, '->object()');
if ($iPos !== false)
{
if ($iPos !== false) {
// Expand the parameters for the object
$sName = substr($sSearch, 0, $iPos);
// Note: Capturing
@@ -7359,63 +7356,67 @@ abstract class MetaModel
// 2 - The arrow
// 3 - The attribute code
$aRegExps = array(
'/(\\$)'.$sName.'-(>|>)([^\\$]+)\\$/', // Support both syntaxes: $this->xxx$ or $this->xxx$ for HTML compatibility
'/(%24)'.$sName.'-(>|>)([^%24]+)%24/', // Support for urlencoded in HTML attributes (%20this->xxx%20)
);
foreach($aRegExps as $sRegExp)
{
if(preg_match_all($sRegExp, $sInput, $aMatches))
{
foreach($aMatches[3] as $idx => $sPlaceholderAttCode)
{
try
{
$sReplacement = $replace->GetForTemplate($sPlaceholderAttCode);
if($sReplacement !== null)
{
$aReplacements[] = $sReplacement;
$aSearches[] = $aMatches[1][$idx] . $sName . '-' . $aMatches[2][$idx] . $sPlaceholderAttCode . $aMatches[1][$idx];
}
}
catch(Exception $e)
{
// No replacement will occur
}
}
}
}
}
else
{
'/(\\$)'.$sName.'-(>|>)([^\\$]+)\\$/', // Support both syntaxes: $this->xxx$ or $this->xxx$ for HTML compatibility
'/(%24)'.$sName.'-(>|>)([^%24]+)%24/', // Support for urlencoded in HTML attributes (%20this->xxx%20)
);
foreach ($aRegExps as $sRegExp) {
if (preg_match_all($sRegExp, $sInput, $aMatches)) {
foreach ($aMatches[3] as $idx => $sPlaceholderAttCode) {
try {
$sReplacement = $replace->GetForTemplate($sPlaceholderAttCode);
if ($sReplacement !== null) {
$aReplacements[] = $sReplacement;
$aSearches[] = $aMatches[1][$idx].$sName.'-'.$aMatches[2][$idx].$sPlaceholderAttCode.$aMatches[1][$idx];
}
}
catch (Exception $e) {
$aContext = [
'placeholder' => $sPlaceholderAttCode,
'replace class' => get_class($replace),
];
if ($replace instanceof DBObject) {
$aContext['replace id'] = $replace->GetKey();
}
IssueLog::Debug(
'Invalid placeholder in notification, no replacement will occur!',
LogChannels::NOTIFICATION,
$aContext
);
}
}
}
}
} else {
continue; // Ignore this non-scalar value
}
}
else
{
} else {
$aRegExps = array(
'/(\$)'.$sSearch.'\$/', // Support for regular placeholders (eg. $APP_URL$)
'/(%24)'.$sSearch.'%24/', // Support for urlencoded in HTML attributes (eg. %24APP_URL%24)
);
foreach($aRegExps as $sRegExp)
{
if(preg_match_all($sRegExp, $sInput, $aMatches))
{
foreach($aMatches[1] as $idx => $sDelimiter)
{
try
{
$aReplacements[] = (string) $replace;
$aSearches[] = $aMatches[1][$idx] . $sSearch . $aMatches[1][$idx];
foreach ($aRegExps as $sRegExp) {
if (preg_match_all($sRegExp, $sInput, $aMatches)) {
foreach ($aMatches[1] as $idx => $sDelimiter) {
try {
$aReplacements[] = (string)$replace;
$aSearches[] = $aMatches[1][$idx].$sSearch.$aMatches[1][$idx];
}
catch(Exception $e)
{
// No replacement will occur
catch (Exception $e) {
IssueLog::Debug(
'Invalid placeholder in notification, no replacement will occur !',
LogChannels::NOTIFICATION,
[
'placeholder' => $sPlaceholderAttCode,
'replace' => $replace,
]
);
}
}
}
}
}
}
return str_replace($aSearches, $aReplacements, $sInput);
}