Skip to content

Commit

Permalink
Add setServerUrl close #168 #166 #162
Browse files Browse the repository at this point in the history
  • Loading branch information
juvenn committed Mar 17, 2018
1 parent de96d95 commit f5aeeb3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/LeanCloud/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class Client {
*/
private static $appMasterKey;

/**
* Server url
*
* @var string
*/
private static $serverUrl;

/**
* Use master key or not
*
Expand Down Expand Up @@ -194,6 +201,18 @@ public static function useMasterKey($flag) {
self::$useMasterKey = $flag ? true : false;
}

/**
* Set server url
*
* Explicitly set server url with which this client will communicate.
* Url shall be in the form of: `https://api.leancloud.cn` .
*
* @param string $url
*/
public static function setServerUrl($url) {
self::$serverUrl = rtrim($url, "/");
}

/**
* Get API Endpoint
*
Expand All @@ -203,7 +222,14 @@ public static function useMasterKey($flag) {
* @return string
*/
public static function getAPIEndPoint() {
return getenv("LEANCLOUD_API_SERVER") . "/" . self::$apiVersion;
if ($url = self::$serverUrl) {
return $url . "/" . self::$apiVersion;
} else if ($url = getenv("LEANCLOUD_API_SERVER")) {
return $url . "/" . self::$apiVersion;
} else {
throw new \RuntimeException("Server url not set, please set it as:" .
"`Client::setServerUrl('https://{left-8-chars-of-appid}.api.lncld.net}')`");
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ public function setUp() {
Client::useMasterKey(false);
}

public function testAPIEndPoint() {
$url = getenv("LEANCLOUD_API_SERVER");
$this->assertEquals("{$url}/1.1", Client::getAPIEndPoint());

Client::setServerURL("https://hello.api.lncld.net");
$this->assertEquals("https://hello.api.lncld.net/1.1", Client::getAPIEndPoint());
Client::setServerURL(null);

$this->assertEquals("{$url}/1.1", Client::getAPIEndPoint());
}

public function testVerifyKey() {
$result = Client::verifyKey(
getenv("LEANCLOUD_APP_ID"),
Expand Down

0 comments on commit f5aeeb3

Please sign in to comment.