web-base/Core/Configuration/Configuration.class.php

104 lines
2.6 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\Configuration;
2020-02-09 23:02:19 +01:00
2022-11-18 18:06:46 +01:00
use Core\Objects\ConnectionData;
2020-04-03 15:56:04 +02:00
2020-02-09 23:02:19 +01:00
class Configuration {
2022-11-18 18:06:46 +01:00
const className = "\Site\Configuration\Database";
2020-04-03 15:56:04 +02:00
private ?ConnectionData $database;
2020-06-25 16:54:58 +02:00
private Settings $settings;
2020-02-09 23:02:19 +01:00
function __construct() {
2020-06-25 16:54:58 +02:00
$this->database = null;
$this->settings = Settings::loadDefaults();
2022-11-18 18:06:46 +01:00
$className = self::className;
$path = getClassPath($className, ".class");
2021-04-02 21:58:06 +02:00
if (file_exists($path) && is_readable($path)) {
2020-06-25 16:54:58 +02:00
include_once $path;
2022-11-18 18:06:46 +01:00
if (class_exists($className)) {
$this->database = new $className();
2020-02-09 23:02:19 +01:00
}
}
}
2021-04-02 21:58:06 +02:00
public function getDatabase(): ?ConnectionData {
2020-06-25 16:54:58 +02:00
return $this->database;
}
2020-02-09 23:02:19 +01:00
2021-04-02 21:58:06 +02:00
public function getSettings(): Settings {
2020-06-25 16:54:58 +02:00
return $this->settings;
2020-02-09 23:02:19 +01:00
}
2022-05-31 16:14:49 +02:00
public static function create(string $className, $data) {
2022-11-18 18:06:46 +01:00
$path = getClassPath($className);
$classNameShort = explode("\\", $className);
$classNameShort = end($classNameShort);
2020-02-09 23:02:19 +01:00
2021-04-02 21:58:06 +02:00
if ($data) {
if (is_string($data)) {
2022-05-31 16:14:49 +02:00
$key = var_export($data, true);
2020-02-09 23:30:26 +01:00
$code = intendCode(
"<?php
2020-02-09 23:02:19 +01:00
2022-11-18 18:06:46 +01:00
namespace Core\Configuration;
2020-02-09 23:02:19 +01:00
2022-11-18 18:06:46 +01:00
class $classNameShort extends KeyData {
2020-04-03 15:56:04 +02:00
2020-02-09 23:30:26 +01:00
public function __construct() {
2022-05-31 16:14:49 +02:00
parent::__construct($key);
2020-02-09 23:30:26 +01:00
}
2020-04-03 15:56:04 +02:00
}", false
2020-02-09 23:30:26 +01:00
);
2021-04-02 21:58:06 +02:00
} else if ($data instanceof ConnectionData) {
2020-02-09 23:30:26 +01:00
$superClass = get_class($data);
2022-05-31 16:14:49 +02:00
$host = var_export($data->getHost(), true);
$port = var_export($data->getPort(), true);
$login = var_export($data->getLogin(), true);
$password = var_export($data->getPassword(), true);
2020-02-09 23:30:26 +01:00
$properties = "";
2021-04-02 21:58:06 +02:00
foreach ($data->getProperties() as $key => $val) {
2022-05-31 16:14:49 +02:00
$key = var_export($key, true);
$val = var_export($val, true);
$properties .= "\n\$this->setProperty($key, $val);";
2020-02-09 23:02:19 +01:00
}
2020-02-09 23:30:26 +01:00
$code = intendCode(
"<?php
2022-11-18 18:06:46 +01:00
namespace Site\Configuration;
2020-02-09 23:30:26 +01:00
2022-11-18 18:06:46 +01:00
class $classNameShort extends \\$superClass {
2020-02-09 23:30:26 +01:00
public function __construct() {
2022-05-31 16:14:49 +02:00
parent::__construct($host, $port, $login, $password);$properties
2020-02-09 23:30:26 +01:00
}
2020-06-17 21:39:46 +02:00
}", false
2020-02-09 23:30:26 +01:00
);
2020-04-03 15:56:04 +02:00
} else {
return false;
2020-02-09 23:30:26 +01:00
}
2020-02-09 23:02:19 +01:00
} else {
2020-04-03 15:56:04 +02:00
$code = "<?php";
2020-02-09 23:02:19 +01:00
}
2020-04-02 21:44:35 +02:00
return @file_put_contents($path, $code);
2020-02-09 23:02:19 +01:00
}
2021-04-02 21:58:06 +02:00
public function delete(string $className): bool {
2020-02-09 23:02:19 +01:00
$path = getClassPath("\\Configuration\\$className");
2021-04-02 21:58:06 +02:00
if (file_exists($path)) {
2020-02-09 23:02:19 +01:00
return unlink($path);
}
return true;
}
2022-05-31 16:14:49 +02:00
2022-11-18 18:06:46 +01:00
public function setDatabase(ConnectionData $connectionData): void {
2022-05-31 16:14:49 +02:00
$this->database = $connectionData;
}
2020-04-03 15:56:04 +02:00
}