Namespace with a path set to null crashes ajax calls
Bug report
Summary
This is very much an edge case.
The code below will fail in PHP >= 8.1 if the path is null. If the 'path' or 'assets_path' of a namespace are null, this throws a deprecation warning (no warning if the value is empty). If it happens in a processor and the deprecation warning is returned, the JS will crash because the warning it not valid JSON. I think this only happens with an empty cache, because then MODX re-creates the namespace cache on every request.
This error also shows up in page content when the cache is empty.
It does not occur if display_errors is off or E_DEPRECATED errors are not caught.
The two fields allow null to be set. Some extras that don't need an assets_path have it set to null. The only ones I know about are mine, but there may be others.
public static function translatePath(xPDO &$xpdo,$path) {
return str_replace(array(
'{core_path}',
'{base_path}',
'{assets_path}',
),array(
$xpdo->getOption('core_path',null,MODX_CORE_PATH),
$xpdo->getOption('base_path',null,MODX_BASE_PATH),
$xpdo->getOption('assets_path',null,MODX_ASSETS_PATH),
),$path);
}
The simplest way to solve it is to add this at the top of the function:
$path = $path===null? '' : $path;
It might also be a good idea to do that same conversion in modNamespace->save(), and if necessary, in the transport package code that saves the namespace (if it doesn't call the namespace's save() method).
Step to reproduce
Set a namespace assets_path value to null, turn on display_errors, with error reporting set to display E_DEPRECATED errors.
You can also see this by setting up test code with the translatePath() code above and calling:
translatePath($modx, null);
Observed behavior
error message
Expected behavior
No error message