Skip to content

Commit 44e80ff

Browse files
committed
Merge branch 'review-curler-pager'
2 parents ee9034a + 1bda63e commit 44e80ff

File tree

17 files changed

+85
-70
lines changed

17 files changed

+85
-70
lines changed

composer.lock

Lines changed: 45 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Toolkit/Contract/Curler/CurlerPagerInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function getFirstRequest(
3535
*
3636
* @param mixed $data
3737
* @param TPage $previousPage
38+
* @param int|null $previousEntities The number of entities returned from
39+
* the endpoint via previous pages.
3840
* @param mixed[]|null $query The query applied to `$request` or returned by
3941
* {@see CurlerPageRequestInterface::getQuery()}, if applicable.
4042
* @return (TPage is null ? CurlerPageInterface : TPage)
@@ -45,6 +47,7 @@ public function getPage(
4547
ResponseInterface $response,
4648
CurlerInterface $curler,
4749
?CurlerPageInterface $previousPage = null,
50+
?int $previousEntities = null,
4851
?array $query = null
4952
): CurlerPageInterface;
5053
}

src/Toolkit/Core/Legacy/IntrospectionClass.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,9 +613,6 @@ function (int $a, int $b) use ($parents, $properties) {
613613
if (!in_array($property, $this->NormalisedKeys, true)) {
614614
continue;
615615
}
616-
if (!is_a($target, Relatable::class, true)) {
617-
continue;
618-
}
619616
switch ($type) {
620617
case Relatable::ONE_TO_ONE:
621618
$this->OneToOneRelationships[$property] = $target;

src/Toolkit/Core/Reflection/ClassReflection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,10 @@ public function getPropertyRelationships(): array
608608
[$this->getChildrenProperty(), Relatable::ONE_TO_MANY],
609609
] as [$property, $type]) {
610610
$name = $normaliser
611+
// @phpstan-ignore argument.type (PHPStan regression)
611612
? $normaliser($property, false)
612613
: $property;
614+
// @phpstan-ignore argument.type (PHPStan regression)
613615
$relationships[$name] = new PropertyRelationship($property, $type, $target);
614616
}
615617
$relationships = array_intersect_key($relationships, $declared);

src/Toolkit/Curler/Curler.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ private function process(string $method, ?array $query, $data = false)
382382
}
383383
$result = $this->getResponseBody($response);
384384
if ($pager) {
385-
$page = $pager->getPage($result, $request, $response, $this, null, $query);
385+
$page = $pager->getPage($result, $request, $response, $this, null, null, $query);
386386
return Arr::unwrap($page->getEntities(), 1);
387387
}
388388
return $result;
@@ -444,16 +444,18 @@ private function paginate(string $method, ?array $query, $data = false): iterabl
444444
$request = $this->createRequest($method, $query, $data);
445445
$request = $pager->getFirstRequest($request, $this, $query);
446446
$prev = null;
447+
$yielded = 0;
447448
do {
448449
if ($request instanceof CurlerPageRequestInterface) {
449450
$query = $request->getQuery() ?? $query;
450451
$request = $request->getRequest();
451452
}
452453
$response = $this->doSendRequest($request);
453454
$result = $this->getResponseBody($response);
454-
$page = $pager->getPage($result, $request, $response, $this, $prev, $query);
455+
$page = $pager->getPage($result, $request, $response, $this, $prev, $prev ? $yielded : null, $query);
455456
// Use `yield` instead of `yield from` so entities get unique keys
456457
foreach ($page->getEntities() as $entity) {
458+
$yielded++;
457459
yield $entity;
458460
}
459461
if (!$page->hasNextRequest()) {
@@ -1209,6 +1211,7 @@ static function ($handle, string $data) use (&$bodyIn): int {
12091211
self::$Handle = $handle;
12101212
$resetHandle = false;
12111213
} else {
1214+
$handle = self::$Handle;
12121215
$opt[\CURLOPT_URL] = (string) $uri;
12131216
$resetHandle = true;
12141217
}
@@ -1291,32 +1294,32 @@ static function ($handle, string $data) use (&$bodyIn): int {
12911294

12921295
if ($resetHandle || !$transfer) {
12931296
if ($resetHandle) {
1294-
curl_reset(self::$Handle);
1297+
curl_reset($handle);
12951298
$resetHandle = false;
12961299
}
12971300
$opt[\CURLOPT_HTTPHEADER] = $headers->getLines('%s: %s', '%s;');
1298-
curl_setopt_array(self::$Handle, $opt);
1301+
curl_setopt_array($handle, $opt);
12991302

13001303
if ($this->CookiesCacheKey !== null) {
13011304
// "If the name is an empty string, no cookies are loaded,
13021305
// but cookie handling is still enabled"
1303-
curl_setopt(self::$Handle, \CURLOPT_COOKIEFILE, '');
1306+
curl_setopt($handle, \CURLOPT_COOKIEFILE, '');
13041307
/** @var non-empty-string[] */
13051308
$cookies = $this->getCacheInstance()->getArray($this->CookiesCacheKey);
13061309
if ($cookies) {
13071310
foreach ($cookies as $cookie) {
1308-
curl_setopt(self::$Handle, \CURLOPT_COOKIELIST, $cookie);
1311+
curl_setopt($handle, \CURLOPT_COOKIELIST, $cookie);
13091312
}
13101313
}
13111314
}
13121315
}
13131316

13141317
$transfer++;
13151318

1316-
Event::dispatch(new CurlRequestEvent($this, self::$Handle, $request));
1317-
$result = curl_exec(self::$Handle);
1319+
Event::dispatch(new CurlRequestEvent($this, $handle, $request));
1320+
$result = curl_exec($handle);
13181321
if ($result === false) {
1319-
throw new CurlErrorException(curl_errno(self::$Handle), $request, $this->getCurlInfo());
1322+
throw new CurlErrorException(curl_errno($handle), $request, $this->getCurlInfo());
13201323
}
13211324

13221325
if (
@@ -1336,12 +1339,12 @@ static function ($handle, string $data) use (&$bodyIn): int {
13361339
$code = (int) $split[1];
13371340
$reason = $split[2] ?? null;
13381341
$response = new Response($code, $bodyIn, $headersIn, $reason, $version);
1339-
Event::dispatch(new CurlResponseEvent($this, self::$Handle, $request, $response));
1342+
Event::dispatch(new CurlResponseEvent($this, $handle, $request, $response));
13401343

13411344
if ($this->CookiesCacheKey !== null) {
13421345
$this->getCacheInstance()->set(
13431346
$this->CookiesCacheKey,
1344-
curl_getinfo(self::$Handle, \CURLINFO_COOKIELIST)
1347+
curl_getinfo($handle, \CURLINFO_COOKIELIST)
13451348
);
13461349
}
13471350

@@ -1397,7 +1400,7 @@ static function ($handle, string $data) use (&$bodyIn): int {
13971400
$resetHandle = true;
13981401
} else {
13991402
curl_setopt(
1400-
self::$Handle,
1403+
$handle,
14011404
\CURLOPT_URL,
14021405
// @phpstan-ignore argument.type
14031406
$opt[\CURLOPT_URL] = (string) $uri,

src/Toolkit/Curler/Pager/LinkPager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function getPage(
7575
ResponseInterface $response,
7676
CurlerInterface $curler,
7777
?CurlerPageInterface $previousPage = null,
78+
?int $previousEntities = null,
7879
?array $query = null
7980
): CurlerPageInterface {
8081
$data = ($this->EntitySelector)($data);

src/Toolkit/Curler/Pager/ODataPager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function getPage(
6666
ResponseInterface $response,
6767
CurlerInterface $curler,
6868
?CurlerPageInterface $previousPage = null,
69+
?int $previousEntities = null,
6970
?array $query = null
7071
): CurlerPageInterface {
7172
if (!is_array($data)) {

src/Toolkit/Curler/Pager/QueryPager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function getPage(
7373
ResponseInterface $response,
7474
CurlerInterface $curler,
7575
?CurlerPageInterface $previousPage = null,
76+
?int $previousEntities = null,
7677
?array $query = null
7778
): CurlerPageInterface {
7879
$data = ($this->EntitySelector)($data);

0 commit comments

Comments
 (0)