-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConfig.php
More file actions
119 lines (100 loc) 路 2.57 KB
/
Copy pathConfig.php
File metadata and controls
119 lines (100 loc) 路 2.57 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/*
* Inphinit
*
* Copyright (c) 2026 Guilherme Nascimento (brcontainer@yahoo.com.br)
*
* Released under the MIT license
*/
namespace Inphinit;
use Inphinit\Diagnostics\Inspector;
class Config
{
private $exceptionLevel = 2;
private $data = array();
private $path;
/**
* Return items from a config file in a object (iterator or with `->`)
*
* @param string $path
* @throws \Inphinit\Exception
*/
public function __construct($path)
{
if (preg_match('#^\w+(\.\w[\w\.]+)?$#', $path) !== 1) {
throw new Exception('Invalid config path');
}
$this->path = 'configs/' . str_replace('.', '/', $path) . '.php';
$this->exceptionLevel = 3;
$this->reload();
}
/**
* Reload configuration from file
*
* @throws \Inphinit\Exception
*/
public function reload()
{
$level = $this->exceptionLevel;
$this->exceptionLevel = 2;
$configs = inphinit_sandbox($this->path);
if (is_array($configs) === false) {
throw new Exception($this->path . ' configurations cannot be loaded or format is invalid', 0, $level);
}
$this->data = $configs;
}
/**
* Save configuration to file
*
* @return bool
*/
public function commit()
{
$path = INPHINIT_SYSTEM . '/' . $this->path;
$contents = "<?php\nreturn " . var_export($this->data, true) . ";\n";
return file_put_contents($path, $contents, LOCK_EX) !== false;
}
/**
* Magic method for get specific item
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
return isset($this->data[$name]) ? $this->data[$name] : null;
}
/**
* Magic method for set value (this method don't save data)
*
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
if (is_scalar($value) === false && $value !== null) {
$type = Inspector::type($value);
throw new Exception("Expects to be scalar or null, {$type} given");
}
$this->data[$name] = $value;
}
/**
* Magic method for check if value exists in top-level
*
* @param string $name
* @return bool
*/
public function __isset($name)
{
return isset($this->data[$name]);
}
/**
* Magic method for unset variable with `unset()` function
*
* @param string $name
*/
public function __unset($name)
{
unset($this->data[$name]);
}
}