Completed
Push — master ( 1ba0cd...0b3834 )
by Henry
08:11
created

Filesystem::copy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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->updateIterator();
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
	 * update the filesystem iterator
178 12
	 *
179
	 * @since 3.2.0
180
	 */
181
182 10
	public function updateIterator()
183 12
	{
184
		$this->_iterator = $this->_filterIterator($this->_scan($this->_root));
185
	}
186
187
	/**
188
	 * filter the filesystem iterator
189
	 *
190
	 * @since 3.2.0
191
	 *
192
	 * @param Traversable $iterator iterator of the filesystem
193
	 *
194
	 * @return Traversable
195
	 */
196 12
197
	protected function _filterIterator(Traversable $iterator = null) : Traversable
198 12
	{
199
		if ($this->_recursive)
200 10
		{
201
			return new RecursiveCallbackFilterIterator($iterator, $this->_validateItem());
202 7
		}
203
		return new CallbackFilterIterator($iterator, $this->_validateItem());
204 3
	}
205
206 2
	/**
207
	 * validate the filesystem item
208
	 *
209
	 * @since 3.2.0
210
	 *
211
	 * @return callable
212
	 */
213
214
	protected function _validateItem() : callable
215
	{
216
		return function ($item)
217
		{
218
			return !in_array($item->getFileName(), $this->_filterArray);
219
		};
220
	}
221
222
	/**
223
	 * scan the filesystem
224
	 *
225
	 * @since 3.2.0
226
	 *
227
	 * @param string $directory name of the directory
228
	 *
229
	 * @return Traversable
230
	 */
231
232
	protected function _scan(string $directory = null) : Traversable
233
	{
234
		if (is_dir($directory))
235
		{
236
			if ($this->_recursive)
237
			{
238
				return new RecursiveDirectoryIterator($directory);
239
			}
240
			return new DirectoryIterator($directory);
241
		}
242
		return new EmptyIterator();
243
	}
244
}
245