_destination = $destination; $this->_directory = rtrim( $destination->getRealPath( $directory ), "/" ); $this->_recursive = $recursive; if(!$destination->dirExists($directory)) return; $this->rewind(); } /** * @return void * @throws IOException */ private function _load():void { try { $this->_items = []; $list = null; while (!$list || $list->Body->has_more) { $cursor = ''; if($list && !($cursor = $list->Body->cursor)) return; if($cursor) $list = $this->_destination->client('listFolderContinue',$cursor); else $list = $this->_destination->client('listFolder', $this->_directory, $this->_recursive); foreach($list->Body->entries as $item) $this->_items[$item->name] = $item; } krsort($this->_items, SORT_STRING); } catch(\Exception $e) { throw new IOException($e->getMessage(), $e->getCode(), $e); } } /** * @return void * @throws IOException */ public function rewind():void { $this->_load(); } /** * @return bool */ public function hasNext():bool { return $this->_items && sizeof($this->_items); } /** * @throws IOException * @return iDestinationFile|null */ public function getNext():?iDestinationFile { if(!$this->hasNext()) return null; $nextfile = array_pop($this->_items); $path = $this->_destination->removeRealPath('/' . trim($nextfile->path_display, '/')); if(strpos($path, '/') !== false) $path = preg_replace("/^\/+/", "", dirname($path)); else $path = ''; $is_dir = $nextfile->{'.tag'} == 'folder'; $file = new DestinationFile(); $file->setType($is_dir ? iDestinationFile::TYPE_DIRECTORY : iDestinationFile::TYPE_FILE); $file->setName($nextfile->name); $file->setPath('/' . $path); $file->setSize($is_dir ? 4096 : $nextfile->size); if(!$is_dir) $file->setModifyTime(strtotime($nextfile->client_modified)); return $file; } }