// Create the GithubService with the prepared client $github = new GithubService($artaxClient, $reactor, new NullResponseCache(), 'Danack/GithubArtaxService'); //Get the first page of data $command = $github->listRepoTags(new NullToken(), 'php', 'php-src'); $listRepoTagsCallback = function (Exception $exception = null, Tags $repoTags, Response $response = null) use($github) { if ($exception) { echo "An error occurred: " . $exception->getMessage(); return; } echo "Tags on first page:\n"; foreach ($repoTags as $repoTag) { echo "Found tag: " . $repoTag->name . "\n"; } $pages = $repoTags->pager->getAllKnownPages(); foreach ($pages as $page) { $command = $github->listRepoTagsPaginate(new NullToken(), $page); $callback = getListRepoTagsPagingCallback($page); echo "Fetching asynchronously: {$page} \n"; $command->executeAsync($callback); } }; // This prepares the request but it is not executed until the reactor starts running $repoTags = $command->executeAsync($listRepoTagsCallback); $time = microtime(true); // So start the reactor $reactor->run(); echo "fin: " . (microtime(true) - $time) . "\n"; /** * Create a callback for processing the response from listRepoTagsPaginate. * All this is doing is 'closing over' the $page variable, so that it can be used * when processing the response.
/** * @param GithubService $api * @param AccessResponse $accessResponse * @param $username * @param $reponame */ function showAllTagsForRepo(GithubService $api, AccessResponse $accessResponse, $username, $reponame, $maxPages = 20) { $results = []; echo "Sorry, this needs updating. <br/>"; return; // What to do if an individual request in the batch fails $onError = function ($requestKey, Exception $error) { echo 'Error: (', $requestKey, ') ', get_class($error) . ": ", $error->getMessage(), "\n"; }; $command = $api->listRepoTags(new Oauth2Token($accessResponse->accessToken), $username, $reponame); $repoTags = $command->execute(); $results[] = $repoTags; $pages = []; if ($repoTags->pager) { $newPages = $repoTags->pager->getAllKnownPages(); foreach ($newPages as $newPage) { $pages[$newPage] = false; } } $finished = false; $count = 0; while ($finished == false) { $maxPages++; //TODO - this is in the wrong place, it's currently max iterations. if ($count > $maxPages) { break; } $requestsAndCallbacks = []; foreach ($pages as $uri => $alreadyFetched) { if ($alreadyFetched == false) { $pages[$uri] = true; $command = $api->listRepoTagsPaginate(new Oauth2Token(new Oauth2Token($accessResponse->accessToken)), $uri); $onResponse = function ($requestKey, Response $response) use($command, &$pages, &$results) { $newRepoTags = $command->processResponse($response); $results[] = $newRepoTags; if ($newRepoTags->pager) { $newPages = $newRepoTags->pager->getAllKnownPages(); foreach ($newPages as $newPage) { if (isset($pages[$newPage]) == false) { $pages[$newPage] = false; } } } }; $requestsAndCallbacks[] = [$command->createRequest(), $onResponse, $onError]; } } if (count($requestsAndCallbacks)) { $client = new ArtaxClient(); //$client->requestMultiVerse($requestsAndCallbacks); //$client->requestMulti($requestsAndCallbacks); } else { $finished = true; } } $mergedRepoTags = []; foreach ($results as $result) { foreach ($result->repoTags as $repoTag) { $mergedRepoTags[$repoTag->name] = $repoTag; } } return $mergedRepoTags; }