-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrester_config.class.php
177 lines (159 loc) · 4.42 KB
/
rester_config.class.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Class rester_config
*/
class rester_config
{
const file_name = 'config.ini';
const common = 'common';
const common_database = 'database';
const common_database_default = 'default';
const auth = 'auth';
const auth_default = 'default';
const cache = 'cache';
const cache_default = 'default';
const access = 'access';
const access_default = 'default';
const access_private = 'private';
const access_internal = 'internal';
const access_public = 'public';
/**
* default value
* @var array
*/
protected $data = [
self::common => [
self::common_database => self::common_database_default
]
];
/**
* @var string
*/
protected $module;
/**
* rester_config constructor.
*
* @param string $module
* @param bool $base_path
*
* @throws Exception
*/
public function __construct($module, $base_path=false)
{
$this->module = $module;
if(!$base_path) $base_path = dirname(__FILE__).'/../src';
$path = $this->path_cfg($module, $base_path);
// init
if($path)
{
$cfg = parse_ini_file($path,true, INI_SCANNER_TYPED);
foreach($cfg as $k=>$v)
{
foreach($v as $kk=>$vv)
{
$this->data[$k][$kk] = $vv;
}
}
// Access level 검증
if($this->data[self::access])
{
foreach($this->data[self::access] as $proc => $level)
{
if( !($level==self::access_public || $level==self::access_internal || $level==self::access_private) )
throw new Exception("Access level must be [public|internal|private]. ({$this->module}/{$proc})", rester_response::code_config);
}
}
}
}
/**
* Path to config file
*
* @param string $module
* @param string $base_path
*
* @return bool|string
*/
protected function path_cfg($module, $base_path)
{
$path = implode('/',array(
$base_path,
$module,
self::file_name
));
if(is_file($path)) return $path;
return false;
}
/**
* 데이터베이스 설정정보 반환
*
* @return bool|array
*/
public function database()
{
return $this->data[self::common][self::common_database];
}
/**
* 권한 검사 유무 체크
* 1. 기본 false
* 2. config.ini 에 기본값이 설정된 경우 덮어씀
* 3. 권한 설정값이 입력되어 있을 경우 해당 값 설정
*
* @param string $proc
*
* @return bool
*/
public function is_auth($proc)
{
$result = false;
if(isset($this->data[self::auth][self::auth_default])) $result = $this->data[self::auth][self::auth_default];
if(isset($this->data[self::auth][$proc])) $result = $this->data[self::auth][$proc];
return $result;
}
/**
* 캐쉬 설정 반환
* 1. 기본 false
* 2. config.ini 에 기본값이 설정된 경우 덮어씀
* 3. 권한 설정값이 입력되어 있을 경우 해당 값 설정
*
* @param string $proc
*
* @return bool|int
*/
public function is_cache($proc)
{
$result = false;
if($v = $this->data[self::cache][self::cache_default]) $result = $v;
if($v = $this->data[self::cache][$proc]) $result = $v;
return $result;
}
/**
* Access level
* -------------------------------------------
* 1. 기본 public
* 2. config.ini 에 기본값이 설정된 경우 덮어씀
* 3. 권한 설정값이 입력되어 있을 경우 해당 값 설정
*
* @param string $proc
*
* @return string
*/
public function access_level($proc)
{
$result = self::access_public;
if($v = $this->data[self::access][self::access_default]) $result = $v;
if($v = $this->data[self::access][$proc]) $result = $v;
return $result;
}
/**
* @param string $section
* @param string $key
*
* @return string|array
*/
public function get($section='', $key='')
{
if($section==='') return $this->data;
if($section && $key) return $this->data[$section][$key];
return $this->data[$section];
}
}