44
55use SDPMlab \Anser \Service \ServiceSettings ;
66use GuzzleHttp \HandlerStack ;
7-
7+ use SDPMlab \ Anser \ Exception \ ActionException ;
88class 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