1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This program is free software. It comes without any warranty, to |
4
|
|
|
* the extent permitted by applicable law. You can redistribute it |
5
|
|
|
* and/or modify it under the terms of the Do What The Fuck You Want |
6
|
|
|
* To Public License, Version 2, as published by Sam Hocevar. See |
7
|
|
|
* http://www.wtfpl.net/ for more details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types = 1); |
11
|
|
|
|
12
|
|
|
namespace hanneskod\classtools\Iterator; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Finder\SplFileInfo as FinderSplFileInfo; |
15
|
|
|
use hanneskod\classtools\Transformer\Reader; |
16
|
|
|
use hanneskod\classtools\Exception\ReaderException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Decorates \Symfony\Component\Finder\SplFileInfo to support the creation of Readers |
20
|
|
|
* |
21
|
|
|
* @author Hannes Forsgård <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class SplFileInfo extends FinderSplFileInfo |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var FinderSplFileInfo |
27
|
|
|
*/ |
28
|
|
|
private $decorated; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Reader |
32
|
|
|
*/ |
33
|
|
|
private $reader; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Load decorated object |
37
|
|
|
*/ |
38
|
|
|
public function __construct(FinderSplFileInfo $decorated) |
39
|
|
|
{ |
40
|
|
|
$this->decorated = $decorated; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get reader for the contents of this file |
45
|
|
|
* |
46
|
|
|
* @throws ReaderException If file contains syntax errors |
47
|
|
|
*/ |
48
|
|
|
public function getReader(): Reader |
49
|
|
|
{ |
50
|
|
|
if (!isset($this->reader)) { |
51
|
|
|
try { |
52
|
|
|
$this->reader = new Reader((string)$this->getContents()); |
53
|
|
|
} catch (ReaderException $exception) { |
54
|
|
|
throw new ReaderException($exception->getMessage() . ' in ' . $this->getPathname()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->reader; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the relative path |
63
|
|
|
*/ |
64
|
|
|
public function getRelativePath(): string |
65
|
|
|
{ |
66
|
|
|
return $this->decorated->getRelativePath(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the relative path name |
71
|
|
|
*/ |
72
|
|
|
public function getRelativePathname(): string |
73
|
|
|
{ |
74
|
|
|
return $this->decorated->getRelativePathname(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the contents of the file |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getContents() |
83
|
|
|
{ |
84
|
|
|
return (string)$this->decorated->getContents(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Gets the last access time for the file |
89
|
|
|
*/ |
90
|
|
|
public function getATime(): int |
91
|
|
|
{ |
92
|
|
|
return $this->decorated->getATime(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the base name of the file, directory, or link without path info |
97
|
|
|
*/ |
98
|
|
|
public function getBasename($suffix = ''): string |
99
|
|
|
{ |
100
|
|
|
return $this->decorated->getBasename($suffix); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the inode change time for the file |
105
|
|
|
*/ |
106
|
|
|
public function getCTime(): int |
107
|
|
|
{ |
108
|
|
|
return $this->decorated->getCTime(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Retrieves the file extension |
113
|
|
|
*/ |
114
|
|
|
public function getExtension(): string |
115
|
|
|
{ |
116
|
|
|
return $this->decorated->getExtension(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Gets an SplFileInfo object for the referenced file |
121
|
|
|
* |
122
|
|
|
* @param string $class_name |
123
|
|
|
*/ |
124
|
|
|
public function getFileInfo($class_name = ''): \SplFileInfo |
125
|
|
|
{ |
126
|
|
|
return $this->decorated->getFileInfo($class_name); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Gets the filename without any path information |
131
|
|
|
*/ |
132
|
|
|
public function getFilename(): string |
133
|
|
|
{ |
134
|
|
|
return $this->decorated->getFilename(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Gets the file group |
139
|
|
|
*/ |
140
|
|
|
public function getGroup(): int |
141
|
|
|
{ |
142
|
|
|
return $this->decorated->getGroup(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Gets the inode number for the filesystem object |
147
|
|
|
*/ |
148
|
|
|
public function getInode(): int |
149
|
|
|
{ |
150
|
|
|
return $this->decorated->getInode(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Gets the target of a filesystem link |
155
|
|
|
*/ |
156
|
|
|
public function getLinkTarget(): sring |
157
|
|
|
{ |
158
|
|
|
return $this->decorated->getLinkTarget(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the time when the contents of the file were changed |
163
|
|
|
*/ |
164
|
|
|
public function getMTime(): int |
165
|
|
|
{ |
166
|
|
|
return $this->decorated->getMTime(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Gets the file owner |
171
|
|
|
*/ |
172
|
|
|
public function getOwner(): int |
173
|
|
|
{ |
174
|
|
|
return $this->decorated->getOwner(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns the path to the file, omitting the filename and any trailing slash |
179
|
|
|
*/ |
180
|
|
|
public function getPath(): string |
181
|
|
|
{ |
182
|
|
|
return $this->decorated->getPath(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Gets an SplFileInfo object for the parent of the current file |
187
|
|
|
* |
188
|
|
|
* @param string $class_name |
189
|
|
|
*/ |
190
|
|
|
public function getPathInfo($class_name = ''): \SplFileInfo |
191
|
|
|
{ |
192
|
|
|
return $this->decorated->getPathInfo($class_name); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns the path to the file |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function getPathname() |
201
|
|
|
{ |
202
|
|
|
return $this->decorated->getPathname(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Gets the file permissions for the file |
207
|
|
|
*/ |
208
|
|
|
public function getPerms(): int |
209
|
|
|
{ |
210
|
|
|
return $this->decorated->getPerms(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Expands all symbolic links and resolves relative references |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
|
|
public function getRealPath() |
219
|
|
|
{ |
220
|
|
|
return $this->decorated->getRealPath(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Returns the filesize in bytes for the file referenced |
225
|
|
|
*/ |
226
|
|
|
public function getSize(): int |
227
|
|
|
{ |
228
|
|
|
return $this->decorated->getSize(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns the type of the file referenced |
233
|
|
|
*/ |
234
|
|
|
public function getType(): string |
235
|
|
|
{ |
236
|
|
|
return $this->decorated->getType(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* This method can be used to determine if the file is a directory |
241
|
|
|
*/ |
242
|
|
|
public function isDir(): bool |
243
|
|
|
{ |
244
|
|
|
return $this->decorated->isDir(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Checks if the file is executable |
249
|
|
|
*/ |
250
|
|
|
public function isExecutable(): bool |
251
|
|
|
{ |
252
|
|
|
return $this->decorated->isExecutable(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Checks if the file referenced exists and is a regular file |
257
|
|
|
*/ |
258
|
|
|
public function isFile(): bool |
259
|
|
|
{ |
260
|
|
|
return $this->decorated->isFile(); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Check if the file referenced is a link |
265
|
|
|
*/ |
266
|
|
|
public function isLink(): bool |
267
|
|
|
{ |
268
|
|
|
return $this->decorated->isLink(); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Check if the file is readable |
273
|
|
|
*/ |
274
|
|
|
public function isReadable(): bool |
275
|
|
|
{ |
276
|
|
|
return $this->decorated->isReadable(); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Check if the file is writable |
281
|
|
|
*/ |
282
|
|
|
public function isWritable(): bool |
283
|
|
|
{ |
284
|
|
|
return $this->decorated->isWritable(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Creates an SplFileObject object of the file |
289
|
|
|
* |
290
|
|
|
* @param string $open_mode |
291
|
|
|
* @param boolean $use_include_path |
292
|
|
|
* @param resource $context |
293
|
|
|
* @return \SplFileObject |
294
|
|
|
*/ |
295
|
|
|
public function openFile($open_mode = "r", $use_include_path = false, $context = null) |
296
|
|
|
{ |
297
|
|
|
return $this->decorated->openFile($open_mode, $use_include_path, $context); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Set the class name which will be used to open files when openFile() is called |
302
|
|
|
* |
303
|
|
|
* @param string $class_name |
304
|
|
|
* @return void |
305
|
|
|
*/ |
306
|
|
|
public function setFileClass($class_name = '') |
307
|
|
|
{ |
308
|
|
|
$this->decorated->setFileClass($class_name); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Set the class name which will be used when getFileInfo and getPathInfo are called |
313
|
|
|
* |
314
|
|
|
* @param string $class_name |
315
|
|
|
* @return void |
316
|
|
|
*/ |
317
|
|
|
public function setInfoClass($class_name = '') |
318
|
|
|
{ |
319
|
|
|
$this->decorated->setInfoClass($class_name); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* This method will return the file name of the referenced file |
324
|
|
|
* |
325
|
|
|
* @return string |
326
|
|
|
*/ |
327
|
|
|
public function __tostring() |
328
|
|
|
{ |
329
|
|
|
return (string)$this->decorated; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|