-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvironment.php
228 lines (211 loc) · 7.59 KB
/
Environment.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* Description of Environment
* @author candasm
* For cli setting example: export YII_APPLICATION_ENV=developement,test,stage,production
* For WebApp apache virtualHost: SetEnv YII_APPLICATION_ENV "development" (development,test,stage,production)
*
* why YII_APPLICATION_ENV ? it is just about cli name for not mix with any other variable if you want u can set it
*
* Config files are under protected/config/environment/
* Config file names starts with webapp,console,test example: webapp.production.php , console.test.php , test.development.php
* ConfigFile array should have these keys inside it key names are YII_PATH , YIIC_PATH, YII_DEBUG, YII_TRACE_LEVEL if it doesnt have YII_PATH or YIIC_PATH system will give an error!
*
* Pay attention environment config files are merging with standart config files. Environment config files are writing on standart config file so standart config files are just for global settings
*
* TODO write cli version for setup it easy
*
*/
class Environment {
/**
* @var string This is system variable name if u wanna use different u can change it!
*/
const APPLICATION_ENV = 'YII_APPLICATION_ENV';
/**
* @var string environment config folder under system config
*/
const ENVIRONMENT_CONFIG_FOLDER = 'environment';
/**
* @var string config folder path
*/
protected $configFolder=null;
/**
* @var string its shows for settings webapp,console,test
*/
protected $app;
/**
* @var array supported app names
*/
private $appNames = array('main', 'test', 'console');
private $defaultAppName = 'main';
/**
* @var array application config
*/
protected $config = array();
/**
* @var string Environment state
*/
protected $state;
protected $errors = array(
'config_file_doesnt_exists' => 'Config file does not exists {path}',
'important_key_name_not_seted' => 'Important key name "{keyName}" not seted in config file "{configFile}" ',
'app_name_is_not_supporting' => 'App name "{appName}" is not supporting!',
);
protected $systemConfig = array(
'YII_TRACE_LEVEL' => FALSE,
'YII_DEBUG' => FALSE,
'YII_PATH' => NULL,
'YIIC_PATH' => NULL,
'YIIT_PATH' => NULL,
);
/**
* @param string|NULL $app app type [webapp,console,test] if null default is webapp
* if YII_APPLICATION_ENV is not seted default is development if there is no config file system will give error!
*/
public function __construct($app = NULL) {
$environment = getenv(self::APPLICATION_ENV);
if (!$environment) {
$environment = 'development';
}
if (is_null($app)) {
$app = $this->defaultAppName;
}
if(is_null($this->configFolder)){
$this->configFolder = dirname(__FILE__) . '/../../../config/';
}
if (!in_array($app, $this->appNames)) {
echo 'Error: ' . str_replace('{appName}', $app, $this->errors['app_name_is_not_supporting']) . PHP_EOL;
exit;
}
$this->app = $app;
$this->state = $environment;
}
/**
* loads environment
*/
public function load(){
$this->install();
}
/**
* checks files if exists install config file and set config ;
*/
protected function install() {
try {
//set config file
$configFile = $this->app . '.' . $this->state . '.php';
$configFileFullPath = $this->configFolder . self::ENVIRONMENT_CONFIG_FOLDER . '/' . $configFile;
//check config file
if (!file_exists($configFileFullPath)) {
throw new Exception(str_replace('{path}', $configFile, $this->errors['config_file_doesnt_exists']));
}
//global config file name
$globalConfigFile = $this->configFolder . $this->app . '.php';
//get config
$config = include $configFileFullPath;
//merge if global config file name exists
if (file_exists($globalConfigFile)) {
$config = self::mergeArray(include $globalConfigFile, $config);
}
/**
* if these keys are not exists system will give error!
*/
$importantKeyNames = array(
'YII_PATH', 'YIIC_PATH',
);
/**
* theese key names are not necessary but if u set keys will set as defined variable!
*/
$unimportantKeyNames = array(
'YII_DEBUG', 'YII_TRACE_LEVEL', 'YIIT_PATH',
);
//check important keys
foreach ($importantKeyNames as $name) {
if (!array_key_exists($name, $config)) {
throw new Exception(str_replace(array('{keyName}', '{configFile}'), array($name, $configFile), $this->errors['important_key_name_not_seted']));
}
//set
$this->systemConfig[$name] = $config[$name];
}
//it doesnt important but also check YII_DEBUG , YII_TRACE_LEVEL and define it if itdoesnt defined
foreach ($unimportantKeyNames as $name) {
if (array_key_exists($name, $config)) {
defined($name) or define($name, $config[$name]);
//set
$this->systemConfig[$name] = $config[$name];
}
}
//unset YII_TRACE_LEVEL,YII_DEBUG,YII_PATH,YIIC_PATH,YIIT_PATH
$unsetKeys = array_merge($importantKeyNames, $unimportantKeyNames);
foreach ($unsetKeys as $name) {
if (array_key_exists($name, $config)) {
unset($config[$name]);
}
}
//set config file!
$this->config = $config;
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
exit;
}
}
/**
* returns config array
*/
public function getConfig() {
return $this->config;
}
/**
* returns frameworks yii path
*/
public function getYiiPath() {
return $this->systemConfig['YII_PATH'];
}
/**
* returns framework yiic file path
*/
public function getYiicPath() {
return $this->systemConfig['YIIC_PATH'];
}
/**
* returns framework yiit file path
*/
public function getYiitPath() {
return $this->systemConfig['YIIT_PATH'];
}
public function getState() {
return $this->state;
}
/**
* @return string Config folder path environment protected/config
*/
public function getConfigFolder(){
return $this->configFolder;
}
/**
* sets application config path
* @param string $configPath application config path
*
*/
public function setConfigFolder($configPath){
$this->configFolder = $configPath;
}
/**
* yiiframework cmap::mergearray function duplicated becaouse of this class starts before framework
*/
public static function mergeArray($a, $b) {
$args = func_get_args();
$res = array_shift($args);
while (!empty($args)) {
$next = array_shift($args);
foreach ($next as $k => $v) {
if (is_integer($k))
isset($res[$k]) ? $res[] = $v : $res[$k] = $v;
elseif (is_array($v) && isset($res[$k]) && is_array($res[$k]))
$res[$k] = self::mergeArray($res[$k], $v);
else
$res[$k] = $v;
}
}
return $res;
}
}