-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9c0bd0
commit 37d30ea
Showing
21 changed files
with
729 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
AVE_BUFFER_FOLDER="/etc/AVE-PHP/Buffer" | ||
AVE_DATA_FOLDER="/etc/AVE-PHP/Data" | ||
AVE_LOG_FOLDER="/var/log/AVE-PHP" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
AVE_BUFFER_FOLDER="%LOCALAPPDATA%\AVE-PHP\Buffer" | ||
AVE_DATA_FOLDER="%LOCALAPPDATA%\AVE-PHP\Data" | ||
AVE_LOG_FOLDER="%LOCALAPPDATA%\AVE-PHP\Logs" | ||
AVE_COLOR="9F" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services; | ||
|
||
use Exception; | ||
|
||
class AppBuffer { | ||
|
||
protected string $path; | ||
|
||
public int $version = 10000; | ||
|
||
function __construct(string $path){ | ||
$this->path = $path; | ||
if(!file_exists($this->path)) mkdir($this->path, 0755, true); | ||
} | ||
|
||
public function getPath() : string { | ||
return $this->path; | ||
} | ||
|
||
public function getFile(string $key) : string { | ||
$key = hash('sha256', $key); | ||
return $this->path.DIRECTORY_SEPARATOR."$key.json"; | ||
} | ||
|
||
public function get(string $key, int|bool|string|array|float|null $default = null) : mixed { | ||
$file = $this->getFile($key); | ||
if(!file_exists($file)) return $default; | ||
$buffer = json_decode(file_get_contents($file), true); | ||
if(!isset($buffer['expire']) || !isset($buffer['type'])) return $default; | ||
if($buffer['expire'] != -1 && time() > $buffer['expire']) return $default; | ||
switch(strtolower($buffer['type'])){ | ||
case 'integer': return (int)$buffer['value']; | ||
case 'boolean': return ($buffer['value'] == 'true'); | ||
case 'string': return $buffer['value']; | ||
case 'array': return json_decode(base64_decode($buffer['value']), true); | ||
case 'float': (float)$buffer['value']; | ||
case 'null': return null; | ||
} | ||
return null; | ||
} | ||
|
||
public function set(string $key, int|bool|string|array|float|null $value, int $expire = -1) : void { | ||
$file = $this->getFile($key); | ||
if($expire > 0) $expire = time() + $expire; | ||
$type = strtolower(gettype($value)); | ||
switch($type){ | ||
case 'float': | ||
case 'integer': { | ||
$value = strval($value); | ||
break; | ||
} | ||
case 'boolean': { | ||
$value = $value ? 'true' : 'false'; | ||
break; | ||
} | ||
case 'array': { | ||
$value = base64_encode(json_encode($value)); | ||
break; | ||
} | ||
case 'null': { | ||
$value = 'null'; | ||
break; | ||
} | ||
} | ||
file_put_contents($file, json_encode([ | ||
'key' => $key, | ||
'type' => $type, | ||
'value' => $value, | ||
'expire' => $expire, | ||
])); | ||
} | ||
|
||
public function forget(string $key) : void { | ||
$this->delete($this->getFile($key)); | ||
} | ||
|
||
public function clearExpired() : void { | ||
$files = scandir($this->path); | ||
foreach($files as $file){ | ||
$buffer = json_decode(file_get_contents($this->path.DIRECTORY_SEPARATOR.$file), true); | ||
if($buffer['expire'] != -1 && time() > $buffer['expire']){ | ||
$this->delete($this->path.DIRECTORY_SEPARATOR.$file); | ||
} | ||
} | ||
} | ||
|
||
public function clear() : void { | ||
$files = scandir($this->path); | ||
foreach($files as $file){ | ||
$this->delete($this->path.DIRECTORY_SEPARATOR.$file); | ||
} | ||
} | ||
|
||
private function delete(string $path) : void { | ||
try { | ||
if(file_exists($path)) unlink($path); | ||
} | ||
catch(Exception $e){ | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.