summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Util/DirectoryIterator.php
blob: cee37b6b54359640b3881b71a340b14a42ae0f6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Util;

use ArrayIterator;
use InvalidArgumentException;
use RecursiveIterator;

/**
 * Iterator for traversing a directory
 */
class DirectoryIterator implements RecursiveIterator
{
    /**
     * Iterate files first
     *
     * @var int
     */
    const FILES_FIRST = 1;

    /**
     * Current directory item
     *
     * @var string|false
     */
    private $current;

    /**
     * The file extension to filter for
     *
     * @var string
     */
    protected $extension;

    /**
     * Scanned files
     *
     * @var ArrayIterator
     */
    private $files;

    /**
     * Iterator flags
     *
     * @var int
     */
    protected $flags;

    /**
     * Current key
     *
     * @var string|false
     */
    private $key;

    /**
     * The path of the directory to traverse
     *
     * @var string
     */
    protected $path;

    /**
     * Directory queue if FILES_FIRST flag is set
     *
     * @var array
     */
    private $queue;

    /**
     * Whether to skip empty files
     *
     * Defaults to true.
     *
     * @var bool
     */
    protected $skipEmpty = true;

    /**
     * Whether to skip hidden files
     *
     * Defaults to true.
     *
     * @var bool
     */
    protected $skipHidden = true;

    /**
     * Create a new directory iterator from path
     *
     * The given path will not be validated whether it is readable. Use {@link isReadable()} before creating a new
     * directory iterator instance.
     *
     * @param   string  $path       The path of the directory to traverse
     * @param   string  $extension  The file extension to filter for. A leading dot is optional
     * @param   int     $flags      Iterator flags
     */
    public function __construct($path, $extension = null, $flags = null)
    {
        if (empty($path)) {
            throw new InvalidArgumentException('The path can\'t be empty');
        }
        $this->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
    {
        $path = null;
        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();
    }
}