Completed
Push — master ( e9b1ca...cbd317 )
by Henry
07:52
created

Filesystem::refreshIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace Redaxscript\Filesystem;
3
4
use CallbackFilterIterator;
5
use DirectoryIterator;
6
use EmptyIterator;
7
use RecursiveCallbackFilterIterator;
8
use RecursiveDirectoryIterator;
9
use RecursiveIteratorIterator;
10
use Traversable;
11
use function array_merge;
12
use function in_array;
13
use function is_array;
14
use function is_dir;
15
use function iterator_count;
16
use function sort;
17
18
/**
19
 * parent class to handle the filesystem
20
 *
21
 * @since 3.2.0
22
 *
23
 * @package Redaxscript
24
 * @category Filesystem
25
 * @author Henry Ruhs
26
 */
27
28
class Filesystem
29
{
30
	/**
31
	 * value of the root
32
	 *
33
	 * @var string
34
	 */
35
36
	protected $_root;
37
38
	/**
39
	 * recursive flag
40
	 *
41
	 * @var bool
42
	 */
43
44
	protected $_recursive;
45
46
	/**
47
	 * array to be filtered
48
	 *
49
	 * @var array
50
	 */
51
52
	protected $_filterArray =
53
	[
54
		'.',
55
		'..'
56
	];
57
58
	/**
59
	 * iterator of the filesystem
60
	 *
61
	 * @var Traversable
62
	 */
63
64
	protected $_iterator;
65
66
	/**
67
	 * init the class
68
	 *
69
	 * @since 3.2.0
70 12
	 *
71
	 * @param string $root value of the root
72 12
	 * @param bool $recursive recursive flag
73 12
	 * @param array $filterArray array to be filtered
74 12
	 *
75
	 * @return self
76 12
	 */
77
78 12
	public function init(string $root = null, bool $recursive = false, array $filterArray = []) : self
79
	{
80
		$this->_root = $root;
81
		$this->_recursive = $recursive;
82
		if (is_array($filterArray))
83
		{
84
			$this->_filterArray = array_merge($this->_filterArray, $filterArray);
85
		}
86
		return $this;
87
	}
88 12
89
	/**
90 12
	 * copy the filesystem
91
	 *
92 12
	 * @since 4.0.0
93
	 *
94 12
	 * @return self
95
	 */
96 7
97
	public function copy() : self
98 5
	{
99
		return clone $this;
100
	}
101
102
	/**
103
	 * count the filesystem iterator
104
	 *
105
	 * @since 4.0.0
106
	 *
107
	 * @return int|null
108
	 */
109 7
110
	public function countIterator() : ?int
111 7
	{
112 7
		$iterator = $this->getIterator();
113
		return $iterator ? iterator_count($iterator) : null;
114
	}
115
116 7
	/**
117
	 * get the filesystem iterator
118 6
	 *
119
	 * @since 3.2.0
120 7
	 *
121
	 * @return Traversable
122
	 */
123
124
	public function getIterator() : Traversable
125
	{
126
		if (!$this->_iterator)
127
		{
128
			$this->refreshIterator();
129
		}
130
		if ($this->_recursive)
131
		{
132
			return new RecursiveIteratorIterator($this->_iterator, RecursiveIteratorIterator::SELF_FIRST);
133 2
		}
134
		return $this->_iterator;
135 2
	}
136 2
137 2
	/**
138
	 * get the filesystem array
139
	 *
140
	 * @since 3.2.0
141
	 *
142
	 * @return array
143
	 */
144
145
	public function getArray() : array
146 12
	{
147
		$filesystemArray = [];
148 12
		$iterator = $this->getIterator();
149 12
150
		/* process iterator */
151
152
		foreach ($iterator as $value)
153
		{
154
			$filesystemArray[] = $value->getBasename();
155
		}
156
		return $filesystemArray;
157
	}
158
159
	/**
160
	 * get the sorted filesystem array
161 12
	 *
162
	 * @since 3.2.0
163 12
	 *
164
	 * @param int $flag
165 7
	 *
166
	 * @return array
167 5
	 */
168
169
	public function getSortArray(int $flag = SORT_FLAG_CASE) : array
170
	{
171
		$filesystemArray = $this->getArray();
172
		sort($filesystemArray, $flag);
173
		return $filesystemArray;
174
	}
175
176
	/**
177
	 * refresh the filesystem iterator
178 12
	 *
179
	 * @since 3.2.0
180
	 *
181
	 * @return self
182 10
	 */
183 12
184
	public function refreshIterator() : self
185
	{
186
		$this->_iterator = $this->_filterIterator($this->_scan($this->_root));
187
		return $this;
188
	}
189
190
	/**
191
	 * filter the filesystem iterator
192
	 *
193
	 * @since 3.2.0
194
	 *
195
	 * @param Traversable $iterator iterator of the filesystem
196 12
	 *
197
	 * @return Traversable
198 12
	 */
199
200 10
	protected function _filterIterator(Traversable $iterator = null) : Traversable
201
	{
202 7
		if ($this->_recursive)
203
		{
204 3
			return new RecursiveCallbackFilterIterator($iterator, $this->_validateItem());
205
		}
206 2
		return new CallbackFilterIterator($iterator, $this->_validateItem());
207
	}
208
209
	/**
210
	 * validate the filesystem item
211
	 *
212
	 * @since 3.2.0
213
	 *
214
	 * @return callable
215
	 */
216
217
	protected function _validateItem() : callable
218
	{
219
		return function ($item)
220
		{
221
			return !in_array($item->getFileName(), $this->_filterArray);
222
		};
223
	}
224
225
	/**
226
	 * scan the filesystem
227
	 *
228
	 * @since 3.2.0
229
	 *
230
	 * @param string $directory name of the directory
231
	 *
232
	 * @return Traversable
233
	 */
234
235
	protected function _scan(string $directory = null) : Traversable
236
	{
237
		if (is_dir($directory))
238
		{
239
			if ($this->_recursive)
240
			{
241
				return new RecursiveDirectoryIterator($directory);
242
			}
243
			return new DirectoryIterator($directory);
244
		}
245
		return new EmptyIterator();
246
	}
247
}
248