-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironmentHelper.php
More file actions
executable file
·164 lines (140 loc) · 4.33 KB
/
EnvironmentHelper.php
File metadata and controls
executable file
·164 lines (140 loc) · 4.33 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
<?php
namespace BaseModule\Helper;
use Zend\Config\Config;
/**
* To different the environment
*
* @copyright Felix Buchheim
* @author Felix Buchheim <hanibal4nothing@gmail.com>
*/
class EnvironmentHelper
{
/**
* @var: string
*/
const PRODUCTION = 'production';
/**
* @var: string
*/
const DEVELOPMENT = 'development';
/**
* @var string
*/
const DISABLE_HARD_ENVIRONMENT = 'disableHardEnvironment';
/**
* @var string
*/
protected static $environment;
/**
* @return string
*/
static public function getEnvironment()
{
return self::$environment;
}
/**
* @return bool
*/
static public function isProduction()
{
return (self::PRODUCTION === self::$environment);
}
/**
* @return bool
*/
static public function isDevelopment()
{
return (self::DEVELOPMENT === self::$environment);
}
/**
* Estimate the environment from Config, Url or ServerAddress
*
* @param Config $oConfig
*
* @return bool
*/
static public function fetchEnvironment(Config $oConfig)
{
self::$environment = null;
$bEnvironmentFetched = false;
if (true === $oConfig->offsetExists('enableSwitchViaUrl') and true === $oConfig->get('enableSwitchViaUrl')) {
$bEnvironmentFetched = self::fetchEnvironmentByUrl();
}
if (false === $bEnvironmentFetched) {
if (true === $oConfig->offsetExists('hardEnvironment') and $oConfig->get('hardEnvironment') !== self::DISABLE_HARD_ENVIRONMENT) {
self::$environment = $oConfig->get('hardEnvironment');
$bEnvironmentFetched = true;
} else {
$bEnvironmentFetched = self::fetchEnvironmentByServer($oConfig);
}
}
if (false === $bEnvironmentFetched) {
self::$environment = $oConfig->get('inDoubt');
}
if (self::$environment !== self::PRODUCTION and self::$environment !== self::DEVELOPMENT) {
throw new \RuntimeException('Coud not estimate environment');
}
self::setErrorReporting($oConfig);
return $bEnvironmentFetched;
}
/**
* Set the errorReporting in the ini
*
* @param Config $oConfig
*
* @return bool
*/
static protected function setErrorReporting(Config $oConfig)
{
$environment = self::$environment;
error_reporting($oConfig->get('environments')->$environment->errorReporting);
ini_set('display_errors', $oConfig->get('environments')->$environment->displayErrors);
return true;
}
/**
* try to fetch the environment by url
*
* @return bool
*/
static protected function fetchEnvironmentByUrl()
{
$sQueryString = (false === empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : '';
$aMatches = array();
$bEnvironment = false;
if (1 === preg_match('/dev=([^&]*)/', $sQueryString, $aMatches)) {
switch ($aMatches[1]) {
case '1':
self::$environment = self::DEVELOPMENT;
$bEnvironment = true;
break;
case '0':
self::$environment = self::PRODUCTION;
$bEnvironment = true;
break;
default:
}
}
return $bEnvironment;
}
/**
* try to fetch the environment by the serverAddress
*
* @param Config $oConfig
*
* @return bool
*/
static protected function fetchEnvironmentByServer(Config $oConfig)
{
$sServerAddress = (false === empty($_SERVER['SERVER_ADDR'])) ? $_SERVER['SERVER_ADDR'] : '';
$bEnvironmentFetched = false;
if (true === $oConfig->offsetExists('devServerAddress') and $oConfig->get('devServerAddress') === $sServerAddress) {
self::$environment = self::DEVELOPMENT;
$bEnvironmentFetched = true;
}
if (true === $oConfig->offsetExists('productionServerAddress') and $oConfig->get('productionServerAddress') === $sServerAddress) {
self::$environment = self::PRODUCTION;
$bEnvironmentFetched = true;
}
return $bEnvironmentFetched;
}
}