public function testFunctionAcceptMeta() { Cloud::define("getMeta", function ($params, $user, $meta) { return $meta['remoteAddress']; }); $result = Cloud::run("getMeta", array("name" => "alice"), null, array("remoteAddress" => "10.0.0.1")); $this->assertEquals("10.0.0.1", $result); }
/** * Dispatch function and render result * * @param string $funcName Function name * @param array $body JSON decoded body params * @param bool $decodeObj */ private function dispatchFunc($funcName, $body, $decodeObj = false) { $params = $body; if ($decodeObj) { $params = LeanClient::decode($body, null); } $meta["remoteAddress"] = $this->env["REMOTE_ADDR"]; try { $result = Cloud::run($funcName, $params, LeanUser::getCurrentUser(), $meta); } catch (FunctionError $err) { $this->renderError($err->getMessage(), $err->getCode()); } if ($decodeObj) { // Encode object to full, type-annotated JSON $out = LeanClient::encode($result, "toFullJSON"); } else { // Encode object to type-less literal JSON $out = LeanClient::encode($result, "toJSON"); } $this->renderJSON(array("result" => $out)); }
/** * Dispatch function and render result * * @param string $funcName Function name * @param array $body JSON decoded body params * @param bool $decodeObj */ private function dispatchFunc($funcName, $body, $decodeObj = false) { // verify hook sign for RTM hooks if (in_array($funcName, array('_messageReceived', '_receiversOffline', '_messageSent', '_conversationStart', '_conversationStarted', '_conversationAdd', '_conversationRemove', '_conversationUpdate'))) { if (!Client::verifyHookSign($funcName, $body["__sign"])) { error_log("Invalid hook sign for message {$funcName}" . " from {$this->env['REMOTE_ADDR']}"); $this->renderError("Unauthorized.", 401, 401); } } $params = $body; if ($decodeObj) { $params = Client::decode($body, null); } $meta["remoteAddress"] = $this->env["REMOTE_ADDR"]; try { $result = Cloud::run($funcName, $params, User::getCurrentUser(), $meta); } catch (FunctionError $err) { $this->renderError($err->getMessage(), $err->getCode()); } if ($decodeObj) { // Encode object to full, type-annotated JSON $out = Client::encode($result, "toFullJSON"); } else { // Encode object to type-less literal JSON $out = Client::encode($result, "toJSON"); } $this->renderJSON(array("result" => $out)); }