dev/core#3181 - Resolver - Compatibility fix for GLOBALS in PHP 8.1
authorTim Otten <totten@civicrm.org>
Tue, 5 Jul 2022 23:33:17 +0000 (16:33 -0700)
committerTim Otten <totten@civicrm.org>
Tue, 5 Jul 2022 23:41:13 +0000 (16:41 -0700)
Civi/Core/Resolver.php

index a3950c74e9f0f4cf6f9d95a3219f3cd81d6fe5ed..67d4689117d4a1fb60373d3b1eb36bc01670d94f 100644 (file)
@@ -251,7 +251,8 @@ class ResolverApi {
 
 class ResolverGlobalCallback {
   private $mode;
-  private $path;
+  private $basePath;
+  private $subPath;
 
   /**
    * Class constructor.
@@ -259,10 +260,13 @@ class ResolverGlobalCallback {
    * @param string $mode
    *   'getter' or 'setter'.
    * @param string $path
+   *   Ex: 'dbLocale' <=> $GLOBALS['dbLocale']
+   *   Ex: 'civicrm_setting/domain/debug_enabled' <=> $GLOBALS['civicrm_setting']['domain']['debug_enabled']
    */
   public function __construct($mode, $path) {
     $this->mode = $mode;
-    $this->path = $path;
+    $this->subPath = explode('/', $path);
+    $this->basePath = array_shift($this->subPath);
   }
 
   /**
@@ -273,11 +277,12 @@ class ResolverGlobalCallback {
    * @return mixed
    */
   public function __invoke($arg1 = NULL) {
+    // For PHP 8.1+ compatibility, we resolve the first path-item before doing any array operations.
     if ($this->mode === 'getter') {
-      return \CRM_Utils_Array::pathGet($GLOBALS, explode('/', $this->path));
+      return \CRM_Utils_Array::pathGet($GLOBALS[$this->basePath], $this->subPath);
     }
     elseif ($this->mode === 'setter') {
-      \CRM_Utils_Array::pathSet($GLOBALS, explode('/', $this->path), $arg1);
+      \CRM_Utils_Array::pathSet($GLOBALS[$this->basePath], $this->subPath, $arg1);
       return NULL;
     }
     else {