コード例 #1
0
ファイル: CloudTest.php プロジェクト: bruce-zzz/php-sdk
 public function testGetKeys()
 {
     $name = uniqid();
     Cloud::define($name, function ($params, $user) {
         return "hello";
     });
     $this->assertContains($name, Cloud::getKeys());
 }
コード例 #2
0
ファイル: LeanEngine.php プロジェクト: bruce-zzz/php-sdk
 /**
  * Dispatch request
  *
  * Following routes are processed and returned by LeanEngine:
  *
  * ```
  * OPTIONS {1,1.1}/{functions,call}.*
  * *       __engine/1/ping
  * *      {1,1.1}/{functions,call}/_ops/metadatas
  * *      {1,1.1}/{functions,call}/onVerified/{sms,email}
  * *      {1,1.1}/{functions,call}/BigQuery/onComplete
  * *      {1,1.1}/{functions,call}/{className}/{hookName}
  * *      {1,1.1}/{functions,call}/{funcName}
  * ```
  *
  * others may be added in future.
  *
  * @param string $method Request method
  * @param string $url    Request url
  */
 protected function dispatch($method, $url)
 {
     if (static::$useHttpsRedirect) {
         $this->httpsRedirect();
     }
     $path = parse_url($url, PHP_URL_PATH);
     $path = rtrim($path, "/");
     if (strpos($path, "/__engine/1/ping") === 0) {
         $this->renderJSON(array("runtime" => "php-" . phpversion(), "version" => LeanClient::VERSION));
     }
     $this->parseHeaders();
     $pathParts = array();
     // matched path components
     if (preg_match("/^\\/(1|1\\.1)\\/(functions|call)(.*)/", $path, $pathParts) === 1) {
         $pathParts["version"] = $pathParts[1];
         // 1 or 1.1
         $pathParts["endpoint"] = $pathParts[2];
         // functions or call
         $pathParts["extra"] = $pathParts[3];
         // extra part after endpoint
         $origin = $this->env["ORIGIN"];
         $this->withHeader("Access-Control-Allow-Origin", $origin ? $origin : "*");
         if ($method == "OPTIONS") {
             $this->withHeader("Access-Control-Max-Age", 86400)->withHeader("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS")->withHeader("Access-Control-Allow-Headers", implode(", ", self::$allowedHeaders))->withHeader("Content-Length", 0)->renderJSON();
         }
         $body = $this->getBody();
         if (preg_match("/text\\/plain/", $this->env["CONTENT_TYPE"])) {
             // To work around with CORS restriction, some requests are
             // submit as text/palin body, where headers are attached
             // in the body.
             $json = $this->parsePlainBody($body);
         } else {
             $json = json_decode($body, true);
         }
         $this->authRequest();
         $this->processSession();
         if (strpos($pathParts["extra"], "/_ops/metadatas") === 0) {
             if ($this->env["useMaster"]) {
                 $this->renderJSON(Cloud::getKeys());
             } else {
                 $this->renderError("Unauthorized.", 401, 401);
             }
         }
         // extract func params from path:
         // /1.1/call/{0}/{1}
         $funcParams = explode("/", ltrim($pathParts["extra"], "/"));
         if (count($funcParams) == 1) {
             // {1,1.1}/functions/{funcName}
             $this->dispatchFunc($funcParams[0], $json, $pathParts["endpoint"] === "call");
         } else {
             if ($funcParams[0] == "onVerified") {
                 // {1,1.1}/functions/onVerified/sms
                 $this->dispatchOnVerified($funcParams[1], $json);
             } else {
                 if ($funcParams[0] == "_User" && $funcParams[1] == "onLogin") {
                     // {1,1.1}/functions/_User/onLogin
                     $this->dispatchOnLogin($json);
                 } else {
                     if ($funcParams[0] == "BigQuery" || $funcParams[0] == "Insight") {
                         // {1,1.1}/functions/Insight/onComplete
                         $this->dispatchOnInsight($json);
                     } else {
                         if (count($funcParams) == 2) {
                             // {1,1.1}/functions/{className}/beforeSave
                             $this->dispatchHook($funcParams[0], $funcParams[1], $json);
                         }
                     }
                 }
             }
         }
     }
 }