icy2003 /
php
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * abstract Class Base |
||
| 4 | * |
||
| 5 | * @link https://www.icy2003.com/ |
||
| 6 | * @author icy2003 <[email protected]> |
||
| 7 | * @copyright Copyright (c) 2017, icy2003 |
||
| 8 | */ |
||
| 9 | namespace icy2003\php\icomponents\file; |
||
| 10 | |||
| 11 | use icy2003\php\ihelpers\Arrays; |
||
| 12 | use icy2003\php\ihelpers\Charset; |
||
| 13 | use icy2003\php\ihelpers\Strings; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * 文件抽象类 |
||
| 17 | */ |
||
| 18 | abstract class Base |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * 获得命令返回值 |
||
| 22 | * |
||
| 23 | * - 不要依赖这个,一些环境不一定支持 |
||
| 24 | * |
||
| 25 | * @param string $command 命令 |
||
| 26 | * |
||
| 27 | * @return string|false |
||
| 28 | */ |
||
| 29 | abstract public function getCommandResult($command); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @ignore |
||
| 33 | */ |
||
| 34 | 2 | public function getBasename($path, $suffix = null) |
|
| 35 | { |
||
| 36 | 2 | $path = str_replace('\\', '/', $path); |
|
| 37 | 2 | return basename($path, $suffix); |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @ignore |
||
| 42 | */ |
||
| 43 | 5 | public function getDirname($path) |
|
| 44 | { |
||
| 45 | 5 | $path = str_replace('\\', '/', $path); |
|
| 46 | 5 | return dirname($path); |
|
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @ignore |
||
| 51 | */ |
||
| 52 | abstract public function isFile($file); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @ignore |
||
| 56 | */ |
||
| 57 | abstract public function isDir($dir); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * 返回规范化的绝对路径名 |
||
| 61 | * |
||
| 62 | * - 不支持 realpath 的类将使用这个,其他的由子类实现 |
||
| 63 | * - 该函数实现是:处理输入的 path 中的 '/./', '/../' 以及多余的 '/' |
||
| 64 | * |
||
| 65 | * @param string $path 要检查的路径 |
||
| 66 | * |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | 7 | public function getRealpath($path) |
|
| 70 | { |
||
| 71 | 7 | if (Strings::isStartsWith($path, 'http')) { |
|
| 72 | 2 | return $path; |
|
| 73 | } |
||
| 74 | 6 | $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); |
|
| 75 | 6 | $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); |
|
| 76 | 6 | $absolutes = []; |
|
| 77 | 6 | foreach ($parts as $part) { |
|
| 78 | 6 | if ('.' == $part) { |
|
| 79 | continue; |
||
| 80 | } |
||
| 81 | |||
| 82 | 6 | if ('..' == $part) { |
|
| 83 | 5 | array_pop($absolutes); |
|
| 84 | } else { |
||
| 85 | 6 | $absolutes[] = $part; |
|
| 86 | } |
||
| 87 | } |
||
| 88 | 6 | return implode(DIRECTORY_SEPARATOR, $absolutes); |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * 列出指定路径中的文件和目录 |
||
| 93 | * |
||
| 94 | * - 优先返回叶节点 |
||
| 95 | * - 不包含“.”和“..” |
||
| 96 | * |
||
| 97 | * @param string $dir 目录的路径,如不给,则使用当前路径(pwd) |
||
| 98 | * @param integer $flags 选项设置,默认 FileConstants::COMPLETE_PATH,可支持参数: |
||
| 99 | * - FileConstants::COMPLETE_PATH:使用完整路径 |
||
| 100 | * - FileConstants::COMPLETE_PATH_DISABLED:不使用完整路径 |
||
| 101 | * - FileConstants::RECURSIVE:递归遍历 |
||
| 102 | * - FileConstants::RECURSIVE_DISABLED:不递归 |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | abstract public function getLists($dir = null, $flags = FileConstants::COMPLETE_PATH); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * 取得文件大小 |
||
| 110 | * |
||
| 111 | * @param string $file 文件的路径 |
||
| 112 | * |
||
| 113 | * @return integer |
||
| 114 | */ |
||
| 115 | abstract public function getFilesize($file); |
||
| 116 | |||
| 117 | /** |
||
| 118 | * 将整个文件读入一个字符串 |
||
| 119 | * |
||
| 120 | * @param string $file 要读取的文件的名称 |
||
| 121 | * |
||
| 122 | * @return string|false |
||
| 123 | */ |
||
| 124 | abstract public function getFileContent($file); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * 创建一个文件(目录会被递归地创建),并用字符串(资源)填充进文件 |
||
| 128 | * |
||
| 129 | * - 可将字符串、资源、数组写入文件,行为和 file_put_contents 一样 |
||
| 130 | * - 资源:`$fp = fopen('https://www.icy2003.com', 'r');` |
||
| 131 | * |
||
| 132 | * @link https://www.php.net/manual/zh/function.file-put-contents.php |
||
| 133 | * |
||
| 134 | * @param string $file 文件的路径 |
||
| 135 | * @param string|array $string 待填充进文件的字符串(资源) |
||
| 136 | * @param integer $mode 默认的 mode 是 0777,意味着最大可能的访问权 |
||
| 137 | * |
||
| 138 | * @return boolean |
||
| 139 | */ |
||
| 140 | abstract public function putFileContent($file, $string, $mode = 0777); |
||
| 141 | |||
| 142 | /** |
||
| 143 | * 递归地创建目录 |
||
| 144 | * |
||
| 145 | * @param string $dir 目录的路径 |
||
| 146 | * @param integer $mode 默认的 mode 是 0777,意味着最大可能的访问权 |
||
| 147 | * |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | 4 | public function createDir($dir, $mode = 0777) |
|
| 151 | { |
||
| 152 | 4 | if ($this->isDir($dir)) { |
|
| 153 | 4 | return true; |
|
| 154 | } |
||
| 155 | 1 | $this->createDir($this->getDirname($dir), $mode); |
|
| 156 | 1 | return $this->_mkdir($dir, $mode); |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * 删除一个文件 |
||
| 161 | * |
||
| 162 | * @param string $file 文件的路径 |
||
| 163 | * |
||
| 164 | * @return boolean |
||
| 165 | */ |
||
| 166 | abstract public function deleteFile($file); |
||
| 167 | |||
| 168 | /** |
||
| 169 | * 递归地删除目录 |
||
| 170 | * |
||
| 171 | * @param string $dir 目录的路径 |
||
| 172 | * @param boolean $deleteRoot 是否删除目录的根节点,默认 true,即删除 |
||
| 173 | * |
||
| 174 | * @return boolean |
||
| 175 | */ |
||
| 176 | 1 | public function deleteDir($dir, $deleteRoot = true) |
|
| 177 | { |
||
| 178 | 1 | if (false === $this->isDir($dir)) { |
|
| 179 | return true; |
||
| 180 | } |
||
| 181 | 1 | $files = $this->getLists($dir, FileConstants::COMPLETE_PATH | FileConstants::RECURSIVE); |
|
| 182 | 1 | foreach ($files as $file) { |
|
| 183 | 1 | $this->isDir($file) ? $this->deleteDir($file) : $this->deleteFile($file); |
|
| 184 | } |
||
| 185 | |||
| 186 | 1 | return true === $deleteRoot ? $this->_rmdir($dir) : true; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * 复制文件(目录会被递归地创建) |
||
| 191 | * |
||
| 192 | * @param string $fromFile 文件原路径 |
||
| 193 | * @param string $toFile 文件目标路径 |
||
| 194 | * @param boolean $overwrite 已存在的文件是否被覆盖,默认 false,即不覆盖 |
||
| 195 | * |
||
| 196 | * @return boolean |
||
| 197 | */ |
||
| 198 | 3 | public function copyFile($fromFile, $toFile, $overwrite = false) |
|
| 199 | { |
||
| 200 | 3 | if (false === $this->isFile($fromFile)) { |
|
| 201 | return false; |
||
| 202 | } |
||
| 203 | 3 | if ($this->isFile($toFile)) { |
|
| 204 | if (false === $overwrite) { |
||
| 205 | return false; |
||
| 206 | } else { |
||
| 207 | $this->deleteFile($toFile); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | 3 | $this->createDir($this->getDirname($toFile)); |
|
| 211 | 3 | return $this->_copy($fromFile, $toFile); |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * 递归地复制目录 |
||
| 216 | * |
||
| 217 | * @param string $fromDir 目录原路径 |
||
| 218 | * @param string $toDir 目录目标路径 |
||
| 219 | * @param boolean $overwrite 已存在的目录是否被覆盖,默认 false,即不覆盖 |
||
| 220 | * |
||
| 221 | * @return boolean |
||
| 222 | */ |
||
| 223 | 1 | public function copyDir($fromDir, $toDir, $overwrite = false) |
|
| 224 | { |
||
| 225 | 1 | $fromDir = rtrim($fromDir, '/') . '/'; |
|
| 226 | 1 | $toDir = rtrim($toDir, '/') . '/'; |
|
| 227 | 1 | if (false === $this->isDir($fromDir)) { |
|
| 228 | return false; |
||
| 229 | } |
||
| 230 | 1 | $this->createDir($toDir); |
|
| 231 | 1 | $files = $this->getLists($fromDir, FileConstants::COMPLETE_PATH_DISABLED); |
|
| 232 | 1 | foreach ($files as $file) { |
|
| 233 | 1 | if ($this->isDir($fromDir . $file)) { |
|
| 234 | 1 | $this->copyDir($fromDir . $file, $toDir . $file, $overwrite); |
|
| 235 | } else { |
||
| 236 | 1 | $this->copyFile($fromDir . $file, $toDir . $file, $overwrite); |
|
| 237 | } |
||
| 238 | } |
||
| 239 | 1 | return true; |
|
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * 移动文件(目录会被递归地创建) |
||
| 244 | * |
||
| 245 | * @param string $fromFile 文件原路径 |
||
| 246 | * @param string $toFile 文件目标路径 |
||
| 247 | * @param boolean $overwrite 已存在的文件是否被覆盖,默认 false,即不覆盖 |
||
| 248 | * |
||
| 249 | * @return boolean |
||
| 250 | */ |
||
| 251 | 1 | public function moveFile($fromFile, $toFile, $overwrite = false) |
|
| 252 | { |
||
| 253 | 1 | if (false === $this->isFile($fromFile)) { |
|
| 254 | return false; |
||
| 255 | } |
||
| 256 | 1 | if ($this->isFile($toFile)) { |
|
| 257 | if (false === $overwrite) { |
||
| 258 | return false; |
||
| 259 | } else { |
||
| 260 | $this->deleteFile($toFile); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | 1 | $this->createDir($this->getDirname($toFile)); |
|
| 264 | 1 | return $this->_move($fromFile, $toFile); |
|
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * 递归地移动目录 |
||
| 269 | * |
||
| 270 | * @param string $fromDir 目录原路径 |
||
| 271 | * @param string $toDir 目录目标路径 |
||
| 272 | * @param boolean $overwrite 已存在的目录是否被覆盖,默认 false,即不覆盖 |
||
| 273 | * |
||
| 274 | * @return boolean |
||
| 275 | */ |
||
| 276 | public function moveDir($fromDir, $toDir, $overwrite = false) |
||
| 277 | { |
||
| 278 | $fromDir = rtrim($fromDir, '/') . '/'; |
||
| 279 | $toDir = rtrim($toDir, '/') . '/'; |
||
| 280 | if (false === $this->isDir($fromDir)) { |
||
| 281 | return false; |
||
| 282 | } |
||
| 283 | $this->createDir($toDir); |
||
| 284 | $files = $this->getLists($fromDir, FileConstants::COMPLETE_PATH_DISABLED); |
||
| 285 | foreach ($files as $file) { |
||
| 286 | if ($this->isDir($fromDir . $file)) { |
||
| 287 | $this->moveDir($fromDir . $file, $toDir . $file, $overwrite); |
||
| 288 | } else { |
||
| 289 | $this->moveFile($fromDir . $file, $toDir . $file, $overwrite); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | return $this->deleteDir($fromDir); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * 上传文件 |
||
| 297 | * |
||
| 298 | * @param string|array $fileMap @see self::fileMap() |
||
| 299 | * @param boolean $overwrite 是否覆盖,默认 true,即:是 |
||
| 300 | * |
||
| 301 | * @return boolean |
||
| 302 | */ |
||
| 303 | abstract public function uploadFile($fileMap, $overwrite = true); |
||
| 304 | |||
| 305 | /** |
||
| 306 | * 下载文件 |
||
| 307 | * |
||
| 308 | * @param string|array $fileMap @see self::fileMap() |
||
| 309 | * @param boolean $overwrite 是否覆盖,默认 true,即:是 |
||
| 310 | * |
||
| 311 | * @return boolean |
||
| 312 | */ |
||
| 313 | abstract public function downloadFile($fileMap, $overwrite = true); |
||
| 314 | |||
| 315 | /** |
||
| 316 | * 改变文件(目录)的创建者 |
||
| 317 | * |
||
| 318 | * - 不支持的类:FtpFile |
||
| 319 | * |
||
| 320 | * @param string $file 文件或者目录 |
||
| 321 | * @param string $user 创建者 |
||
| 322 | * @param integer $flags 选项设置,默认 FileConstants::RECURSIVE_DISABLED,可支持参数: |
||
| 323 | * - FileConstants::RECURSIVE_DISABLED |
||
| 324 | * - FileConstants::RECURSIVE |
||
| 325 | * |
||
| 326 | * @return boolean |
||
| 327 | */ |
||
| 328 | abstract public function chown($file, $user, $flags = FileConstants::RECURSIVE_DISABLED); |
||
| 329 | |||
| 330 | /** |
||
| 331 | * 改变文件(目录)的群组 |
||
| 332 | * |
||
| 333 | * - 不支持的类:FtpFile |
||
| 334 | * |
||
| 335 | * @param string $file 文件或者目录 |
||
| 336 | * @param string $group 群组 |
||
| 337 | * @param integer $flags 选项设置,默认 FileConstants::RECURSIVE_DISABLED,可支持参数: |
||
| 338 | * - FileConstants::RECURSIVE_DISABLED |
||
| 339 | * - FileConstants::RECURSIVE |
||
| 340 | * |
||
| 341 | * @return boolean |
||
| 342 | */ |
||
| 343 | abstract public function chgrp($file, $group, $flags = FileConstants::RECURSIVE_DISABLED); |
||
| 344 | |||
| 345 | /** |
||
| 346 | * 改变文件(目录)的安全模式 |
||
| 347 | * |
||
| 348 | * @param string $file 文件或者目录 |
||
| 349 | * @param integer $mode 默认的 mode 是 0777,意味着最大可能的访问权 |
||
| 350 | * @param integer $flags 选项设置,默认 FileConstants::RECURSIVE_DISABLED,可支持参数: |
||
| 351 | * - FileConstants::RECURSIVE_DISABLED |
||
| 352 | * - FileConstants::RECURSIVE |
||
| 353 | * @return boolean |
||
| 354 | */ |
||
| 355 | abstract public function chmod($file, $mode = 0777, $flags = FileConstants::RECURSIVE_DISABLED); |
||
| 356 | |||
| 357 | /** |
||
| 358 | * 建立符号连接 |
||
| 359 | * |
||
| 360 | * - 不支持的类:FtpFile |
||
| 361 | * |
||
| 362 | * @param string $from 连接的目标 |
||
| 363 | * @param string $to 连接的名称 |
||
| 364 | * |
||
| 365 | * @return boolean |
||
| 366 | */ |
||
| 367 | abstract public function symlink($from, $to); |
||
| 368 | |||
| 369 | /** |
||
| 370 | * 关闭文件句柄(连接) |
||
| 371 | * |
||
| 372 | * @return boolean |
||
| 373 | */ |
||
| 374 | abstract public function close(); |
||
| 375 | |||
| 376 | /** |
||
| 377 | * 非递归地复制目录(文件) |
||
| 378 | * |
||
| 379 | * @param string $fromFile 源目录(文件)的路径 |
||
| 380 | * @param string $toFile 目标目录(文件)的路径 |
||
| 381 | * |
||
| 382 | * @return boolean |
||
| 383 | */ |
||
| 384 | abstract protected function _copy($fromFile, $toFile); |
||
| 385 | |||
| 386 | /** |
||
| 387 | * 非递归地移动目录(文件) |
||
| 388 | * |
||
| 389 | * @param string $fromFile 源目录(文件)的路径 |
||
| 390 | * @param string $toFile 目标目录(文件)的路径 |
||
| 391 | * |
||
| 392 | * @return boolean |
||
| 393 | */ |
||
| 394 | abstract protected function _move($fromFile, $toFile); |
||
| 395 | |||
| 396 | /** |
||
| 397 | * 非递归地创建目录 |
||
| 398 | * |
||
| 399 | * @param string $dir 目录的路径 |
||
| 400 | * @param integer $mode 默认的 mode 是 0777,意味着最大可能的访问权 |
||
| 401 | * |
||
| 402 | * @return boolean |
||
| 403 | */ |
||
| 404 | abstract protected function _mkdir($dir, $mode = 0777); |
||
| 405 | |||
| 406 | /** |
||
| 407 | * 非递归地删除目录 |
||
| 408 | * |
||
| 409 | * @param string $dir |
||
| 410 | * |
||
| 411 | * @return boolean |
||
| 412 | */ |
||
| 413 | abstract protected function _rmdir($dir); |
||
| 414 | |||
| 415 | /** |
||
| 416 | * 析构函数:关闭文件句柄(连接) |
||
| 417 | */ |
||
| 418 | 35 | public function __destruct() |
|
| 419 | { |
||
| 420 | 35 | $this->close(); |
|
| 421 | 35 | } |
|
| 422 | |||
| 423 | /** |
||
| 424 | * 返回文件映射 |
||
| 425 | * |
||
| 426 | * - 支持别名 |
||
| 427 | * |
||
| 428 | * @param string|array $file 数组:[[带路径的文件名],[不带路径的文件名]],字符串会转成数组 |
||
| 429 | * |
||
| 430 | * @return array |
||
| 431 | */ |
||
| 432 | 1 | public function fileMap($file) |
|
| 433 | { |
||
| 434 | 1 | if (is_string($file)) { |
|
| 435 | $file = [$file, Charset::toCn($this->getBasename($file))]; |
||
| 436 | 1 | } elseif (is_array($file)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 437 | 1 | $file = Arrays::lists($file, 2); |
|
| 438 | 1 | if ($this->isDir($file[1])) { |
|
| 439 | 1 | $file[1] = rtrim($file[1], '/') . '/' . Charset::toCn($this->getBasename($file[0])); |
|
| 440 | } |
||
| 441 | } else { |
||
| 442 | $file = ['', '']; |
||
| 443 | } |
||
| 444 | 1 | return $file; |
|
| 445 | } |
||
| 446 | } |
||
| 447 |