1
|
|
|
<?php |
2
|
|
|
namespace WebStream\IO; |
3
|
|
|
|
4
|
|
|
use WebStream\Exception\Extend\IOException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* File |
8
|
|
|
* 状態を表さないもの(ファイルパス等)はキャッシュし、 |
9
|
|
|
* 状態を表すもの(存在チェク、ファイル種別、権限等)はキャッシュクリアし都度取得する |
10
|
|
|
* @author Ryuichi TANAKA. |
11
|
|
|
* @since 2016/02/05 |
12
|
|
|
* @version 0.7 |
13
|
|
|
*/ |
14
|
|
|
class File |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string ファイルパス |
18
|
|
|
*/ |
19
|
|
|
private $filePath; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string ファイル名 |
23
|
|
|
*/ |
24
|
|
|
private $fileName; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string ファイル拡張子 |
28
|
|
|
*/ |
29
|
|
|
private $fileExt; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* constructor |
33
|
|
|
* @param string $filepath ファイルパス |
|
|
|
|
34
|
|
|
*/ |
35
|
|
|
public function __construct(string $filePath) |
36
|
|
|
{ |
37
|
|
|
// realpathを含めてキャッシュクリア |
38
|
|
|
clearstatcache(true); |
39
|
|
|
|
40
|
|
|
$realPath = realpath($filePath); |
41
|
|
|
$this->filePath = $realPath !== false ? $realPath : $filePath; |
42
|
|
|
$this->fileName = basename($this->filePath); |
43
|
|
|
$this->fileExt = pathinfo($this->filePath, PATHINFO_EXTENSION); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* ファイル名を返却する |
48
|
|
|
* @return string ファイル名 |
49
|
|
|
*/ |
50
|
|
|
public function getFileName() |
51
|
|
|
{ |
52
|
|
|
return $this->fileName; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* ファイル拡張子を返却する |
57
|
|
|
* @return string ファイル拡張子 |
58
|
|
|
*/ |
59
|
|
|
public function getFileExtension() |
60
|
|
|
{ |
61
|
|
|
return $this->fileExt; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* ファイルパスを返却する |
66
|
|
|
* シンボリックリンクの場合、シンボリックリンクファイルパスを返却する |
67
|
|
|
* @return string ファイルパス |
68
|
|
|
*/ |
69
|
|
|
public function getFilePath() |
70
|
|
|
{ |
71
|
|
|
return $this->filePath; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* ファイルパスを返却する |
76
|
|
|
* シンボリックリンクの場合、実ファイルパスを返却する |
77
|
|
|
* @throws IOException |
78
|
|
|
* @return string ファイルパス |
79
|
|
|
*/ |
80
|
|
|
public function getAbsoluteFilePath() |
81
|
|
|
{ |
82
|
|
|
if ($this->isLink()) { |
83
|
|
|
$filePath = $this->filePath; |
84
|
|
|
while (@is_link($filePath)) { |
85
|
|
|
$linkPath = readlink($filePath); |
86
|
|
|
if ($linkPath === false) { |
87
|
|
|
throw new IOException("Symbolic link read error: " . $filePath); |
88
|
|
|
} |
89
|
|
|
$filePath = $linkPath; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $filePath; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->getFilePath(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* 読み込み権限があるか返却する |
100
|
|
|
* @return bool ファイルが存在し、読み込み権限があればtrue |
101
|
|
|
*/ |
102
|
|
|
public function isReadable() |
103
|
|
|
{ |
104
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
105
|
|
|
clearstatcache(); |
106
|
|
|
|
107
|
|
|
return @is_readable($this->filePath); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* 書き込み権限があるか返却する |
112
|
|
|
* @return bool ファイルが存在し、書き込み権限があればtrue |
113
|
|
|
*/ |
114
|
|
|
public function isWritable() |
115
|
|
|
{ |
116
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
117
|
|
|
clearstatcache(); |
118
|
|
|
|
119
|
|
|
return @is_writable($this->filePath); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* 実行権限があるか返却する |
124
|
|
|
* @return bool ファイルが存在し、実行権限があればtrue |
125
|
|
|
*/ |
126
|
|
|
public function isExecutable() |
127
|
|
|
{ |
128
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
129
|
|
|
clearstatcache(); |
130
|
|
|
|
131
|
|
|
return @is_executable($this->filePath); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* ファイルかどうか |
136
|
|
|
* @return bool ファイルならtrue |
137
|
|
|
*/ |
138
|
|
|
public function isFile() |
139
|
|
|
{ |
140
|
|
|
clearstatcache(); |
141
|
|
|
|
142
|
|
|
return is_file($this->filePath); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* ディレクトリかどうか |
147
|
|
|
* @return bool ディレクトリならtrue |
148
|
|
|
*/ |
149
|
|
|
public function isDirectory() |
150
|
|
|
{ |
151
|
|
|
clearstatcache(); |
152
|
|
|
|
153
|
|
|
return is_dir($this->filePath); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* リンクかどうか |
158
|
|
|
* @return bool リンクならtrue |
159
|
|
|
*/ |
160
|
|
|
public function isLink() |
161
|
|
|
{ |
162
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
163
|
|
|
clearstatcache(); |
164
|
|
|
|
165
|
|
|
return @is_link($this->filePath); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* ファイルサイズを返却する |
170
|
|
|
* ファイルが存在しなければ0 |
171
|
|
|
* @return int ファイルサイズ |
172
|
|
|
*/ |
173
|
|
|
public function length() |
174
|
|
|
{ |
175
|
|
|
$length = 0; |
176
|
|
|
|
177
|
|
|
if ($this->exists()) { |
178
|
|
|
$filePath = $this->getAbsoluteFilePath(); |
179
|
|
|
$length = filesize($filePath); |
180
|
|
|
|
181
|
|
|
if ($length === false) { |
182
|
|
|
throw new IOException("Cannot get filesize of " . $filePath); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $length; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* ファイル(ディレクトリ、リンク含む)が存在するか |
191
|
|
|
* @return bool 存在すればtrue |
192
|
|
|
*/ |
193
|
|
|
public function exists() |
194
|
|
|
{ |
195
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
196
|
|
|
clearstatcache(); |
197
|
|
|
|
198
|
|
|
return $this->isLink() || $this->isDirectory() || $this->isFile(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* ファイルを削除する |
203
|
|
|
* @return bool 削除結果 |
204
|
|
|
*/ |
205
|
|
|
public function delete() |
206
|
|
|
{ |
207
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
208
|
|
|
clearstatcache(); |
209
|
|
|
|
210
|
|
|
$isDeleted = false; |
211
|
|
|
if ($this->isWritable()) { |
212
|
|
|
if ($this->isDirectory()) { |
213
|
|
|
$isDeleted = rmdir($this->filePath); |
214
|
|
|
} else { |
215
|
|
|
$isDeleted = unlink($this->filePath); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $isDeleted; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* ファイルをリネームする |
224
|
|
|
* @param string $destPath 変更後ファイル名 |
225
|
|
|
* @return bool リネーム結果 |
226
|
|
|
*/ |
227
|
|
|
public function renameTo($destPath) |
228
|
|
|
{ |
229
|
|
|
$dirname = dirname($destPath); |
230
|
|
|
$dir = new File($dirname); |
231
|
|
|
if (!$dir->isWritable()) { |
232
|
|
|
throw new IOException("Cannot writable: " . $destPath); |
233
|
|
|
} |
234
|
|
|
$dirPath = $dir->getFilePath(); |
235
|
|
|
$absDestPath = $dirPath . "/" . basename($destPath); |
236
|
|
|
|
237
|
|
|
return rename($this->filePath, $absDestPath); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* ファイルサイズを返却する |
242
|
|
|
* @return int ファイルサイズ |
243
|
|
|
*/ |
244
|
|
|
public function size() |
245
|
|
|
{ |
246
|
|
|
return $this->exists() ? filesize($this->filePath) : 0; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.