| Total Complexity | 45 |
| Total Lines | 307 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like FileLogger often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FileLogger, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class FileLogger extends AbstractLogger implements ConfigInterface |
||
| 20 | { |
||
| 21 | use ConfigTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * 文件 |
||
| 25 | * |
||
| 26 | * @var resource |
||
| 27 | */ |
||
| 28 | protected $temp; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * 临时文件名 |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $tempName; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * 移除文件 |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $removeFiles = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * 最后的日志 |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $latest; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * 构建文件日志 |
||
| 53 | * |
||
| 54 | * @param array $config |
||
| 55 | */ |
||
| 56 | public function __construct(array $config = []) |
||
| 57 | { |
||
| 58 | $this->set($config); |
||
| 59 | register_shutdown_function([$this, 'shutdown']); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * 设置配置 |
||
| 64 | * @param array $config |
||
| 65 | */ |
||
| 66 | public function set(array $config) |
||
| 67 | { |
||
| 68 | $this->applyConfig($config); |
||
| 69 | FileSystem::make($this->getConfig('save-path')); |
||
| 70 | FileSystem::make($this->getConfig('save-dump-path')); |
||
| 71 | FileSystem::make($this->getConfig('save-zip-path')); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return resource |
||
| 76 | * @throws FileLoggerException |
||
| 77 | */ |
||
| 78 | public function getAvailableWrite() |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @throws FileLoggerException |
||
| 89 | */ |
||
| 90 | private function prepareWrite() |
||
| 91 | { |
||
| 92 | $unique = substr(md5(uniqid()), 0, 8); |
||
| 93 | $save = $this->getConfig('save-path'); |
||
| 94 | $this->tempName = $save . '/' . date('YmdHis') . '.' . $unique . '.log'; |
||
| 95 | $temp = fopen($this->tempName, 'w+'); |
||
| 96 | if ($temp !== false) { |
||
| 97 | $this->temp = $temp; |
||
| 98 | } else { |
||
| 99 | throw new FileLoggerException(__METHOD__ . ':' . sprintf('cannot create log file')); |
||
| 100 | } |
||
| 101 | $this->latest = $save . '/' . $this->getConfig('file-name'); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | public function getDefaultConfig(): array |
||
| 117 | ]; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * 打包文件 |
||
| 122 | */ |
||
| 123 | private function packLogFile() |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $from |
||
| 163 | * @param string $to |
||
| 164 | */ |
||
| 165 | private function safeMoveKeep(string $from, string $to) |
||
| 166 | { |
||
| 167 | $fromFile = fopen($from, 'r'); |
||
| 168 | $toFile = fopen($to, 'w+'); |
||
| 169 | if ($fromFile !== false && $toFile !== false) { |
||
| 170 | flock($toFile, LOCK_EX); |
||
| 171 | // 复制内容 |
||
| 172 | stream_copy_to_stream($fromFile, $toFile); |
||
| 173 | flock($toFile, LOCK_UN); |
||
| 174 | fclose($toFile); |
||
| 175 | fclose($fromFile); |
||
| 176 | } |
||
| 177 | // 清空内容 |
||
| 178 | $this->clearContent($from); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * 清空内容 |
||
| 183 | * @param string $path |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | private function clearContent(string $path) |
||
| 187 | { |
||
| 188 | $file = fopen($path, 'w'); |
||
| 189 | if ($file) { |
||
|
|
|||
| 190 | fclose($file); |
||
| 191 | return true; |
||
| 192 | } |
||
| 193 | return false; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * 获取压缩 |
||
| 198 | * |
||
| 199 | * @param string $path |
||
| 200 | * @return ZipArchive|null |
||
| 201 | */ |
||
| 202 | private function getZipArchive(string $path) |
||
| 203 | { |
||
| 204 | if (class_exists('ZipArchive')) { |
||
| 205 | $zip = new ZipArchive; |
||
| 206 | $res = $zip->open($path, ZipArchive::CREATE); |
||
| 207 | if ($res === true) { |
||
| 208 | return $zip; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | return null; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * 检查日志文件大小 |
||
| 216 | * |
||
| 217 | * @return boolean |
||
| 218 | */ |
||
| 219 | private function checkSize(): bool |
||
| 220 | { |
||
| 221 | $logFile = $this->latest; |
||
| 222 | if (file_exists($logFile)) { |
||
| 223 | if (filesize($logFile) > $this->getConfig('max-file-size')) { |
||
| 224 | return true; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | return false; |
||
| 228 | } |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $level |
||
| 233 | * @param string $message |
||
| 234 | * @param array $context |
||
| 235 | * @return mixed|void |
||
| 236 | * @throws FileLoggerException |
||
| 237 | */ |
||
| 238 | public function log($level, string $message, array $context = []) |
||
| 239 | { |
||
| 240 | if (LogLevel::compare($level, $this->getConfig('log-level')) >= 0) { |
||
| 241 | $replace = []; |
||
| 242 | $message = $this->interpolate($message, $context); |
||
| 243 | $replace['%level%'] = $level; |
||
| 244 | $replace['%message%'] = $message; |
||
| 245 | $write = strtr($this->getConfig('log-format'), $replace); |
||
| 246 | fwrite($this->getAvailableWrite(), $write . PHP_EOL); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | |||
| 251 | /** |
||
| 252 | * 将临时文件写入最后日志 |
||
| 253 | */ |
||
| 254 | private function rollLatest() |
||
| 255 | { |
||
| 256 | if (isset($this->latest)) { |
||
| 257 | $latest = fopen($this->latest, 'a+'); |
||
| 258 | if (flock($latest, LOCK_EX)) { |
||
| 259 | rewind($this->temp); |
||
| 260 | stream_copy_to_stream($this->temp, $latest); |
||
| 261 | flock($latest, LOCK_UN); |
||
| 262 | if (file_exists($this->tempName)) { |
||
| 263 | unlink($this->tempName); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | fclose($latest); |
||
| 267 | fclose($this->temp); |
||
| 268 | $this->temp = null; |
||
| 269 | $this->tempName = null; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * 删除已经压缩的文件 |
||
| 275 | */ |
||
| 276 | private function removePackFiles() |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * 即时写入日志 |
||
| 288 | */ |
||
| 289 | public function write() |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * 程序关闭时调用 |
||
| 300 | */ |
||
| 301 | public function shutdown() |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $message |
||
| 311 | * @param array $context |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function interpolate(string $message, array $context) |
||
| 326 | } |
||
| 327 | } |
||
| 328 |