Skip to content

Commit 69708de

Browse files
authored
Merge pull request #16 from SDPM-lab/dev
Added Enhanced the getServiceData method.
2 parents 0431680 + 44ff9bd commit 69708de

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

src/Exception/ActionException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public static function forServiceDataNotFound(string $serviceName): ActionExcept
117117
return new self("尚未定義 {$serviceName} 服務進服務列表內,請檢查服務的 serviceName 是否正確。");
118118
}
119119

120+
public static function forServiceDataCallbackTypeError(string $serviceName): ActionException
121+
{
122+
return new self("Action {$serviceName} 定義的回呼函數回傳型別錯誤,請檢查回傳型別是否為 \SDPMlab\Anser\Service\ServiceSettings 或 null。");
123+
}
124+
120125
/**
121126
* 取得發生錯誤的 Restponse 實體
122127
*

src/Service/ServiceList.php

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use SDPMlab\Anser\Service\ServiceSettings;
66
use GuzzleHttp\HandlerStack;
7-
7+
use SDPMlab\Anser\Exception\ActionException;
88
class ServiceList
99
{
1010

@@ -22,6 +22,13 @@ class ServiceList
2222
*/
2323
protected static $globalHandlerCallback = null;
2424

25+
/**
26+
* ServiceList Update callback From Anser-Gateway
27+
*
28+
* @var null|callable
29+
*/
30+
protected static $serviceDataHandlerCallback = null;
31+
2532
/**
2633
* Guzzle7 HTTP Client Instance
2734
*
@@ -57,6 +64,17 @@ public static function setGlobalHandlerStack(callable $handler)
5764
static::$globalHandlerCallback = $handler;
5865
}
5966

67+
/**
68+
* Action will use this Handler to handle the Service Data.
69+
*
70+
* @param callable $handler
71+
* @return void
72+
*/
73+
public static function setServiceDataHandler(callable $handler)
74+
{
75+
static::$serviceDataHandlerCallback = $handler;
76+
}
77+
6078
/**
6179
* Add a new service to local service list.
6280
*
@@ -94,25 +112,33 @@ public static function getServiceList(): array
94112
*/
95113
public static function getServiceData(string $serviceName): ?ServiceSettings
96114
{
97-
if (filter_var($serviceName, FILTER_VALIDATE_URL) !== false) {
98-
$parseUrl = parse_url($serviceName);
99-
if(isset($parseUrl["port"])){
100-
$port = (int)$parseUrl["port"];
101-
}else{
102-
$port = $parseUrl["scheme"] === "https" ? 443 : 80;
115+
if(static::$serviceDataHandlerCallback === null){
116+
if (filter_var($serviceName, FILTER_VALIDATE_URL) !== false) {
117+
$parseUrl = parse_url($serviceName);
118+
if(isset($parseUrl["port"])){
119+
$port = (int)$parseUrl["port"];
120+
}else{
121+
$port = $parseUrl["scheme"] === "https" ? 443 : 80;
122+
}
123+
return new \SDPMlab\Anser\Service\ServiceSettings(
124+
$parseUrl["host"],
125+
$parseUrl["host"],
126+
$port,
127+
$parseUrl["scheme"] === "https"
128+
);
129+
}
130+
131+
if (isset(static::$localServiceList[$serviceName])) {
132+
return static::$localServiceList[$serviceName];
133+
} else {
134+
return null;
103135
}
104-
return new \SDPMlab\Anser\Service\ServiceSettings(
105-
$parseUrl["host"],
106-
$parseUrl["host"],
107-
$port,
108-
$parseUrl["scheme"] === "https"
109-
);
110-
}
111-
112-
if (isset(static::$localServiceList[$serviceName])) {
113-
return static::$localServiceList[$serviceName];
114136
} else {
115-
return null;
137+
$callableResult = call_user_func(static::$serviceDataHandlerCallback);
138+
if (!$callableResult instanceof ServiceSettings) {
139+
throw ActionException::forServiceDataCallbackTypeError($serviceName);
140+
}
141+
return $callableResult;
116142
}
117143
}
118144

0 commit comments

Comments
 (0)