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
|
|
|
$this->filePath = $filePath; |
41
|
|
|
$this->fileName = basename($this->filePath); |
42
|
|
|
$this->fileExt = pathinfo($this->filePath, PATHINFO_EXTENSION); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* ファイル名を返却する |
47
|
|
|
* @return string ファイル名 |
48
|
|
|
*/ |
49
|
|
|
public function getFileName() |
50
|
|
|
{ |
51
|
|
|
return $this->fileName; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* ファイル拡張子を返却する |
56
|
|
|
* @return string ファイル拡張子 |
57
|
|
|
*/ |
58
|
|
|
public function getFileExtension() |
59
|
|
|
{ |
60
|
|
|
return $this->fileExt; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* ファイルパスを返却する |
65
|
|
|
* シンボリックリンクの場合、シンボリックリンクファイルパスを返却する |
66
|
|
|
* @return string ファイルパス |
67
|
|
|
*/ |
68
|
|
|
public function getFilePath() |
69
|
|
|
{ |
70
|
|
|
return $this->filePath; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* ファイルパスを返却する |
75
|
|
|
* シンボリックリンクの場合、実ファイルパスを返却する |
76
|
|
|
* @throws IOException |
77
|
|
|
* @return string ファイルパス |
78
|
|
|
*/ |
79
|
|
|
public function getAbsoluteFilePath() |
80
|
|
|
{ |
81
|
|
|
if ($this->isLink()) { |
82
|
|
|
$filePath = $this->filePath; |
83
|
|
|
while (@is_link($filePath)) { |
84
|
|
|
$linkPath = readlink($filePath); |
85
|
|
|
if ($linkPath === false) { |
86
|
|
|
throw new IOException("Symbolic link read error: " . $filePath); |
87
|
|
|
} |
88
|
|
|
$filePath = $linkPath; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $filePath; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->getFilePath(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* 読み込み権限があるか返却する |
99
|
|
|
* @return bool ファイルが存在し、読み込み権限があればtrue |
100
|
|
|
*/ |
101
|
|
|
public function isReadable() |
102
|
|
|
{ |
103
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
104
|
|
|
clearstatcache(); |
105
|
|
|
|
106
|
|
|
return @is_readable($this->filePath); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* 書き込み権限があるか返却する |
111
|
|
|
* @return bool ファイルが存在し、書き込み権限があればtrue |
112
|
|
|
*/ |
113
|
|
|
public function isWritable() |
114
|
|
|
{ |
115
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
116
|
|
|
clearstatcache(); |
117
|
|
|
|
118
|
|
|
return @is_writable($this->filePath); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* 実行権限があるか返却する |
123
|
|
|
* @return bool ファイルが存在し、実行権限があればtrue |
124
|
|
|
*/ |
125
|
|
|
public function isExecutable() |
126
|
|
|
{ |
127
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
128
|
|
|
clearstatcache(); |
129
|
|
|
|
130
|
|
|
return @is_executable($this->filePath); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* ファイルかどうか |
135
|
|
|
* @return bool ファイルならtrue |
136
|
|
|
*/ |
137
|
|
|
public function isFile() |
138
|
|
|
{ |
139
|
|
|
clearstatcache(); |
140
|
|
|
|
141
|
|
|
return is_file($this->filePath); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* ディレクトリかどうか |
146
|
|
|
* @return bool ディレクトリならtrue |
147
|
|
|
*/ |
148
|
|
|
public function isDirectory() |
149
|
|
|
{ |
150
|
|
|
clearstatcache(); |
151
|
|
|
|
152
|
|
|
return is_dir($this->filePath); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* リンクかどうか |
157
|
|
|
* @return bool リンクならtrue |
158
|
|
|
*/ |
159
|
|
|
public function isLink() |
160
|
|
|
{ |
161
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
162
|
|
|
clearstatcache(); |
163
|
|
|
|
164
|
|
|
return @is_link($this->filePath); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* ファイルサイズを返却する |
169
|
|
|
* ファイルが存在しなければ0 |
170
|
|
|
* @return int ファイルサイズ |
171
|
|
|
*/ |
172
|
|
|
public function length() |
173
|
|
|
{ |
174
|
|
|
$length = 0; |
175
|
|
|
|
176
|
|
|
if ($this->exists()) { |
177
|
|
|
$filePath = $this->getAbsoluteFilePath(); |
178
|
|
|
$length = filesize($filePath); |
179
|
|
|
|
180
|
|
|
if ($length === false) { |
181
|
|
|
throw new IOException("Cannot get filesize of " . $filePath); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $length; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* ファイル(ディレクトリ、リンク含む)が存在するか |
190
|
|
|
* @return bool 存在すればtrue |
191
|
|
|
*/ |
192
|
|
|
public function exists() |
193
|
|
|
{ |
194
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
195
|
|
|
clearstatcache(); |
196
|
|
|
|
197
|
|
|
return $this->isLink() || $this->isDirectory() || $this->isFile(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* ファイルを削除する |
202
|
|
|
* @return bool 削除結果 |
203
|
|
|
*/ |
204
|
|
|
public function delete() |
205
|
|
|
{ |
206
|
|
|
// Fileオブジェクト作成後に属性が変わることを考慮しキャッシュクリアする |
207
|
|
|
clearstatcache(); |
208
|
|
|
|
209
|
|
|
$isDeleted = false; |
210
|
|
|
if ($this->isWritable()) { |
211
|
|
|
if ($this->isDirectory()) { |
212
|
|
|
$isDeleted = rmdir($this->filePath); |
213
|
|
|
} else { |
214
|
|
|
$isDeleted = unlink($this->filePath); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $isDeleted; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* ファイルをリネームする |
223
|
|
|
* @param string $destPath 変更後ファイル名 |
224
|
|
|
* @return bool リネーム結果 |
225
|
|
|
*/ |
226
|
|
|
public function renameTo($destPath) |
227
|
|
|
{ |
228
|
|
|
$dirname = dirname($destPath); |
229
|
|
|
$dir = new File($dirname); |
230
|
|
|
if (!$dir->isWritable()) { |
231
|
|
|
throw new IOException("Cannot writable: " . $destPath); |
232
|
|
|
} |
233
|
|
|
$dirPath = $dir->getFilePath(); |
234
|
|
|
$absDestPath = $dirPath . "/" . basename($destPath); |
235
|
|
|
|
236
|
|
|
return rename($this->filePath, $absDestPath); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* ファイル更新日時を返却する |
241
|
|
|
* @return int ファイル更新日時 |
242
|
|
|
*/ |
243
|
|
|
public function lastModified() |
244
|
|
|
{ |
245
|
|
|
return $this->exists() ? filemtime($this->filePath) : 0; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
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.