From 8ca6cc32b2c789a3149861159ad258f2cb9491e3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:39:39 +0200 Subject: Adding upstream version 2.11.4. Signed-off-by: Daniel Baumann --- library/Icinga/Util/DirectoryIterator.php | 213 ++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 library/Icinga/Util/DirectoryIterator.php (limited to 'library/Icinga/Util/DirectoryIterator.php') diff --git a/library/Icinga/Util/DirectoryIterator.php b/library/Icinga/Util/DirectoryIterator.php new file mode 100644 index 0000000..a2bed1c --- /dev/null +++ b/library/Icinga/Util/DirectoryIterator.php @@ -0,0 +1,213 @@ +path = $path; + if (! empty($extension)) { + $this->extension = '.' . ltrim($extension, '.'); + } + if ($flags !== null) { + $this->flags = $flags; + } + } + + /** + * Check whether the given path is a directory and is readable + * + * @param string $path The path of the directory + * + * @return bool + */ + public static function isReadable($path) + { + return is_dir($path) && is_readable($path); + } + + public function hasChildren(): bool + { + return static::isReadable($this->current); + } + + public function getChildren(): DirectoryIterator + { + return new static($this->current, $this->extension, $this->flags); + } + + #[\ReturnTypeWillChange] + public function current() + { + return $this->current; + } + + public function next(): void + { + do { + $this->files->next(); + $skip = false; + if (! $this->files->valid()) { + $file = false; + $path = false; + break; + } else { + $file = $this->files->current(); + do { + if ($this->skipHidden && $file[0] === '.') { + $skip = true; + break; + } + + $path = $this->path . '/' . $file; + + if (is_dir($path)) { + if ($this->flags & static::FILES_FIRST === static::FILES_FIRST) { + $this->queue[] = array($path, $file); + $skip = true; + } + break; + } + + if ($this->skipEmpty && ! filesize($path)) { + $skip = true; + break; + } + + if ($this->extension && ! StringHelper::endsWith($file, $this->extension)) { + $skip = true; + break; + } + } while (0); + } + } while ($skip); + + /** @noinspection PhpUndefinedVariableInspection */ + + if ($path === false && ! empty($this->queue)) { + list($path, $file) = array_shift($this->queue); + } + + $this->current = $path; + $this->key = $file; + } + + #[\ReturnTypeWillChange] + public function key() + { + return $this->key; + } + + public function valid(): bool + { + return $this->current !== false; + } + + public function rewind(): void + { + if ($this->files === null) { + $files = scandir($this->path); + natcasesort($files); + $this->files = new ArrayIterator($files); + } + $this->files->rewind(); + $this->queue = array(); + $this->next(); + } +} -- cgit v1.2.3