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