| 1 |
<?php |
| 2 |
|
| 3 |
namespace RabbitLoader\SDK; |
| 4 |
|
| 5 |
class File |
| 6 |
{ |
| 7 |
private $debug = false; |
| 8 |
private $fp = '/'; |
| 9 |
|
| 10 |
public function __construct($fp = '') |
| 11 |
{ |
| 12 |
$this->fp = $fp; |
| 13 |
} |
| 14 |
|
| 15 |
public function setDebug($debug) |
| 16 |
{ |
| 17 |
$this->debug = $debug; |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
public function fpc($fp, &$data) |
| 22 |
{ |
| 23 |
if ($this->debug) { |
| 24 |
$file_updated = file_put_contents($fp, $data, LOCK_EX); |
| 25 |
} else { |
| 26 |
$file_updated = @file_put_contents($fp, $data, LOCK_EX); |
| 27 |
} |
| 28 |
if (!$file_updated && $this->debug) { |
| 29 |
throw new \Exception("could not write file $fp"); |
| 30 |
} |
| 31 |
return $file_updated; |
| 32 |
} |
| 33 |
|
| 34 |
public function fac(&$data) |
| 35 |
{ |
| 36 |
if ($this->debug) { |
| 37 |
$file_updated = file_put_contents($this->fp, $data, LOCK_EX | FILE_APPEND); |
| 38 |
} else { |
| 39 |
$file_updated = @file_put_contents($this->fp, $data, LOCK_EX | FILE_APPEND); |
| 40 |
} |
| 41 |
if (!$file_updated && $this->debug) { |
| 42 |
throw new \Exception("could not write file $this->fp"); |
| 43 |
} |
| 44 |
return $file_updated; |
| 45 |
} |
| 46 |
|
| 47 |
public function unlink() |
| 48 |
{ |
| 49 |
$file_deleted = false; |
| 50 |
if (!file_exists($this->fp)) { |
| 51 |
return true; |
| 52 |
} |
| 53 |
if ($this->debug) { |
| 54 |
$file_deleted = unlink($this->fp); |
| 55 |
} else { |
| 56 |
$file_deleted = @unlink($this->fp); |
| 57 |
} |
| 58 |
|
| 59 |
return $file_deleted; |
| 60 |
} |
| 61 |
|
| 62 |
public function cleanDir($dir, $max_limit, $offsetSec) |
| 63 |
{ |
| 64 |
$dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*'; |
| 65 |
$isBefore = time() - $offsetSec; |
| 66 |
$deleted_count = 0; |
| 67 |
$files = glob($dir); // get all file names |
| 68 |
foreach ($files as $file) { |
| 69 |
$delete = is_file($file); |
| 70 |
if ($offsetSec && filemtime($file) > $isBefore) { |
| 71 |
$delete = false; |
| 72 |
} |
| 73 |
if ($delete && @unlink($file)) { |
| 74 |
++$deleted_count; |
| 75 |
if ($max_limit > 0 && $deleted_count > $max_limit) { |
| 76 |
break; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
return $deleted_count; |
| 81 |
} |
| 82 |
|
| 83 |
public function lockForTime($fp, $mtime) |
| 84 |
{ |
| 85 |
try { |
| 86 |
if (file_exists($fp) && (filemtime($fp) > $mtime)) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
return $this->lock($fp); |
| 90 |
} catch (\Throwable $e) { |
| 91 |
Exc::catch($e); |
| 92 |
} catch (\Exception $e) { |
| 93 |
Exc::catch($e); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public function lock($fp) |
| 98 |
{ |
| 99 |
try { |
| 100 |
return touch($fp); |
| 101 |
} catch (\Throwable $e) { |
| 102 |
Exc::catch($e); |
| 103 |
} catch (\Exception $e) { |
| 104 |
Exc::catch($e); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public function isLocked($fp, $mtime) |
| 109 |
{ |
| 110 |
try { |
| 111 |
if (file_exists($fp) && filemtime($fp) > $mtime) { |
| 112 |
return true; |
| 113 |
} |
| 114 |
return false; |
| 115 |
} catch (\Throwable $e) { |
| 116 |
Exc::catch($e); |
| 117 |
} catch (\Exception $e) { |
| 118 |
Exc::catch($e); |
| 119 |
} |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
public function countFiles($dir) |
| 124 |
{ |
| 125 |
if (!is_dir($dir)) { |
| 126 |
return 0; |
| 127 |
} |
| 128 |
$fi = new \FilesystemIterator($dir, \FilesystemIterator::SKIP_DOTS); |
| 129 |
$fcount = iterator_count($fi) / 2; ///2 cause content and header |
| 130 |
return round($fcount); |
| 131 |
} |
| 132 |
|
| 133 |
public function fgc($length = 5000) |
| 134 |
{ |
| 135 |
if (empty($this->fp) || !file_exists($this->fp)) { |
| 136 |
return ''; |
| 137 |
} |
| 138 |
return file_get_contents($this->fp, false, null, 0, $length); |
| 139 |
} |
| 140 |
} |
| 141 |
|