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

119 lines
2.8 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
namespace Configuration;
2020-04-03 15:56:04 +02:00
use Error;
use Objects\ConnectionData;
2020-02-09 23:02:19 +01:00
class Configuration {
2020-04-03 15:56:04 +02:00
private ?ConnectionData $database;
private ?ConnectionData $mail;
private ?KeyData $jwt;
2020-02-09 23:02:19 +01:00
function __construct() {
}
public function load() {
try {
$classes = array(
\Configuration\Database::class => &$this->database,
\Configuration\Mail::class => &$this->mail,
2020-02-09 23:30:26 +01:00
\Configuration\JWT::class => &$this->jwt
2020-02-09 23:02:19 +01:00
);
$success = true;
foreach($classes as $class => &$ref) {
$path = getClassPath($class);
if(!file_exists($path)) {
$success = false;
} else {
include_once $path;
if(class_exists($class)) {
$ref = new $class();
}
}
}
return $success;
2020-04-03 15:56:04 +02:00
} catch(Error $e) {
2020-02-09 23:02:19 +01:00
die($e);
}
}
public function getDatabase() { return $this->database; }
public function getJWT() { return $this->jwt; }
public function getMail() { return $this->mail; }
public function isFilePresent($className) {
$path = getClassPath("\\Configuration\\$className");
return file_exists($path);
}
2020-04-03 15:56:04 +02:00
public function create(string $className, $data) {
2020-02-09 23:02:19 +01:00
$path = getClassPath("\\Configuration\\$className");
if($data) {
2020-02-09 23:30:26 +01:00
if(is_string($data)) {
$key = addslashes($data);
$code = intendCode(
"<?php
2020-02-09 23:02:19 +01:00
2020-02-09 23:30:26 +01:00
namespace Configuration;
2020-02-09 23:02:19 +01:00
2020-04-03 15:56:04 +02:00
class $className extends KeyData {
2020-02-09 23:30:26 +01:00
public function __construct() {
2020-04-03 15:56:04 +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
);
2020-04-03 15:56:04 +02:00
} else if($data instanceof ConnectionData) {
2020-02-09 23:30:26 +01:00
$superClass = get_class($data);
$host = addslashes($data->getHost());
$port = intval($data->getPort());
$login = addslashes($data->getLogin());
$password = addslashes($data->getPassword());
$properties = "";
foreach($data->getProperties() as $key => $val) {
$key = addslashes($key);
$val = is_string($val) ? "'" . addslashes($val) . "'" : $val;
$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
namespace Configuration;
class $className extends \\$superClass {
public function __construct() {
parent::__construct('$host', $port, '$login', '$password');$properties
}
}
?>", false
);
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
}
2020-04-03 15:56:04 +02:00
public function delete(string $className) {
2020-02-09 23:02:19 +01:00
$path = getClassPath("\\Configuration\\$className");
if(file_exists($path)) {
return unlink($path);
}
return true;
}
2020-04-03 15:56:04 +02:00
}