-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mfwServerEnv.php
145 lines (127 loc) · 3.16 KB
/
mfwServerEnv.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
<?php
/** @file
* Server environment.
*/
class mfwServerEnv
{
const CONFIG_FILE = '/config/mfw_serverenv_config.php';
protected static $env = null;
protected static $config = null;
protected static $cache_prefix = '';
protected static $dbauth = array();
protected static function errorLog($message)
{
$file = self::CONFIG_FILE;
$env = self::$env;
error_log("$file: serverenv_config[$env]: $message");
}
/**
* 環境を指定(設定読み込み).
* 必ず最初に呼ぶ.
* @return 成功なら$env, 失敗ならnull.
*/
public static function setEnv($env)
{
include APP_ROOT.self::CONFIG_FILE;
self::$env = $env;
$id = isset($serverenv_config['application_identifier'])?
$serverenv_config['application_identifier']: '';
$id = str_replace(' ','_',$id);
$branch = basename(APP_ROOT);
self::$cache_prefix = "{$id}_{$env}_{$branch}_";
if(isset($serverenv_config[$env])){
self::$config = $serverenv_config[$env];
}
else{
static::errorLog("undefined environment");
self::$config = null;
}
return self::$config;
}
/**
* 現在の環境を取得.
*/
public static function getEnv()
{
return self::$env;
}
protected static function loadConfig($cat)
{
if(is_null(self::$config)){
// config読み込まれていない.
throw new InvalidArgumentException(__CLASS__.': invalid environment ('.self::$env.')');
}
if(!array_key_exists($cat, self::$config)){
static::errorLog("undefined category: $cat");
return null; // 項目が無いだけ
}
return self::$config[$cat];
}
/**
* HTTP Proxy.
* @return array('host'=>hostname, 'port'=>port)
*/
public static function httpProxy()
{
$proxy = static::loadConfig('http_proxy');
if(is_null($proxy)){
return null;
}
if(!isset($proxy['host']) || !isset($proxy['port'])){
return null;
}
return $proxy;
}
/**
* DatabaseのDSN, ユーザ名, パスワード.
* @return array('dsn'=>DSN, 'user'=>USER, 'pass'=>PASS)
*/
public static function databaseSetting($dbname)
{
$conf = static::loadConfig('database');
if(!isset($conf['authfile']) || !isset($conf[$dbname])){
static::errorLog("database: authfile/dsn($dbname) is not defined");
return null;
}
$authfile = $conf['authfile'];
if(!isset(self::$dbauth[$authfile])){
if(!is_file($authfile)){
throw new Exception(__CLASS__.": no database authfile ($authfile)");
}
$content = file_get_contents($authfile);
self::$dbauth[$authfile] = explode(':',trim($content));
}
$auth = self::$dbauth[$authfile];
return array(
'dsn' => $conf[$dbname],
'user' => $auth[0],
'pass' => isset($auth[1])?$auth[1]:null);
}
/**
* Memcacheのホスト、ポート.
* @return array('host'=>host, 'port'=>port)
*/
public static function memcache()
{
$conf = static::loadConfig('memcache');
if(!isset($conf['host']) || !isset($conf['port'])){
static::errorLog("memcache: host/port is not defined");
return null;
}
return $conf;
}
/**
* swfmill コマンドのパス.
* 指定無しならnull.
*/
public static function swfmill()
{
return static::loadConfig('swfmill');
}
/**
*/
public static function cachePrefix()
{
return static::$cache_prefix;
}
}