SettingsBag - Fix bug upgrading `4.1.0-setupsh.sql.bz2` to 4.7.beta.
[civicrm-core.git] / Civi / Core / Resolver.php
index d35b512f5db5bc183d8d5ce3e9d5324f10f6f673..70654bac29a43cbf64d6a114cdadf4150a074eb5 100644 (file)
@@ -19,6 +19,8 @@ namespace Civi\Core;
  *   - 'api3://EntityName/action?first=@1&second=@2' - Call an API method, mapping the
  *     first & second args to named parameters.
  *     (Performance note: Requires parsing/interpolating arguments).
+ *   - 'global://Variable/Key2/Key3?getter' - A dummy which looks up a global variable.
+ *   - 'global://Variable/Key2/Key3?setter' - A dummy which updates a global variable.
  *   - '0' or '1' - A dummy which returns the constant '0' or '1'.
  *
  * Note: To differentiate classes and functions, there is a hard requirement that
@@ -70,17 +72,21 @@ class Resolver {
       switch ($url['scheme']) {
         case 'obj':
           // Object: Lookup in container.
-          return Container::singleton()->get($url['host']);
+          return \Civi::service($url['host']);
 
         case 'call':
           // Callback: Object/method in container.
-          $obj = Container::singleton()->get($url['host']);
+          $obj = \Civi::service($url['host']);
           return array($obj, ltrim($url['path'], '/'));
 
         case 'api3':
           // Callback: API.
           return new ResolverApi($url);
 
+        case 'global':
+          // Lookup in a global variable.
+          return new ResolverGlobalCallback($url['query'], $url['host'] . (isset($url['path']) ? rtrim($url['path'], '/') : ''));
+
         default:
           throw new \RuntimeException("Unsupported callback scheme: " . $url['scheme']);
       }
@@ -242,3 +248,39 @@ class ResolverApi {
   }
 
 }
+
+class ResolverGlobalCallback {
+  private $mode, $path;
+
+  /**
+   * Class constructor.
+   *
+   * @param string $mode
+   *   'getter' or 'setter'.
+   * @param string $path
+   */
+  public function __construct($mode, $path) {
+    $this->mode = $mode;
+    $this->path = $path;
+  }
+
+  /**
+   * Invoke function.
+   *
+   * @param mixed $arg1
+   *
+   * @return mixed
+   */
+  public function __invoke($arg1 = NULL) {
+    if ($this->mode === 'getter') {
+      return \CRM_Utils_Array::pathGet($GLOBALS, explode('/', $this->path));
+    }
+    elseif ($this->mode === 'setter') {
+      \CRM_Utils_Array::pathSet($GLOBALS, explode('/', $this->path), $arg1);
+    }
+    else {
+      throw new \RuntimeException("Resolver failed: global:// must specify getter or setter mode.");
+    }
+  }
+
+}