|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Kotori.php |
|
4
|
|
|
* |
|
5
|
|
|
* A Tiny Model-View-Controller PHP Framework |
|
6
|
|
|
* |
|
7
|
|
|
* This content is released under the Apache 2 License |
|
8
|
|
|
* |
|
9
|
|
|
* Copyright (c) 2015-2017 Kotori Technology. All rights reserved. |
|
10
|
|
|
* |
|
11
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
12
|
|
|
* you may not use this file except in compliance with the License. |
|
13
|
|
|
* You may obtain a copy of the License at |
|
14
|
|
|
* |
|
15
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
16
|
|
|
* |
|
17
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
18
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
19
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
20
|
|
|
* See the License for the specific language governing permissions and |
|
21
|
|
|
* limitations under the License. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Config Class |
|
26
|
|
|
* |
|
27
|
|
|
* This class contains functions that enable config files to be managed |
|
28
|
|
|
* |
|
29
|
|
|
* @package Kotori |
|
30
|
|
|
* @subpackage Core |
|
31
|
|
|
* @author Kokororin |
|
32
|
|
|
* @link https://kotori.love |
|
33
|
|
|
*/ |
|
34
|
|
|
namespace Kotori\Core; |
|
35
|
|
|
|
|
36
|
|
|
use Kotori\Debug\Hook; |
|
37
|
|
|
use Kotori\Exception\ConfigException; |
|
38
|
|
|
|
|
39
|
|
|
class Config |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* Config Array |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $config = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Default Config Array |
|
50
|
|
|
* |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $defaults = [ |
|
54
|
|
|
'app_debug' => true, |
|
55
|
|
|
'app_name' => 'app', |
|
56
|
|
|
'url_mode' => 'query_string', |
|
57
|
|
|
'time_zone' => 'Asia/Shanghai', |
|
58
|
|
|
'session' => [ |
|
59
|
|
|
'adapter' => '', |
|
60
|
|
|
'auto_start' => false, |
|
61
|
|
|
], |
|
62
|
|
|
'url_route_annotation' => false, |
|
63
|
|
|
'middleware' => [ |
|
64
|
|
|
'before_app' => [], |
|
65
|
|
|
'after_app' => [], |
|
66
|
|
|
'before_route' => [], |
|
67
|
|
|
'after_route' => [], |
|
68
|
|
|
'before_controller' => [], |
|
69
|
|
|
'after_controller' => [], |
|
70
|
|
|
'before_action' => [], |
|
71
|
|
|
'after_action' => [], |
|
72
|
|
|
], |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Class constructor |
|
77
|
|
|
* |
|
78
|
|
|
* Initialize Config. |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function __construct() |
|
81
|
|
|
{ |
|
82
|
3 |
|
Hook::listen(__CLASS__); |
|
83
|
3 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Initialize Config |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $config |
|
89
|
|
|
* @return \Kotori\Core\Config |
|
90
|
|
|
* |
|
91
|
|
|
* @throws \Kotori\Exception\ConfigException |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function initialize(array $config = []) |
|
94
|
|
|
{ |
|
95
|
3 |
|
$this->config = $config; |
|
96
|
3 |
|
if (is_array($this->config)) { |
|
97
|
3 |
|
if (array_keys($this->config) !== range(0, count($this->config) - 1)) { |
|
98
|
3 |
|
if (isset($this->config['db']) && is_array($this->config['db'])) { |
|
99
|
|
|
foreach ($this->config['db'] as $key => &$value) { |
|
100
|
|
|
if (!isset($value['port'])) { |
|
101
|
|
|
$value['port'] = 3306; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (!isset($value['charset'])) { |
|
105
|
|
|
$value['charset'] = 'utf8'; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
3 |
|
$this->config = array_merge($this->defaults, $this->config); |
|
111
|
3 |
|
if (is_array($this->get('app_name'))) { |
|
112
|
|
|
$hostName = Container::get('request')->getHostName(); |
|
113
|
|
|
if (array_key_exists($hostName, $this->get('app_name'))) { |
|
114
|
|
|
$appName = $this->get('app_name')[$hostName]; |
|
115
|
|
|
} else { |
|
116
|
|
|
throw new ConfigException('Cannot find any app paths.'); |
|
117
|
|
|
} |
|
118
|
|
|
} else { |
|
119
|
3 |
|
$appName = $this->get('app_name'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
3 |
|
if (Container::get('request')->isCli()) { |
|
123
|
3 |
|
$stack = debug_backtrace(); |
|
124
|
3 |
|
$firstFrame = $stack[count($stack) - 1]; |
|
125
|
3 |
|
$initialFile = $firstFrame['file']; |
|
126
|
3 |
|
$appFullPath = realpath(dirname($initialFile) . '/../' . rtrim($appName, '/')); |
|
127
|
|
|
} else { |
|
128
|
|
|
$appFullPath = realpath(realpath('.') . '/../' . rtrim($appName, '/')); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
3 |
|
if (!$appFullPath && !defined('KOTORI_PHP_TEST_ENV')) { |
|
132
|
|
|
throw new ConfigException('Cannot find your app directory (' . $appName . ').'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
$this->config = array_merge([ |
|
136
|
3 |
|
'app_full_path' => $appFullPath, |
|
137
|
3 |
|
], $this->config); |
|
138
|
3 |
|
$this->set('namespace_prefix', basename($this->get('app_full_path')) . '\\'); |
|
139
|
|
|
} |
|
140
|
|
|
} else { |
|
141
|
|
|
throw new ConfigException('config is not an array'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
3 |
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Set the specified config item |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $key |
|
151
|
|
|
* @param mixed $value |
|
152
|
|
|
* @return void |
|
153
|
|
|
* |
|
154
|
|
|
* @throws \Kotori\Exception\ConfigException |
|
155
|
|
|
*/ |
|
156
|
3 |
|
public function set($key, $value) |
|
157
|
|
|
{ |
|
158
|
3 |
|
if (is_string($key)) { |
|
159
|
3 |
|
$this->config[$key] = $value; |
|
160
|
|
|
} else { |
|
161
|
|
|
throw new ConfigException('config key must be a string'); |
|
162
|
|
|
} |
|
163
|
3 |
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Returns the specified config item |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $key |
|
169
|
|
|
* @return mixed |
|
170
|
|
|
*/ |
|
171
|
12 |
|
public function get($key) |
|
172
|
|
|
{ |
|
173
|
12 |
|
if (is_string($key)) { |
|
174
|
12 |
|
return isset($this->config[$key]) ? $this->config[$key] : null; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return null; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Return the config array |
|
182
|
|
|
* |
|
183
|
|
|
* @return array |
|
184
|
|
|
*/ |
|
185
|
1 |
|
public function getArray() |
|
186
|
|
|
{ |
|
187
|
1 |
|
return $this->config; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|