Skip to content

Commit 33917f8

Browse files
authored
Make modNamespace's translatePath method null-safe (#16763)
### What does it do? Replaces NULL `$path` with empty string. ### Why is it needed? The `str_replace` fn no longer quietly allows a null value to be passed to its subject param. Deprecation notices show in php logs when null is passed in php 8.1+. ### How to test 1. Change a namespace's core or assets path to NULL via sql query. 2. Observe the php logs after clearing modx's cache (before loading this PR); you should see the deprecation notice(s). 3. Load the PR and clear cache again. There should be no deprecation notices (of the type seen in step 2). ### Future Consideration This could also be avoided by altering the _path_ and _assets_path_ columns in the namespaces table to be not null with default of empty string. ### Related issue(s)/PR(s) No related issue.
1 parent 97d054f commit 33917f8

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

core/src/Revolution/modNamespace.php

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
* Represents a Component in the MODX framework. Isolates controllers, lexicons and other logic into the virtual
1212
* containment space defined by the path of the namespace.
1313
*
14-
* @property string $name The key of the namespace
15-
* @property string $path The absolute path of the namespace. May use {core_path}, {base_path} or {assets_path} as
14+
* @property string $name The key of the namespace
15+
* @property string $path The absolute path of the namespace. May use {core_path}, {base_path} or {assets_path} as
1616
* placeholders for the path.
17-
* @property string $assets_path
17+
* @property string $assets_path
1818
*
19-
* @property modLexiconEntry[] $LexiconEntries
20-
* @property modSystemSetting[] $SystemSettings
21-
* @property modContextSetting[] $ContextSettings
22-
* @property modUserSetting[] $UserSettings
19+
* @property modLexiconEntry[] $LexiconEntries
20+
* @property modSystemSetting[] $SystemSettings
21+
* @property modContextSetting[] $ContextSettings
22+
* @property modUserSetting[] $UserSettings
2323
* @property modExtensionPackage[] $ExtensionPackages
24-
* @property modAccessNamespace[] $Acls
24+
* @property modAccessNamespace[] $Acls
2525
*
2626
* @package MODX\Revolution
2727
*/
@@ -55,10 +55,16 @@ public static function loadCache(modX $modx)
5555
$cacheKey = 'namespaces';
5656
$cache = $modx->cacheManager->get($cacheKey, [
5757
xPDO::OPT_CACHE_KEY => $modx->getOption('cache_namespaces_key', null, 'namespaces'),
58-
xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_namespaces_handler', null,
59-
$modx->getOption(xPDO::OPT_CACHE_HANDLER)),
60-
xPDO::OPT_CACHE_FORMAT => (integer)$modx->getOption('cache_namespaces_format', null,
61-
$modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP)),
58+
xPDO::OPT_CACHE_HANDLER => $modx->getOption(
59+
'cache_namespaces_handler',
60+
null,
61+
$modx->getOption(xPDO::OPT_CACHE_HANDLER)
62+
),
63+
xPDO::OPT_CACHE_FORMAT => (int)$modx->getOption(
64+
'cache_namespaces_format',
65+
null,
66+
$modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP)
67+
),
6268
]);
6369
if (empty($cache)) {
6470
$cache = $modx->cacheManager->generateNamespacesCache($cacheKey);
@@ -72,10 +78,16 @@ public static function clearCache(modX $modx)
7278
$cacheKey = 'namespaces';
7379
$cleared = $modx->cacheManager->delete($cacheKey, [
7480
xPDO::OPT_CACHE_KEY => $modx->getOption('cache_namespaces_key', null, 'namespaces'),
75-
xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_namespaces_handler', null,
76-
$modx->getOption(xPDO::OPT_CACHE_HANDLER)),
77-
xPDO::OPT_CACHE_FORMAT => (integer)$modx->getOption('cache_namespaces_format', null,
78-
$modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP)),
81+
xPDO::OPT_CACHE_HANDLER => $modx->getOption(
82+
'cache_namespaces_handler',
83+
null,
84+
$modx->getOption(xPDO::OPT_CACHE_HANDLER)
85+
),
86+
xPDO::OPT_CACHE_FORMAT => (int)$modx->getOption(
87+
'cache_namespaces_format',
88+
null,
89+
$modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP)
90+
),
7991
]);
8092

8193
return $cleared;
@@ -97,6 +109,9 @@ public function getAssetsPath()
97109

98110
public static function translatePath(xPDO &$xpdo, $path)
99111
{
112+
if ($path === null) {
113+
return '';
114+
}
100115
return str_replace([
101116
'{core_path}',
102117
'{base_path}',

0 commit comments

Comments
 (0)