<?php
namespace ModulesAbstract\ImportedOrdersFileManager\Model\Sources; class GeneralFilesystemScanner implements \Iterator { private $path = NULL; private $recurse = NULL; private $filterCallback = NULL; private $iterator = NULL; private $current = NULL; private $currentIndex = -1; private $iterating = false; private $totalNumberOfResults = NULL; private $numberOfResults = NULL; protected $layoutHandler = NULL; protected $source = NULL; private $shouldStop = false; public function setShouldStop($shouldStop) { $this->shouldStop = $shouldStop; return $this; } public function setLayoutHandler(\ModulesAbstract\ImportedOrdersFileManager\Model\LayoutHandler $layoutHandler) { $this->layoutHandler = $layoutHandler; return $this; } public function setSource(\Iterator $source) { $this->source = $source; return $this; } public function getSource() { return $this->source; } public function setParams($path, $recurse, $filterCallback) { $this->path = $path; $this->recurse = (bool)$recurse; $this->filterCallback = $filterCallback; if ($this->recurse) { $this->iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path), \RecursiveIteratorIterator::SELF_FIRST); } else { $this->iterator = new \DirectoryIterator($this->path); } $this->rewind(); return $this; } public function getPath() { return $this->path; } public function getTotalNumberOfResults() { return $this->totalNumberOfResults; } public function getNumberOfResults() { return $this->numberOfResults; } public function current() :mixed { return $this->current; } public function key() :mixed { return $this->currentIndex; } public function next() :void { } public function rewind() :void { $this->iterating = false; $this->currentIndex = -1; if ($this->iterator) { $this->iterator->rewind(); } } public function valid() :bool { if ($this->iterator) { if (!$this->iterating) { $this->iterator->rewind(); $this->iterating = true; $this->currentIndex = -1; } while ($this->iterator->valid() && !$this->shouldStop) { $file = $this->iterator->current(); if ($file) { $pathinfo = pathinfo($file->getPathname()); if ($pathinfo['basename'][0] == '.') { $this->iterator->next(); continue; } if ($file->isDir()) { $this->current = new \ModulesAbstract\ImportedOrdersFileManager\Model\DirCached($file->getPathname()); } else { $this->current = new \ModulesAbstract\ImportedOrdersFileManager\Model\File($file->getPathname()); } $this->iterator->next(); $callback = $this->filterCallback; if ($callback($this->current->getFullPath(), $this->currentIndex, $this->current)) { $this->currentIndex++; $this->numberOfResults++; return true; } } } } return false; } } 