-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.php
90 lines (75 loc) · 2.23 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace common;
// do configuration of the application
class Config
{
private $valid_types = array(
'log',
'system',
'php'
);
private $log_file = null;
private $system = [];
private $php_value = [];
private static $instance = null;
public static function obj(?string $file = null) : Config
{
if ($file !== null)
{
$file = new \SplFileInfo($file);
if (!$file->isReadable()) {
die('Configuration file not found: '. $file ."\n");
}
$config = parse_ini_file($file->getRealPath(), TRUE);
self::$instance = new Config();
foreach ($config as $type => $value)
{
if (in_array($type, self::$instance->valid_types))
{
self::$instance->{'set_' . strtolower($type)}($config[$type]);
}
}
unset($file, $config, $type, $value);
}
return self::$instance;
}
private function set_log(array $logInfo) : void
{
$file = new \SplFileObject($logInfo['file'], 'a');
if (!$file->isWritable())
throw new \RuntimeException('File does not exist');
$this->log_file = $file;
}
private function set_system(array $system_info) : void
{
if (!isset($system_info['timezone']))
{
$system_info['timezone'] = 'UTC';
}
$this->system = $system_info;
date_default_timezone_set($system_info['timezone']);
}
private function set_php_value(array $phpValue) : void
{
foreach ($phpValue as $k => $value)
{
if (ini_set($k, $value) !== FALSE)
{
$this->php_value[$k] = $value;
}
else
{
Logger::obj()->writeDebug('Cannot set PHP VALUE: ' . $k . ' -> ' . $value, 1);
}
}
unset($k, $value);
}
public function __get($name)
{
if (!property_exists($this, $name))
{
throw new \UnexpectedValueException('Property '.$name.' does not exist');
}
return $this->$name;
}
}