Skip to content

Commit

Permalink
Parse JSON streams without loading into memory
Browse files Browse the repository at this point in the history
  • Loading branch information
nbish11 committed May 20, 2019
1 parent bde02a2 commit 8b08051
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use InvalidArgumentException;
use Generator;
use RuntimeException;
use JsonMachine\JsonMachine;

/**
* A simple class for fetching data from a single GitHub repository.
Expand Down Expand Up @@ -173,13 +174,15 @@ public function getMilestones(array $params = []): Generator
*/
private function fetch(string $url): Generator
{
$response = file_get_contents($url, false, $this->context);
$response = fopen($url, 'r', false, $this->context);

if (!$response) {
throw new RuntimeException(sprintf('Cannot connect to: %s', $url));
}

yield from json_decode($response);
yield from JsonMachine::fromStream($response);

fclose($response);

// "It's important to form calls with Link header values instead of constructing your own URLs." - GitHub
if ($nextPage = $this->getNextPageFromLinkHeader($http_response_header)) {
Expand Down

0 comments on commit 8b08051

Please sign in to comment.