1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace ntentan\utils\filesystem; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use ntentan\utils\Filesystem; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Holds a collection of files. |
11
|
|
|
* This may be returned from directory contents or glob operations. A collection can contain regular files and |
12
|
|
|
* directories and, its contents do not need to be from the same root directory. |
13
|
|
|
* |
14
|
|
|
* @package ntentan\utils\filesystem |
15
|
|
|
*/ |
16
|
|
|
class FileCollection implements \Iterator, \ArrayAccess, FileInterface, \Countable |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* An array holding all paths in this collection |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $paths; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Current index of the iterator in the list of paths. |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $iteratorIndex; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* A lightweight cache that holds instances of file objects created. |
32
|
|
|
* @var array<FileInterface> |
33
|
|
|
*/ |
34
|
|
|
private $instances; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* FileCollection constructor. |
38
|
|
|
* @param $paths |
39
|
|
|
*/ |
40
|
5 |
|
public function __construct($paths) |
41
|
|
|
{ |
42
|
5 |
|
$this->paths = $paths; |
43
|
5 |
|
$this->iteratorIndex = 0; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get an instance of a file object for a path in the collection. |
48
|
|
|
* This is used whenever an item must be returned from the collection. |
49
|
|
|
* |
50
|
|
|
* @param $index |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
5 |
|
private function getInstance($index) |
54
|
|
|
{ |
55
|
5 |
|
if(!isset($this->instances[$index])) { |
56
|
5 |
|
if(is_dir($this->paths[$index])) { |
57
|
5 |
|
$this->instances[$index] = Filesystem::directory($this->paths[$index]); |
58
|
|
|
} else { |
59
|
5 |
|
$this->instances[$index] = Filesystem::file($this->paths[$index]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
5 |
|
return $this->instances[$index]; |
63
|
|
|
} |
64
|
|
|
|
65
|
5 |
|
public function rewind(): void |
66
|
|
|
{ |
67
|
5 |
|
$this->iteratorIndex = 0; |
68
|
|
|
} |
69
|
|
|
|
70
|
5 |
|
public function current(): mixed |
71
|
|
|
{ |
72
|
5 |
|
return $this->getInstance($this->iteratorIndex); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function key(): mixed |
76
|
|
|
{ |
77
|
1 |
|
return $this->iteratorIndex; |
78
|
|
|
} |
79
|
|
|
|
80
|
5 |
|
public function next(): void |
81
|
|
|
{ |
82
|
5 |
|
$this->iteratorIndex++; |
83
|
|
|
} |
84
|
|
|
|
85
|
5 |
|
public function valid(): bool |
86
|
|
|
{ |
87
|
5 |
|
return isset($this->paths[$this->iteratorIndex]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function offsetSet($index, $path): void |
91
|
|
|
{ |
92
|
|
|
if(is_null($index)) { |
93
|
|
|
$this->paths[] = $path; |
94
|
|
|
} else { |
95
|
|
|
$this->paths[$index] = $path; |
96
|
|
|
unset($this->instances[$index]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function offsetExists($index): bool |
101
|
|
|
{ |
102
|
|
|
return isset($this->paths[$index]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function offsetGet($index): mixed |
106
|
|
|
{ |
107
|
|
|
return isset($this->paths[$index]) ? $this->paths[$index] : null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function offsetUnset($index): void |
111
|
|
|
{ |
112
|
|
|
unset($this->paths[$index]); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
public function moveTo(string $destination, int $ovewrite = 0): void |
116
|
|
|
{ |
117
|
1 |
|
foreach($this as $file) { |
118
|
1 |
|
$file->moveTo($destination . DIRECTORY_SEPARATOR . basename($file), $ovewrite); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
public function copyTo(string $destination, int $ovewrite = 0): void |
123
|
|
|
{ |
124
|
1 |
|
foreach($this as $file) { |
125
|
1 |
|
$file->copyTo($destination . DIRECTORY_SEPARATOR . basename($file), $ovewrite); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
public function getSize(): int |
130
|
|
|
{ |
131
|
1 |
|
return array_reduce(iterator_to_array($this), |
132
|
1 |
|
function($carry, $item){ |
133
|
1 |
|
return $carry + $item->getSize(); |
134
|
1 |
|
}, 0); |
135
|
|
|
} |
136
|
|
|
|
137
|
2 |
|
public function delete(): void |
138
|
|
|
{ |
139
|
2 |
|
foreach($this as $file) { |
140
|
1 |
|
$file->delete(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function getPath(): string |
145
|
|
|
{ |
146
|
|
|
return array_reduce($this->paths, |
147
|
|
|
function($carry, $path) { |
148
|
|
|
$carry .= escapeshellarg($path) . " "; |
149
|
|
|
}, ""); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
public function count(): int |
153
|
|
|
{ |
154
|
1 |
|
return count($this->paths); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function deleteIfExists(): void |
158
|
|
|
{ |
159
|
|
|
$this->delete(); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|