diff --git a/composer.json b/composer.json index 15ca74a3a1..cce8035984 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "api-clients/rx": "^2.1", "api-clients/rx-operators": "^2.0", "api-clients/transport": "^3.0", + "kelunik/link-header-rfc5988": "^1.0", "react/promise-stream": "^0.1.1", "wyrihaximus/react-stream-base64": "^1.0", "wyrihaximus/react-stream-json": "^1.0" diff --git a/composer.lock b/composer.lock index c8bcdcb5f8..ce4ee23c18 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "b7a490951441aaf48e3989c14789161b", + "content-hash": "e7dee045b0e4e54d01b9487ba442e860", "packages": [ { "name": "api-clients/client-services", @@ -1263,6 +1263,46 @@ ], "time": "2015-07-16T22:30:20+00:00" }, + { + "name": "kelunik/link-header-rfc5988", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/kelunik/link-header-rfc5988.git", + "reference": "027a34c61060eb349c1b9c875186cc68812fd14a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/kelunik/link-header-rfc5988/zipball/027a34c61060eb349c1b9c875186cc68812fd14a", + "reference": "027a34c61060eb349c1b9c875186cc68812fd14a", + "shasum": "" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.3", + "phpunit/phpunit": "^6" + }, + "type": "library", + "autoload": { + "psr-4": { + "Kelunik\\LinkHeaderRfc5988\\": "src" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Niklas Keller", + "email": "me@kelunik.com" + } + ], + "description": "RFC 5988 compatible link header parser.", + "time": "2017-09-16T16:48:40+00:00" + }, { "name": "league/tactician", "version": "v1.0.2", diff --git a/src/Service/IteratePagesService.php b/src/Service/IteratePagesService.php index ec1e544708..7bdd098f1c 100644 --- a/src/Service/IteratePagesService.php +++ b/src/Service/IteratePagesService.php @@ -9,6 +9,7 @@ use Rx\Observable; use Rx\Scheduler; use Rx\Subject\Subject; +use function Kelunik\LinkHeaderRfc5988\parseLinks; class IteratePagesService { @@ -46,23 +47,18 @@ public function iterate(string $path): Observable return; } + $parsedLinks = parseLinks($response->getHeaderLine('link')); $links = [ - 'next' => false, - 'last' => false, + 'next' => $parsedLinks->getByRel('next'), + 'last' => $parsedLinks->getByRel('last'), ]; - foreach (explode(', ', $response->getHeader('link')[0]) as $link) { - list($url, $rel) = explode('>; rel="', ltrim(rtrim($link, '"'), '<')); - if (isset($links[$rel])) { - $links[$rel] = $url; - } - } - if ($links['next'] === false || $links['last'] === false) { + if ($links['next'] === null || $links['last'] === null) { return; } $this->scheduler->schedule(function () use ($paths, $links) { - $paths->onNext($links['next']); + $paths->onNext($links['next']->getUri()); }); }) ->map(function (ResponseInterface $response) {