BPTproto /
BPT-Multi
| 1 | <?php |
||
| 2 | |||
| 3 | namespace BPT; |
||
| 4 | /** |
||
| 5 | * lock class , Manage and handle lock files |
||
| 6 | */ |
||
| 7 | class lock { |
||
| 8 | /** |
||
| 9 | * Check lock is exist or not |
||
| 10 | * |
||
| 11 | * @param string $name |
||
| 12 | * |
||
| 13 | * @return bool |
||
| 14 | */ |
||
| 15 | public static function exist(string $name): bool { |
||
| 16 | return file_exists(realpath(settings::$name."$name.lock")); |
||
| 17 | } |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Set lock(create lock) |
||
| 21 | * |
||
| 22 | * @param string $name |
||
| 23 | * |
||
| 24 | * @return bool |
||
| 25 | */ |
||
| 26 | public static function set(string $name): bool { |
||
| 27 | return touch(settings::$name."$name.lock"); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Save data in lock file and make it local |
||
| 32 | * |
||
| 33 | * @param string $name |
||
| 34 | * @param string $data |
||
| 35 | * |
||
| 36 | * @return bool|int |
||
| 37 | */ |
||
| 38 | public static function save(string $name, string $data): bool|int { |
||
| 39 | return file_put_contents(settings::$name."$name.lock", $data) && chmod(settings::$name."$name.lock",0640); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Read data from lock |
||
| 44 | * |
||
| 45 | * @param string $name |
||
| 46 | * |
||
| 47 | * @return bool|string |
||
| 48 | */ |
||
| 49 | public static function read(string $name): bool|string { |
||
| 50 | return file_get_contents(realpath(settings::$name."$name.lock")); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get last modify time of lock |
||
| 55 | * |
||
| 56 | * @param string $name |
||
| 57 | * |
||
| 58 | * @return bool|int |
||
| 59 | */ |
||
| 60 | public static function mtime(string $name): bool|int { |
||
| 61 | return filemtime(realpath(settings::$name."$name.lock")); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Delete lock |
||
| 66 | * |
||
| 67 | * @param string $name |
||
| 68 | * |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | public static function delete(string $name): bool { |
||
| 72 | return unlink(realpath(settings::$name."$name.lock")); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * check lock exist or not then delete it |
||
| 77 | * |
||
| 78 | * @param string|array $names |
||
| 79 | * |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | public static function deleteIfExist (string|array $names): bool { |
||
| 83 | if (is_string($names)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 84 | $names = [$names]; |
||
| 85 | } |
||
| 86 | foreach ($names as $name) { |
||
| 87 | if ($path = realpath(settings::$name."$name.lock")) { |
||
| 88 | unlink($path); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | return true; |
||
| 92 | } |
||
| 93 | } |