find() * */ public function __construct(array $lazypages, array $options = []) { $this->lazypages = $lazypages; $this->options = $options; } /** * Retrieves the next chunk of real pages * */ protected function loadChunk() { $this->chunkSize = (int) $this->wire()->config->lazyPageChunkSize; $this->pagesPosition = 0; $start = $this->currentChunk++ * $this->chunkSize; $pages = $this->wire()->pages; if($this->cacheGroup) { $this->wire()->pages->cacher()->uncacheGroup($this->cacheGroup); } // If the starting position exceeds the amount of placeholder objects, we just issue an empty // PageArray, which causes the loop to stop (because valid() will return false) if(!isset($this->lazypages[$start])) { $this->pages = $pages->newPageArray(); } else { // Check if the user gave options for the loading $options = isset($this->options['loadOptions']) ? $this->options['loadOptions'] : array(); // Here we retrieve a chunk of Page objects and loop over them to retrieve the IDs of the Pages. $lazypages = array_slice($this->lazypages, $start, $this->chunkSize); $ids = array(); foreach($lazypages as $page) { // Grab the ID from the placeholder object. We are using the internal method here which does // not cause the real Page to be loaded. We only need to collect the IDs to request a chunk // of real Page-objects from Pages::getById() $ids[] = $page->id; } $this->cacheGroup = 'lazy' . md5(implode(',', $ids)); $options['cache'] = $this->cacheGroup; $debug = $pages->debug(); if($debug) $pages->debug(false); $this->pages = $pages->getById($ids, $options); if($debug) $pages->debug(true); } $this->pagesCount = count($this->pages); } /** * Rewind to beginning * */ #[\ReturnTypeWillChange] public function rewind() { $this->pagesPosition = 0; $this->position = 0; $this->currentChunk = 0; $this->pagesCount = 0; $this->pages = null; } /** * Get current Page * * @return Page * */ #[\ReturnTypeWillChange] public function current() { return $this->pages[$this->pagesPosition]; } /** * Get current key/position * * @return int * */ #[\ReturnTypeWillChange] public function key() { return $this->position; } /** * Update current position to next * */ #[\ReturnTypeWillChange] public function next() { $this->pagesPosition++; $this->position++; } /** * Return whether or not there are more items after current position * * @return bool * */ #[\ReturnTypeWillChange] public function valid() { if($this->position === 0 || $this->pagesPosition >= $this->pagesCount) { // If we have just been rewound or if we have reached the end of the buffer, // we will load the next chunk. $this->loadChunk(); } // We have reached the end when no Pages were loaded return ($this->pagesCount > 0); } }