|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File information retriever. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright 2018 Institute of Legal Medicine, Medical University of Innsbruck |
|
6
|
|
|
* @author Andreas Erhard <[email protected]> |
|
7
|
|
|
* @license LGPL-3.0-only |
|
8
|
|
|
* @link http://www.gerichtsmedizin.at/ |
|
9
|
|
|
* |
|
10
|
|
|
* @package fileinfo |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace Gmi\Toolkit\Fileinfo; |
|
13
|
|
|
|
|
14
|
|
|
use Gmi\Toolkit\Fileinfo\Exception\FileUnreadableException; |
|
15
|
|
|
use Gmi\Toolkit\Fileinfo\Exception\UnavailableException; |
|
16
|
|
|
use Gmi\Toolkit\Fileinfo\Part\DateInfo; |
|
17
|
|
|
use Gmi\Toolkit\Fileinfo\Part\DateInfoFactory; |
|
18
|
|
|
use Gmi\Toolkit\Fileinfo\Part\PathInfo; |
|
19
|
|
|
use Gmi\Toolkit\Fileinfo\Part\PathInfoFactory; |
|
20
|
|
|
use Gmi\Toolkit\Fileinfo\Part\PermissionInfo; |
|
21
|
|
|
use Gmi\Toolkit\Fileinfo\Part\PermissionInfoFactory; |
|
22
|
|
|
use Gmi\Toolkit\Fileinfo\Part\SizeInfo; |
|
23
|
|
|
use Gmi\Toolkit\Fileinfo\Part\SizeInfoFactory; |
|
24
|
|
|
use Gmi\Toolkit\Fileinfo\Part\TypeInfo; |
|
25
|
|
|
use Gmi\Toolkit\Fileinfo\Part\TypeInfoFactory; |
|
26
|
|
|
|
|
27
|
|
|
use SplFileInfo; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Retrieves information about file metadata. |
|
31
|
|
|
* |
|
32
|
|
|
* Instances of this class and file information value objects are immutable. |
|
33
|
|
|
* File metadata is read once in the constructor and can be limited there. |
|
34
|
|
|
*/ |
|
35
|
|
|
class FileInfo |
|
36
|
|
|
{ |
|
37
|
|
|
const INFO_NONE = 0; |
|
38
|
|
|
const INFO_PATH = 1; |
|
39
|
|
|
const INFO_SIZE = 2; |
|
40
|
|
|
const INFO_DATE = 4; |
|
41
|
|
|
const INFO_PERMISSION = 8; |
|
42
|
|
|
const INFO_TYPE = 16; |
|
43
|
|
|
const INFO_ALL = 65535; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $file; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var int |
|
52
|
|
|
*/ |
|
53
|
|
|
private $infoParts; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var PathInfo |
|
57
|
|
|
*/ |
|
58
|
|
|
private $pathInfo; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var PathInfoFactory |
|
62
|
|
|
*/ |
|
63
|
|
|
private $pathInfoFactory; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var SizeInfo |
|
67
|
|
|
*/ |
|
68
|
|
|
private $sizeInfo; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var SizeInfoFactory |
|
72
|
|
|
*/ |
|
73
|
|
|
private $sizeInfoFactory; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var DateInfo |
|
77
|
|
|
*/ |
|
78
|
|
|
private $dateInfo; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @var DateInfoFactory |
|
82
|
|
|
*/ |
|
83
|
|
|
private $dateInfoFactory; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var PermissionInfo |
|
87
|
|
|
*/ |
|
88
|
|
|
private $permissionInfo; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @var PermissionInfoFactory |
|
92
|
|
|
*/ |
|
93
|
|
|
private $permissionInfoFactory; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @var TypeInfo |
|
97
|
|
|
*/ |
|
98
|
|
|
private $typeInfo; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @var TypeInfoFactory |
|
102
|
|
|
*/ |
|
103
|
|
|
private $typeInfoFactory; |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Constructor. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $file Path and filename of the file. |
|
109
|
|
|
* @param int $infoParts Bitmask of INFO_* constants. |
|
110
|
|
|
* Parts which are not in the provided bitmask will not |
|
111
|
|
|
* be analyzed, so the getters for them will |
|
112
|
|
|
* throw an UnavailableException. |
|
113
|
|
|
* @param PathInfoFactory $pathInfoFactory Factory for path information value objects. |
|
114
|
|
|
* @param SizeInfoFactory $sizeInfoFactory Factory for size information value objects. |
|
115
|
|
|
* @param DateInfoFactory $dateInfoFactory Factory for date information value objects. |
|
116
|
|
|
* @param PermissionInfoFactory $permissionInfoFactory Factory for permission information value objects. |
|
117
|
|
|
* @param TypeInfoFactory $typeInfoFactory Factory for type information value objects. |
|
118
|
|
|
* |
|
119
|
|
|
* @throws FileUnreadableException |
|
120
|
|
|
*/ |
|
121
|
14 |
|
public function __construct( |
|
122
|
|
|
$file, |
|
123
|
|
|
$infoParts = self::INFO_ALL, |
|
124
|
|
|
PathInfoFactory $pathInfoFactory = null, |
|
125
|
|
|
SizeInfoFactory $sizeInfoFactory = null, |
|
126
|
|
|
DateInfoFactory $dateInfoFactory = null, |
|
127
|
|
|
PermissionInfoFactory $permissionInfoFactory = null, |
|
128
|
|
|
TypeInfoFactory $typeInfoFactory = null |
|
129
|
|
|
) { |
|
130
|
14 |
|
$this->file = $file; |
|
131
|
14 |
|
$this->infoParts = $infoParts; |
|
132
|
|
|
|
|
133
|
14 |
|
$this->pathInfoFactory = $pathInfoFactory ?: new PathInfoFactory(); |
|
134
|
14 |
|
$this->sizeInfoFactory = $sizeInfoFactory ?: new SizeInfoFactory(); |
|
135
|
14 |
|
$this->dateInfoFactory = $dateInfoFactory ?: new DateInfoFactory(); |
|
136
|
14 |
|
$this->permissionInfoFactory = $permissionInfoFactory ?: new PermissionInfoFactory(); |
|
137
|
14 |
|
$this->typeInfoFactory = $typeInfoFactory ?: new TypeInfoFactory(); |
|
138
|
|
|
|
|
139
|
14 |
|
$this->reloadFileInformation(); |
|
140
|
13 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Reloads the file infos. |
|
144
|
|
|
* |
|
145
|
|
|
* @param int $infoParts During reload, different or more info parts can be retrieved. |
|
146
|
|
|
* If $infoParts is null, the previous value is used. |
|
147
|
|
|
* |
|
148
|
|
|
* @return self |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function reload($infoParts = null) |
|
151
|
|
|
{ |
|
152
|
1 |
|
$this->reloadFileInformation($infoParts); |
|
153
|
|
|
|
|
154
|
1 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns the path information. |
|
159
|
|
|
* |
|
160
|
|
|
* @return PathInfo |
|
161
|
|
|
* |
|
162
|
|
|
* @throws UnavailableException |
|
163
|
|
|
*/ |
|
164
|
5 |
|
public function getPathInfo() |
|
165
|
|
|
{ |
|
166
|
5 |
|
if (null === $this->pathInfo) { |
|
167
|
2 |
|
throw new UnavailableException('Path information is not available!'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
3 |
|
return $this->pathInfo; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Alias for getPathInfo(). |
|
175
|
|
|
*/ |
|
176
|
1 |
|
public function path() |
|
177
|
|
|
{ |
|
178
|
1 |
|
return $this->getPathInfo(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Returns the size information. |
|
183
|
|
|
* |
|
184
|
|
|
* @return SizeInfo |
|
185
|
|
|
* |
|
186
|
|
|
* @throws UnavailableException |
|
187
|
|
|
*/ |
|
188
|
2 |
|
public function getSizeInfo() |
|
189
|
|
|
{ |
|
190
|
2 |
|
if (null === $this->sizeInfo) { |
|
191
|
1 |
|
throw new UnavailableException('Size information is not available!'); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
1 |
|
return $this->sizeInfo; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Alias for getSizeInfo(). |
|
199
|
|
|
*/ |
|
200
|
1 |
|
public function size() |
|
201
|
|
|
{ |
|
202
|
1 |
|
return $this->getSizeInfo(); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Returns the size information. |
|
207
|
|
|
* |
|
208
|
|
|
* @return DateInfo |
|
209
|
|
|
* |
|
210
|
|
|
* @throws UnavailableException |
|
211
|
|
|
*/ |
|
212
|
2 |
|
public function getDateInfo() |
|
213
|
|
|
{ |
|
214
|
2 |
|
if (null === $this->dateInfo) { |
|
215
|
1 |
|
throw new UnavailableException('Date information is not available!'); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
1 |
|
return $this->dateInfo; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Alias for getDateInfo(). |
|
223
|
|
|
*/ |
|
224
|
1 |
|
public function date() |
|
225
|
|
|
{ |
|
226
|
1 |
|
return $this->getDateInfo(); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Returns the permission information. |
|
231
|
|
|
* |
|
232
|
|
|
* @return PermissionInfo |
|
233
|
|
|
* |
|
234
|
|
|
* @throws UnavailableException |
|
235
|
|
|
*/ |
|
236
|
2 |
|
public function getPermissionInfo() |
|
237
|
|
|
{ |
|
238
|
2 |
|
if (null === $this->permissionInfo) { |
|
239
|
1 |
|
throw new UnavailableException('Permission information is not available!'); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
1 |
|
return $this->permissionInfo; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Alias for getPermissionInfo(). |
|
247
|
|
|
*/ |
|
248
|
1 |
|
public function perm() |
|
249
|
|
|
{ |
|
250
|
1 |
|
return $this->getPermissionInfo(); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Returns the type information. |
|
255
|
|
|
* |
|
256
|
|
|
* @return TypeInfo |
|
257
|
|
|
* |
|
258
|
|
|
* @throws UnavailableException |
|
259
|
|
|
*/ |
|
260
|
2 |
|
public function getTypeInfo() |
|
261
|
|
|
{ |
|
262
|
2 |
|
if (null === $this->typeInfo) { |
|
263
|
1 |
|
throw new UnavailableException('Type information is not available!'); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
1 |
|
return $this->typeInfo; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Alias for getTypeInfo(). |
|
271
|
|
|
*/ |
|
272
|
1 |
|
public function type() |
|
273
|
|
|
{ |
|
274
|
1 |
|
return $this->getTypeInfo(); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Reloads the file infos. |
|
279
|
|
|
* |
|
280
|
|
|
* @param int $infoParts |
|
281
|
|
|
* |
|
282
|
|
|
* @throws FileUnreadableException |
|
283
|
|
|
*/ |
|
284
|
14 |
|
private function reloadFileInformation($infoParts = null) |
|
285
|
|
|
{ |
|
286
|
14 |
|
if (null !== $infoParts) { |
|
287
|
1 |
|
$this->infoParts = $infoParts; |
|
288
|
1 |
|
} |
|
289
|
|
|
|
|
290
|
14 |
|
clearstatcache(true, $this->file); |
|
291
|
|
|
|
|
292
|
14 |
|
if (!is_readable($this->file)) { |
|
293
|
1 |
|
throw new FileUnreadableException(sprintf('File "%s" can not be read!', $this->file)); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
13 |
|
$fileInfo = new SplFileInfo($this->file); |
|
297
|
|
|
|
|
298
|
13 |
|
if ($this->infoParts & self::INFO_PATH) { |
|
299
|
3 |
|
$this->pathInfo = $this->pathInfoFactory->create($fileInfo); |
|
300
|
3 |
|
} |
|
301
|
|
|
|
|
302
|
13 |
|
if ($this->infoParts & self::INFO_SIZE) { |
|
303
|
2 |
|
$this->sizeInfo = $this->sizeInfoFactory->create($fileInfo); |
|
304
|
2 |
|
} |
|
305
|
|
|
|
|
306
|
13 |
|
if ($this->infoParts & self::INFO_DATE) { |
|
307
|
2 |
|
$this->dateInfo = $this->dateInfoFactory->create($fileInfo); |
|
308
|
2 |
|
} |
|
309
|
|
|
|
|
310
|
13 |
|
if ($this->infoParts & self::INFO_PERMISSION) { |
|
311
|
2 |
|
$this->permissionInfo = $this->permissionInfoFactory->create($fileInfo); |
|
312
|
2 |
|
} |
|
313
|
|
|
|
|
314
|
13 |
|
if ($this->infoParts & self::INFO_TYPE) { |
|
315
|
2 |
|
$this->typeInfo = $this->typeInfoFactory->create($fileInfo); |
|
316
|
2 |
|
} |
|
317
|
13 |
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|