| Total Complexity | 24 |
| Total Lines | 154 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 0 | ||
| 1 | <?php |
||
| 21 | class File extends SplFileInfo |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * 文件hash规则 |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $hash = []; |
||
| 29 | |||
| 30 | protected $hashName; |
||
| 31 | |||
| 32 | public function __construct(string $path, bool $checkPath = true) |
||
| 33 | { |
||
| 34 | if ($checkPath && !is_file($path)) { |
||
| 35 | throw new FileException(sprintf('The file "%s" does not exist', $path)); |
||
| 36 | } |
||
| 37 | |||
| 38 | parent::__construct($path); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * 获取文件的哈希散列值 |
||
| 43 | * @access public |
||
| 44 | * @param string $type |
||
|
1 ignored issue
–
show
|
|||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | public function hash(string $type = 'sha1'): string |
||
| 54 | } |
||
| 55 | |||
| 56 | public function md5() |
||
| 57 | { |
||
| 58 | return $this->hash('md5'); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function sha1() |
||
| 62 | { |
||
| 63 | return $this->hash('sha1'); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * 获取文件类型信息 |
||
| 68 | * @access public |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function getMime(): string |
||
| 72 | { |
||
| 73 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
||
| 74 | |||
| 75 | return finfo_file($finfo, $this->getPathname()); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * 移动文件 |
||
| 80 | * @access public |
||
| 81 | * @param string $directory 保存路径 |
||
|
1 ignored issue
–
show
|
|||
| 82 | * @param string|bool $name 保存的文件名 |
||
|
1 ignored issue
–
show
|
|||
| 83 | * @return File |
||
| 84 | */ |
||
| 85 | public function move(string $directory, $name = null) |
||
| 86 | { |
||
| 87 | $target = $this->getTargetFile($directory, $name); |
||
| 88 | |||
| 89 | set_error_handler(function ($type, $msg) use (&$error) { |
||
| 90 | $error = $msg; |
||
| 91 | }); |
||
| 92 | $renamed = rename($this->getPathname(), $target); |
||
| 93 | restore_error_handler(); |
||
| 94 | if (!$renamed) { |
||
| 95 | throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error))); |
||
| 96 | } |
||
| 97 | |||
| 98 | @chmod($target, 0666 & ~umask()); |
||
| 99 | |||
| 100 | return $target; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * 实例化一个新文件 |
||
| 105 | * @param string $directory |
||
|
1 ignored issue
–
show
|
|||
| 106 | * @param null|string $name |
||
|
1 ignored issue
–
show
|
|||
| 107 | * @return File |
||
| 108 | */ |
||
| 109 | protected function getTargetFile($directory, $name = null) |
||
| 110 | { |
||
| 111 | if (!is_dir($directory)) { |
||
| 112 | if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) { |
||
| 113 | throw new FileException(sprintf('Unable to create the "%s" directory', $directory)); |
||
| 114 | } |
||
| 115 | } elseif (!is_writable($directory)) { |
||
| 116 | throw new FileException(sprintf('Unable to write in the "%s" directory', $directory)); |
||
| 117 | } |
||
| 118 | |||
| 119 | $target = rtrim($directory, '/\\') . \DIRECTORY_SEPARATOR . (null === $name ? $this->getBasename() : $this->getName($name)); |
||
| 120 | |||
| 121 | return new self($target, false); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * 获取文件名 |
||
| 126 | * @param $name |
||
|
1 ignored issue
–
show
|
|||
| 127 | * @return bool|mixed|string |
||
| 128 | */ |
||
| 129 | protected function getName($name) |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * 文件扩展名 |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function extension() |
||
| 143 | { |
||
| 144 | return $this->getExtension(); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * 自动生成文件名 |
||
| 149 | * @access protected |
||
| 150 | * @param string|\Closure $rule |
||
|
1 ignored issue
–
show
|
|||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function hashName($rule = 'date'): string |
||
| 175 | } |
||
| 176 | } |
||
| 177 |