Changed config usage

This commit is contained in:
Maximilian Giller 2020-01-11 19:18:29 +01:00
parent 6e3ff52607
commit 78077e5ce8
7 changed files with 14 additions and 70 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
juggl/config/config.txt
juggl/config/config.path
juggl/config/config.php

View file

@ -1,6 +0,0 @@
<?php
require_once(__DIR__."/../services/configReader.inc.php");
$config = new ConfigReader();
$config->readPathFile(__DIR__."/config.path");
?>

View file

@ -1 +0,0 @@
relativepath=config/config.txt

View file

@ -0,0 +1,8 @@
<?php
$config = [
"host" => "",
"dbname" => "",
"username" => "",
"password" => "",
"table_prefix" => "ju_"
];

View file

@ -1,4 +0,0 @@
host=HOST
dbname=DBNAME
username=USERNAME
password=PASSWORD

View file

@ -1,54 +0,0 @@
<?php
class ConfigReader {
private const VALUE_SEPARATOR = '=';
function __construct () {
$this->configuration = array();
}
function readFile ($path) {
if (file_exists($path) == false)
return;
$this->configuration = array();
foreach (file($path) as $line) {
$valuePair = $this->convertLine(trim($line));
if ($valuePair === false)
continue;
$this->configuration[$valuePair['key']] = $valuePair['value'];
}
}
function readPathFile ($pathToPathFile) {
$this->readFile($pathToPathFile);
$this->readFile(__DIR__."/../".$this->getSetting("relativepath"));
}
function getSetting ($key) {
if (array_key_exists($key, $this->configuration) == false)
return NULL;
return $this->configuration[$key];
}
private function convertLine($line) {
if ($line == "")
return False;
$splitted = explode (self::VALUE_SEPARATOR, $line, 2);
$key = $splitted[0];
$value = "";
for ($i = 1; $i < sizeof($splitted); $i++) {
if ($i > 1) {
$value .= "=";
}
$value .= $splitted[$i];
}
return ['key' => $key, 'value' => $value];
}
}
?>

View file

@ -6,11 +6,11 @@ class DbOperations {
$this->resetQuery();
$this->tablePrefix = $tablePrefix;
require(__DIR__."/../config/config.inc.php");
require(__DIR__."/../config/config.php");
$this->config = $config;
if ($this->tablePrefix == null) {
$this->tablePrefix = $this->config->getSetting("table_prefix");
$this->tablePrefix = $this->config["table_prefix"];
}
}
@ -22,13 +22,13 @@ class DbOperations {
}
private function openConnection () {
$host = $this->config->getSetting('host');
$dbname = $this->config->getSetting('dbname');
$host = $this->config['host'];
$dbname = $this->config['dbname'];
$dsn = "mysql:host=$host;dbname=$dbname";
$options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC);
$this->pdo = new PDO($dsn, $this->config->getSetting('username'), $this->config->getSetting('password'), $options);
$this->pdo = new PDO($dsn, $this->config['username'], $this->config['password'], $options);
}
function select (string $table, array $attributes = array()) {