71 public function mount( $prefix, $instance ) {
72 if ( !preg_match( self::VALID_MOUNT_REGEX, $prefix ) ) {
73 throw new UnexpectedValueException(
"Invalid service mount point '$prefix'." );
74 } elseif ( isset( $this->instances[$prefix] ) ) {
75 throw new UnexpectedValueException(
"A service is already mounted on '$prefix'." );
78 if ( !isset( $instance[
'class'] ) || !isset( $instance[
'config'] ) ) {
79 throw new UnexpectedValueException(
"Missing 'class' or 'config' ('$prefix')." );
82 $this->instances[$prefix] = $instance;
165 foreach ( $reqs as $index => &$req ) {
166 if ( isset( $req[0] ) ) {
167 $req[
'method'] = $req[0];
170 if ( isset( $req[1] ) ) {
171 $req[
'url'] = $req[1];
179 $armoredIndexMap = [];
183 $replaceReqsByService = [];
186 foreach ( $reqs as $origIndex => $req ) {
188 $index = $curUniqueId++;
189 $armoredIndexMap[$origIndex] = $index;
190 $origPending[$index] = 1;
191 if ( preg_match(
'#^(http|ftp)s?://#', $req[
'url'] ) ) {
193 $executeReqs[$index] = $req;
198 throw new UnexpectedValueException(
"Path '{$req['url']}' has no service." );
201 $req[
'url'] = substr( $req[
'url'], strlen( $prefix ) );
202 $replaceReqsByService[$prefix][$index] = $req;
207 $idFunc =
static function () use ( &$curUniqueId ) {
208 return $curUniqueId++;
213 if ( ++$rounds > 5 ) {
214 throw new Exception(
"Too many replacement rounds detected. Aborting." );
218 $checkReqIndexesByPrefix = [];
223 $newReplaceReqsByService = [];
224 foreach ( $replaceReqsByService as $prefix => $servReqs ) {
225 $service = $this->getInstance( $prefix );
226 foreach ( $service->onRequests( $servReqs, $idFunc ) as $index => $req ) {
228 if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
232 $newReplaceReqsByService[$prefix][$index] = $req;
234 if ( isset( $req[
'response'] ) ) {
236 unset( $executeReqs[$index] );
237 unset( $origPending[$index] );
238 $doneReqs[$index] = $req;
241 $executeReqs[$index] = $req;
243 $checkReqIndexesByPrefix[$prefix][$index] = 1;
250 foreach ( $executeReqs as $index => &$req ) {
252 if ( preg_match(
'#^//#', $req[
'url'] ) ) {
261 isset( $req[
'reqTimeout'] ) &&
262 ( !isset( $opts[
'reqTimeout'] ) ||
263 $req[
'reqTimeout'] < $opts[
'reqTimeout'] )
265 $opts[
'reqTimeout'] = $req[
'reqTimeout'];
270 foreach ( $this->http->runMulti( $executeReqs, $opts ) as $index => $ranReq ) {
271 $doneReqs[$index] = $ranReq;
272 unset( $origPending[$index] );
280 $newReplaceReqsByService = [];
281 foreach ( $checkReqIndexesByPrefix as $prefix => $servReqIndexes ) {
282 $service = $this->getInstance( $prefix );
284 $servReqs = array_intersect_key( $doneReqs, $servReqIndexes );
285 foreach ( $service->onResponses( $servReqs, $idFunc ) as $index => $req ) {
287 if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
291 $newReplaceReqsByService[$prefix][$index] = $req;
293 if ( isset( $req[
'response'] ) ) {
295 unset( $origPending[$index] );
296 $doneReqs[$index] = $req;
299 $executeReqs[$index] = $req;
304 $replaceReqsByService = $newReplaceReqsByService;
305 }
while ( count( $origPending ) );
310 foreach ( $reqs as $origIndex => $req ) {
311 $index = $armoredIndexMap[$origIndex];
312 if ( !isset( $doneReqs[$index] ) ) {
313 throw new UnexpectedValueException(
"Response for request '$index' is NULL." );
315 $responses[$origIndex] = $doneReqs[$index][
'response'];