70 public function mount( $prefix, $instance ) {
71 if ( !preg_match( self::VALID_MOUNT_REGEX, $prefix ) ) {
72 throw new UnexpectedValueException(
"Invalid service mount point '$prefix'." );
73 } elseif ( isset( $this->instances[$prefix] ) ) {
74 throw new UnexpectedValueException(
"A service is already mounted on '$prefix'." );
77 if ( !isset( $instance[
'class'] ) || !isset( $instance[
'config'] ) ) {
78 throw new UnexpectedValueException(
"Missing 'class' or 'config' ('$prefix')." );
81 $this->instances[$prefix] = $instance;
90 if ( !preg_match( self::VALID_MOUNT_REGEX, $prefix ) ) {
91 throw new UnexpectedValueException(
"Invalid service mount point '$prefix'." );
92 } elseif ( !isset( $this->instances[$prefix] ) ) {
93 throw new UnexpectedValueException(
"No service is mounted on '$prefix'." );
95 unset( $this->instances[$prefix] );
164 foreach ( $reqs as $index => &$req ) {
165 if ( isset( $req[0] ) ) {
166 $req[
'method'] = $req[0];
169 if ( isset( $req[1] ) ) {
170 $req[
'url'] = $req[1];
178 $armoredIndexMap = [];
182 $replaceReqsByService = [];
185 foreach ( $reqs as $origIndex => $req ) {
187 $index = $curUniqueId++;
188 $armoredIndexMap[$origIndex] = $index;
189 $origPending[$index] = 1;
190 if ( preg_match(
'#^(http|ftp)s?://#', $req[
'url'] ) ) {
192 $executeReqs[$index] = $req;
197 throw new UnexpectedValueException(
"Path '{$req['url']}' has no service." );
200 $req[
'url'] = substr( $req[
'url'], strlen( $prefix ) );
201 $replaceReqsByService[$prefix][$index] = $req;
206 $idFunc =
function () use ( &$curUniqueId ) {
207 return $curUniqueId++;
212 if ( ++$rounds > 5 ) {
213 throw new Exception(
"Too many replacement rounds detected. Aborting." );
217 $checkReqIndexesByPrefix = [];
222 $newReplaceReqsByService = [];
223 foreach ( $replaceReqsByService as $prefix => $servReqs ) {
225 foreach ( $service->onRequests( $servReqs, $idFunc ) as $index => $req ) {
227 if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
231 $newReplaceReqsByService[$prefix][$index] = $req;
233 if ( isset( $req[
'response'] ) ) {
235 unset( $executeReqs[$index] );
236 unset( $origPending[$index] );
237 $doneReqs[$index] = $req;
240 $executeReqs[$index] = $req;
242 $checkReqIndexesByPrefix[$prefix][$index] = 1;
247 foreach ( $executeReqs as $index => &$req ) {
248 if ( preg_match(
'#^//#', $req[
'url'] ) ) {
254 foreach ( $this->http->runMulti( $executeReqs ) as $index => $ranReq ) {
255 $doneReqs[$index] = $ranReq;
256 unset( $origPending[$index] );
264 $newReplaceReqsByService = [];
265 foreach ( $checkReqIndexesByPrefix as $prefix => $servReqIndexes ) {
268 $servReqs = array_intersect_key( $doneReqs, $servReqIndexes );
269 foreach ( $service->onResponses( $servReqs, $idFunc ) as $index => $req ) {
271 if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
275 $newReplaceReqsByService[$prefix][$index] = $req;
277 if ( isset( $req[
'response'] ) ) {
279 unset( $origPending[$index] );
280 $doneReqs[$index] = $req;
283 $executeReqs[$index] = $req;
288 $replaceReqsByService = $newReplaceReqsByService;
289 }
while ( count( $origPending ) );
294 foreach ( $reqs as $origIndex => $req ) {
295 $index = $armoredIndexMap[$origIndex];
296 if ( !isset( $doneReqs[$index] ) ) {
297 throw new UnexpectedValueException(
"Response for request '$index' is NULL." );
299 $responses[$origIndex] = $doneReqs[$index][
'response'];
310 if ( !isset( $this->instances[$prefix] ) ) {
311 throw new RuntimeException(
"No service registered at prefix '{$prefix}'." );
315 $config = $this->instances[$prefix][
'config'];
316 $class = $this->instances[$prefix][
'class'];
317 $service =
new $class( $config );
319 throw new UnexpectedValueException(
"Registered service has the wrong class." );
321 $this->instances[$prefix] = $service;
324 return $this->instances[$prefix];