.htaccess
1 year ago
ArrayData.php
1 year ago
DBObject.php
1 year ago
Engine.php
1 year ago
Mysqldump.php
5 months ago
ReflectionObject.php
1 year ago
SleekStore.php
1 year ago
index.html
1 year ago
web.config
1 year ago
ReflectionObject.php
84 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Data; |
| 4 | |
| 5 | use JetBackup\Exception\IOException; |
| 6 | use JetBackup\IO\Lock; |
| 7 | use ReflectionClass; |
| 8 | use ReflectionException; |
| 9 | |
| 10 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 11 | |
| 12 | class ReflectionObject extends ArrayData { |
| 13 | |
| 14 | private string $_file; |
| 15 | private string $_className; |
| 16 | private array $_diff; |
| 17 | |
| 18 | /** |
| 19 | * @param string $file |
| 20 | * @param string $className |
| 21 | * |
| 22 | * @throws ReflectionException |
| 23 | * @throws IOException |
| 24 | */ |
| 25 | public function __construct(string $file, string $className) { |
| 26 | $this->_file = $file; |
| 27 | $this->_className = $className; |
| 28 | $this->_diff = []; |
| 29 | |
| 30 | // Create the class if not exists |
| 31 | if(!file_exists($this->_file)) { |
| 32 | $this->loadFromDatabase(); |
| 33 | $this->save(); |
| 34 | } |
| 35 | |
| 36 | chmod($this->_file, 0600); |
| 37 | |
| 38 | require_once($this->_file); |
| 39 | $this->setData((new ReflectionClass($this->_className))->getConstants()); |
| 40 | } |
| 41 | |
| 42 | function loadFromDatabase():void {} |
| 43 | |
| 44 | public function getDiff():array { return $this->_diff; } |
| 45 | |
| 46 | public function set($key, $value) { |
| 47 | $this->_diff[$key] = $value; |
| 48 | parent::set($key, $value); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @return void |
| 53 | * @throws IOException |
| 54 | */ |
| 55 | public function save():void { |
| 56 | if (!($f = fopen($this->_file, 'w'))) { |
| 57 | Lock::UnlockFile($this->_file . '.lock'); |
| 58 | throw new IOException("Error creating config file " . $this->_file); |
| 59 | } |
| 60 | fwrite($f, "<?php" . PHP_EOL); |
| 61 | fwrite($f, "if (!defined( '__JETBACKUP__')) die('Direct access is not allowed');" . PHP_EOL); |
| 62 | fwrite($f, "class $this->_className {" . PHP_EOL); |
| 63 | foreach ($this->getData() as $key => $value) { |
| 64 | switch (true) { |
| 65 | case is_array($value): |
| 66 | case is_object($value): |
| 67 | $value = ''; // Unsupported types default to an empty string |
| 68 | break; |
| 69 | case is_bool($value): |
| 70 | $value = $value ? 'true' : 'false'; // Convert booleans to 'true' or 'false' |
| 71 | break; |
| 72 | case is_null($value): |
| 73 | $value = 'null'; // set null values to 'null' |
| 74 | break; |
| 75 | case !is_int($value) && !is_float($value): |
| 76 | $value = "'" . preg_replace("/([\\\'])/", "\\\\$1", $value) . "'"; // Wrap non-numeric strings in single quotes |
| 77 | break; |
| 78 | } |
| 79 | fwrite($f, "\tconst $key = $value;" . PHP_EOL); |
| 80 | } |
| 81 | fwrite($f, "}"); |
| 82 | fclose($f); |
| 83 | } |
| 84 | } |