* - '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
// 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']);
}
}
}
+
+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.
+ *
+ * @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.");
+ }
+ }
+
+}
$this->resolver->get('call://totallyNonexistentService/ping');
}
+ /**
+ * Test callback which returns a global variable.
+ */
+ public function testGlobalGetter() {
+ $_GET['resolverTest'] = 123;
+ $cb = $this->resolver->get('global://_GET/resolverTest?getter');
+ $_GET['resolverTest'] = 456;
+ $this->assertEquals(456, call_user_func($cb, 'side-effect-free'));
+ $this->assertEquals(456, $_GET['resolverTest']);
+ unset($_GET['resolverTest']);
+ }
+
+ public function testGlobalSetter() {
+ $GLOBALS['resolverTest2'] = 78;
+ $cb = $this->resolver->get('global://resolverTest2?setter');
+ call_user_func($cb, 90);
+ $this->assertEquals(90, $GLOBALS['resolverTest2']);
+ }
+
/**
* Test object-lookup in the container.
*/