1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the PHP Generics package. |
4
|
|
|
* |
5
|
|
|
* @package Generics |
6
|
|
|
*/ |
7
|
|
|
namespace Generics\Util; |
8
|
|
|
|
9
|
|
|
use Generics\DirectoryException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This utility class helps on acting on directories |
13
|
|
|
* |
14
|
|
|
* @author Maik Greubel <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Directory |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The absolute path of directory |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $path; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create new directory object |
28
|
|
|
* |
29
|
|
|
* @param string $path |
30
|
|
|
*/ |
31
|
3 |
|
public function __construct($path) |
32
|
|
|
{ |
33
|
3 |
|
$this->path = $this->fixDirectorySeparator($path); |
34
|
3 |
|
if ($this->exists()) { |
35
|
1 |
|
$this->path = realpath($this->path); |
36
|
|
|
} |
37
|
3 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Checks whether directory is empty or not |
41
|
|
|
* |
42
|
|
|
* @param string $filter |
43
|
|
|
* The filter for entries to skip |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
* |
47
|
|
|
* @throws DirectoryException |
48
|
|
|
*/ |
49
|
3 |
|
public function isEmpty($filter = null): bool |
50
|
|
|
{ |
51
|
3 |
|
if (! $this->exists()) { |
52
|
|
|
throw new DirectoryException("Directory {dir} does not exist", array( |
53
|
|
|
'dir' => $this->path |
54
|
|
|
)); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
$iter = new \DirectoryIterator($this->path); |
58
|
3 |
|
while ($iter->valid()) { |
59
|
3 |
|
if (! $iter->isDot() && ($filter === null || ! preg_match("/$filter/", $iter->getFilename()))) { |
60
|
2 |
|
return false; |
61
|
|
|
} |
62
|
3 |
|
$iter->next(); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Remove a directory |
70
|
|
|
* |
71
|
|
|
* @param boolean $recursive |
72
|
|
|
* Whether to remove it if its not empty |
73
|
|
|
* |
74
|
|
|
* @throws DirectoryException |
75
|
|
|
*/ |
76
|
3 |
|
public function remove($recursive = false) |
77
|
|
|
{ |
78
|
3 |
|
if (! $this->exists()) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
if ($this->isEmpty()) { |
83
|
1 |
|
if (rmdir($this->path) === false) { |
84
|
|
|
throw new DirectoryException("Could not remove directory {dir}", array( |
85
|
|
|
'dir' => $this->path |
86
|
|
|
)); |
87
|
|
|
} |
88
|
1 |
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
if (! $recursive) { |
92
|
|
|
throw new DirectoryException("Directory {dir} is not empty", array( |
93
|
|
|
'dir' => $this->path |
94
|
|
|
)); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
$iter = new \DirectoryIterator($this->path); |
98
|
2 |
|
while ($iter->valid()) { |
99
|
2 |
|
if ($iter->isDot()) { |
100
|
2 |
|
$iter->next(); |
101
|
2 |
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
if ($iter->isDir()) { |
105
|
1 |
|
$dir = new Directory($iter->getPathname()); |
106
|
1 |
|
$dir->remove(true); |
107
|
|
|
} else { |
108
|
2 |
|
unlink($iter->getPathname()); |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
$iter->next(); |
112
|
|
|
} |
113
|
2 |
|
rmdir($this->path); |
114
|
2 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Create a directory |
118
|
|
|
* |
119
|
|
|
* @param boolean $recursive |
120
|
|
|
* Create also sub directories |
121
|
|
|
* |
122
|
|
|
* @throws DirectoryException |
123
|
|
|
*/ |
124
|
3 |
|
public function create($recursive = false, $mode = 0755) |
125
|
|
|
{ |
126
|
3 |
|
if ($this->exists()) { |
127
|
|
|
throw new DirectoryException("Directory {dir} already exists", array( |
128
|
|
|
'dir' => $this->path |
129
|
|
|
)); |
130
|
|
|
} |
131
|
|
|
|
132
|
3 |
|
if (mkdir($this->path, $mode, $recursive) === false) { |
133
|
|
|
throw new DirectoryException("Could not create the directory {dir}", array( |
134
|
|
|
'dir' => $this->path |
135
|
|
|
)); |
136
|
|
|
} |
137
|
3 |
|
$this->path = realpath($this->path); |
138
|
3 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Checks whether directory exists |
142
|
|
|
* |
143
|
|
|
* @throws DirectoryException |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
3 |
|
public function exists(): bool |
147
|
|
|
{ |
148
|
3 |
|
if (! file_exists($this->path)) { |
149
|
3 |
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
3 |
|
if (! is_dir($this->path)) { |
153
|
|
|
throw new DirectoryException("Entry {path} exists, but it is not a directory!", array( |
154
|
|
|
'path' => $this->path |
155
|
|
|
)); |
156
|
|
|
} |
157
|
|
|
|
158
|
3 |
|
return true; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Retrieve the path |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
2 |
|
public function getPath(): string |
167
|
|
|
{ |
168
|
2 |
|
return $this->path; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Check whether a particular file exist in directory |
173
|
|
|
* |
174
|
|
|
* @param string $fileName |
175
|
|
|
* The file name to check |
176
|
|
|
* |
177
|
|
|
* @throws DirectoryException |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
1 |
|
public function fileExists($fileName): bool |
182
|
|
|
{ |
183
|
1 |
|
if (! $this->exists()) { |
184
|
1 |
|
return false; |
185
|
|
|
} |
186
|
|
|
|
187
|
1 |
|
$file = sprintf("%s/%s", $this->path, $fileName); |
188
|
|
|
|
189
|
1 |
|
return file_exists($file); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Generate a platform specific path by replacing invalid directory separators |
194
|
|
|
* |
195
|
|
|
* @param string $path |
196
|
|
|
* The path to check |
197
|
|
|
* @return string The corrected path |
198
|
|
|
*/ |
199
|
3 |
|
private function fixDirectorySeparator($path): string |
200
|
|
|
{ |
201
|
3 |
|
$path = str_replace("\\", DIRECTORY_SEPARATOR, $path); |
202
|
3 |
|
$path = str_replace("/", DIRECTORY_SEPARATOR, $path); |
203
|
|
|
|
204
|
3 |
|
return $path; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|