forked from tylerhall/simple-php-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.config.php
More file actions
182 lines (151 loc) · 6.44 KB
/
class.config.php
File metadata and controls
182 lines (151 loc) · 6.44 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
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
<?PHP
// The Config class provides a single object to store your application's settings.
// Define your settings as public members. (We've already setup the standard options
// required for the Database and Auth classes.) Then, assign values to those settings
// inside the "location" functions. This allows you to have different configuration
// options depending on the server environment you're running on. Ex: local, staging,
// and production.
class Config
{
// Singleton object. Leave $me alone.
private static $me;
// Add your server hostnames to the appropriate arrays. ($_SERVER['HTTP_HOST'])
// Each array item should be a regular expression. This gives you the option to detect a whole range
// of server names if needed. Otherwise, you can simply detect a single server like '/^servername\.com$/'
private $productionServers = array('/^your-domain\.com$/');
private $stagingServers = array();
private $localServers = array();
// Standard Config Options...
// ...For Auth Class
public $authDomain; // Domain to set for the cookie
public $authSalt; // Can be any random string of characters
// ...For Database Class
public $dbReadHost; // Database read-only server
public $dbWriteHost; // Database read/write server
public $dbName;
public $dbReadUsername;
public $dbWriteUsername;
public $dbReadPassword;
public $dbWritePassword;
public $dbOnError; // What do do on a database error (see class.database.php for details)
public $dbEmailOnError; // Email an error report on error?
// Add your config options here...
public $useDBSessions; // Set to true to store sessions in the database
// Singleton constructor
private function __construct()
{
$this->everywhere();
$i_am_here = $this->whereAmI();
if('production' == $i_am_here)
$this->production();
elseif('staging' == $i_am_here)
$this->staging();
elseif('local' == $i_am_here)
$this->local();
elseif('shell' == $i_am_here)
$this->shell();
else
die('<h1>Where am I?</h1> <p>You need to setup your server names in <code>class.config.php</code></p>
<p><code>$_SERVER[\'HTTP_HOST\']</code> reported <code>' . $_SERVER['HTTP_HOST'] . '</code></p>');
}
// Get Singleton object
public static function getConfig()
{
if(is_null(self::$me))
self::$me = new Config();
return self::$me;
}
// Allow access to config settings statically.
// Ex: Config::get('some_value')
public static function get($key)
{
return self::$me->$key;
}
// Add code to be run on all servers
private function everywhere()
{
// Store sesions in the database?
$this->useDBSessions = true;
// Settings for the Auth class
$this->authDomain = $_SERVER['HTTP_HOST'];
$this->authSalt = '';
}
// Add code/variables to be run only on production servers
private function production()
{
ini_set('display_errors', '0');
define('WEB_ROOT', '/');
$this->dbReadHost = 'localhost';
$this->dbWriteHost = 'localhost';
$this->dbName = '';
$this->dbReadUsername = '';
$this->dbWriteUsername = '';
$this->dbReadPassword = '';
$this->dbWritePassword = '';
$this->dbOnError = '';
$this->dbEmailOnError = false;
}
// Add code/variables to be run only on staging servers
private function staging()
{
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
define('WEB_ROOT', '');
$this->dbReadHost = 'localhost';
$this->dbWriteHost = 'localhost';
$this->dbName = '';
$this->dbReadUsername = '';
$this->dbWriteUsername = '';
$this->dbReadPassword = '';
$this->dbWritePassword = '';
$this->dbOnError = 'die';
$this->dbEmailOnError = false;
}
// Add code/variables to be run only on local (testing) servers
private function local()
{
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
define('WEB_ROOT', '');
$this->dbReadHost = 'localhost';
$this->dbWriteHost = 'localhost';
$this->dbName = '';
$this->dbReadUsername = '';
$this->dbWriteUsername = '';
$this->dbReadPassword = '';
$this->dbWritePassword = '';
$this->dbOnError = 'die';
$this->dbEmailOnError = false;
}
// Add code/variables to be run only on when script is launched from the shell
private function shell()
{
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
define('WEB_ROOT', '');
$this->dbReadHost = 'localhost';
$this->dbWriteHost = 'localhost';
$this->dbName = '';
$this->dbReadUsername = '';
$this->dbWriteUsername = '';
$this->dbReadPassword = '';
$this->dbWritePassword = '';
$this->dbOnError = false;
$this->dbEmailOnError = true;
}
public function whereAmI()
{
for($i = 0; $i < count($this->productionServers); $i++)
if(preg_match($this->productionServers[$i], getenv('HTTP_HOST')) === 1)
return 'production';
for($i = 0; $i < count($this->stagingServers); $i++)
if(preg_match($this->stagingServers[$i], getenv('HTTP_HOST')) === 1)
return 'staging';
for($i = 0; $i < count($this->localServers); $i++)
if(preg_match($this->localServers[$i], getenv('HTTP_HOST')) === 1)
return 'local';
if(isset($_ENV['SHELL']))
return 'shell';
return false;
}
}