diff --git a/src/Paginations/Pagination.php b/src/Paginations/Pagination.php index c9cfd80..93709c5 100644 --- a/src/Paginations/Pagination.php +++ b/src/Paginations/Pagination.php @@ -4,11 +4,11 @@ namespace Cerbero\LazyJsonPages\Paginations; +use Cerbero\LazyJsonPages\Concerns\ParsesPages; use Cerbero\LazyJsonPages\Concerns\ResolvesPages; -use Cerbero\LazyJsonPages\Concerns\YieldsPaginatedItems; use Cerbero\LazyJsonPages\Dtos\Config; use Cerbero\LazyJsonPages\Services\Book; -use Cerbero\LazyJsonPages\Sources\Source; +use Cerbero\LazyJsonPages\Sources\AnySource; use IteratorAggregate; use Traversable; @@ -19,7 +19,7 @@ */ abstract class Pagination implements IteratorAggregate { - use YieldsPaginatedItems; + use ParsesPages; use ResolvesPages; /** @@ -27,11 +27,6 @@ abstract class Pagination implements IteratorAggregate */ public readonly Book $book; - /** - * The number of items per page. - */ - protected readonly int $itemsPerPage; - /** * Yield the paginated items. * @@ -43,7 +38,7 @@ abstract public function getIterator(): Traversable; * Instantiate the class. */ final public function __construct( - protected readonly Source $source, + protected readonly AnySource $source, protected readonly Config $config, ) { $this->book = new Book(); diff --git a/src/Sources/AnySource.php b/src/Sources/AnySource.php index edd96c7..16b9269 100644 --- a/src/Sources/AnySource.php +++ b/src/Sources/AnySource.php @@ -28,7 +28,12 @@ class AnySource extends Source /** * The matching source. */ - protected ?Source $matchingSource; + protected readonly Source $matchingSource; + + /** + * The cached HTTP response. + */ + protected ?ResponseInterface $response; /** * Retrieve the HTTP request. @@ -67,6 +72,18 @@ protected function matchingSource(): Source */ public function response(): ResponseInterface { - return $this->matchingSource()->response(); + return $this->response ??= $this->matchingSource()->response(); + } + + /** + * Retrieve the HTTP response and forget it to save memory. + */ + public function pullResponse(): ResponseInterface + { + $response = $this->response(); + + $this->response = null; + + return $response; } } diff --git a/src/Sources/Endpoint.php b/src/Sources/Endpoint.php index 7eb81a9..3f55961 100644 --- a/src/Sources/Endpoint.php +++ b/src/Sources/Endpoint.php @@ -20,16 +20,6 @@ class Endpoint extends Source { use DetectsEndpoints; - /** - * The HTTP request. - */ - protected readonly RequestInterface $request; - - /** - * The HTTP response value object - */ - protected readonly ResponseInterface $response; - /** * Determine whether this class can handle the source. */ @@ -44,7 +34,7 @@ public function matches(): bool */ public function request(): RequestInterface { - return $this->request ??= new Request('GET', $this->source); + return new Request('GET', $this->source); } /** @@ -54,6 +44,6 @@ public function request(): RequestInterface */ public function response(): ResponseInterface { - return $this->response ??= Client::instance()->send($this->request()); + return Client::instance()->send($this->request()); } }