Skip to content
Snippets Groups Projects
Unverified Commit 4ade7791 authored by totten's avatar totten Committed by GitHub
Browse files

Merge pull request #23947 from totten/master-php81-global

#3181 - Resolver - Compatibility fix for GLOBALS in PHP 8.1
parents 65d81bf2 6b4be82d
No related branches found
No related tags found
No related merge requests found
......@@ -1077,6 +1077,9 @@ class CRM_Utils_Array {
* @return bool
*/
public static function pathIsset($values, $path) {
if ($path === []) {
return ($values !== NULL);
}
foreach ($path as $key) {
if (!is_array($values) || !isset($values[$key])) {
return FALSE;
......@@ -1100,6 +1103,11 @@ class CRM_Utils_Array {
* TRUE if anything has been removed. FALSE if no changes were required.
*/
public static function pathUnset(&$values, $path, $cleanup = FALSE) {
if (count($path) === 0) {
$values = NULL;
return TRUE;
}
if (count($path) === 1) {
if (isset($values[$path[0]])) {
unset($values[$path[0]]);
......@@ -1131,6 +1139,10 @@ class CRM_Utils_Array {
* Ex: 456.
*/
public static function pathSet(&$values, $pathParts, $value) {
if ($pathParts === []) {
$values = $value;
return;
}
$r = &$values;
$last = array_pop($pathParts);
foreach ($pathParts as $part) {
......
......@@ -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 {
......
......@@ -207,6 +207,30 @@ class CRM_Utils_ArrayTest extends CiviUnitTestCase {
}
public function testGetSet_EmptyPath() {
$emptyPath = [];
$x = 'hello';
$this->assertEquals(TRUE, CRM_Utils_Array::pathIsset($x, $emptyPath));
$this->assertEquals('hello', CRM_Utils_Array::pathGet($x, $emptyPath));
$this->assertEquals('hello', $x);
CRM_Utils_Array::pathSet($x, $emptyPath, 'bon jour');
$this->assertEquals(TRUE, CRM_Utils_Array::pathIsset($x, $emptyPath));
$this->assertEquals('bon jour', CRM_Utils_Array::pathGet($x, $emptyPath));
$this->assertEquals('bon jour', $x);
CRM_Utils_Array::pathUnset($x, $emptyPath);
$this->assertEquals(FALSE, CRM_Utils_Array::pathIsset($x, $emptyPath));
$this->assertEquals(NULL, CRM_Utils_Array::pathGet($x, $emptyPath));
$this->assertEquals(NULL, $x);
CRM_Utils_Array::pathSet($x, $emptyPath, 'buenos dias');
$this->assertEquals(TRUE, CRM_Utils_Array::pathIsset($x, $emptyPath));
$this->assertEquals('buenos dias', CRM_Utils_Array::pathGet($x, $emptyPath));
$this->assertEquals('buenos dias', $x);
}
public function getSortExamples() {
$red = ['label' => 'Red', 'id' => 1, 'weight' => '90'];
$orange = ['label' => 'Orange', 'id' => 2, 'weight' => '70'];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment