|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package infrastructure |
|
4
|
|
|
* @author marius orcsik <[email protected]> |
|
5
|
|
|
* @date 09.08.31 |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace vsc\infrastructure; |
|
8
|
|
|
|
|
9
|
|
|
use vsc\application\dispatchers\DispatcherA; |
|
10
|
|
|
use vsc\application\dispatchers\HttpDispatcherA; |
|
11
|
|
|
use vsc\application\dispatchers\RwDispatcher; |
|
12
|
|
|
use vsc\application\sitemaps\ModuleMap; |
|
13
|
|
|
use vsc\application\sitemaps\SiteMapA; |
|
14
|
|
|
use vsc\presentation\requests\HttpRequestA; |
|
15
|
|
|
use vsc\presentation\requests\RawHttpRequest; |
|
16
|
|
|
use vsc\presentation\requests\RwHttpRequest; |
|
17
|
|
|
use vsc\presentation\responses\HttpResponse; |
|
18
|
|
|
use vsc\presentation\responses\HttpResponseA; |
|
19
|
|
|
|
|
20
|
|
|
class vsc extends BaseObject { |
|
21
|
|
|
/** |
|
22
|
|
|
* @var vsc |
|
23
|
|
|
*/ |
|
24
|
|
|
static private $oInstance; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var HttpRequestA |
|
28
|
|
|
*/ |
|
29
|
|
|
private $oRequest = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var HttpResponseA |
|
33
|
|
|
*/ |
|
34
|
|
|
private $oResponse = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var DispatcherA |
|
38
|
|
|
*/ |
|
39
|
|
|
private $oDispatcher; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param vsc $envObject |
|
43
|
|
|
*/ |
|
44
|
19 |
|
public static function setInstance(vsc $envObject) { |
|
45
|
19 |
|
self::$oInstance = $envObject; |
|
46
|
19 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return vsc |
|
50
|
|
|
*/ |
|
51
|
26 |
|
public static function getEnv() { |
|
52
|
26 |
|
if (!(vsc::isValid(self::$oInstance))) { |
|
53
|
|
|
self::$oInstance = new self(); |
|
54
|
|
|
} |
|
55
|
26 |
|
return self::$oInstance; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param HttpRequestA $oRequest |
|
60
|
|
|
*/ |
|
61
|
18 |
|
public function setHttpRequest(HttpRequestA $oRequest) { |
|
62
|
18 |
|
if (HttpRequestA::isValid($oRequest)) { |
|
63
|
18 |
|
$this->oRequest = $oRequest; |
|
64
|
|
|
} |
|
65
|
18 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @returns HttpRequestA |
|
69
|
|
|
*/ |
|
70
|
20 |
|
public function getHttpRequest() { |
|
71
|
20 |
|
if (is_null($this->oRequest)) { |
|
72
|
|
|
// @todo FIX this ugly stuff |
|
73
|
|
|
if (array_key_exists('CONTENT_TYPE', $_SERVER) && strlen($_SERVER['CONTENT_TYPE']) > 0) { |
|
74
|
|
|
$this->oRequest = new RawHttpRequest(); |
|
75
|
|
|
} else { |
|
76
|
|
|
$this->oRequest = new RwHttpRequest(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
20 |
|
return $this->oRequest; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param HttpResponseA $oResponse |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function setHttpResponse(HttpResponseA $oResponse) { |
|
86
|
1 |
|
if (HttpResponseA::isValid($oResponse) && (is_null($this->oResponse) || get_class($this->oResponse) != get_class($oResponse))) { |
|
87
|
1 |
|
$this->oResponse = $oResponse; |
|
88
|
|
|
} |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @returns HttpResponseA |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function getHttpResponse() { |
|
95
|
1 |
|
if (is_null($this->oResponse)) { |
|
96
|
1 |
|
$this->oResponse = new HttpResponse(); |
|
97
|
|
|
} |
|
98
|
1 |
|
return $this->oResponse; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param HttpDispatcherA $oDispatcher |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function setDispatcher($oDispatcher) { |
|
105
|
1 |
|
if (DispatcherA::isValid($oDispatcher)) { |
|
106
|
1 |
|
$this->oDispatcher = $oDispatcher; |
|
107
|
|
|
} |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @returns HttpDispatcherA |
|
112
|
|
|
*/ |
|
113
|
3 |
|
public function getDispatcher() { |
|
114
|
3 |
|
if (!HttpDispatcherA::isValid($this->oDispatcher)) { |
|
115
|
1 |
|
$this->oDispatcher = new RwDispatcher(); |
|
116
|
|
|
} |
|
117
|
3 |
|
return $this->oDispatcher; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return SiteMapA |
|
122
|
|
|
*/ |
|
123
|
2 |
|
public function getSiteMap() { |
|
124
|
2 |
|
return $this->getDispatcher()->getSiteMap(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return boolean |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function isDevelopment() { |
|
131
|
|
|
return ( |
|
132
|
1 |
|
self::isCli() || ( |
|
133
|
|
|
stristr($_SERVER['REMOTE_ADDR'], '127.0.0.1') != false || |
|
|
|
|
|
|
134
|
1 |
|
stristr($_SERVER['REMOTE_ADDR'], '192.168') != false |
|
|
|
|
|
|
135
|
|
|
) |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
4 |
|
protected function _isCli() { |
|
143
|
4 |
|
return (php_sapi_name() == 'cli'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
5 |
|
public static function isCli() { |
|
150
|
5 |
|
return self::getEnv()->_isCli(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public static function name() { |
|
157
|
1 |
|
return 'V<sup>S<sup>C</sup></sup>'; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* returns an end of line, based on the environment |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
1 |
|
public static function nl() { |
|
165
|
1 |
|
return self::isCli() ? "\n" : '<br/>' . "\n"; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param array ...$values |
|
170
|
|
|
* @return string |
|
|
|
|
|
|
171
|
|
|
*/ |
|
172
|
1 |
|
public static function d(...$values) { |
|
173
|
1 |
|
$output = ''; |
|
174
|
1 |
|
for ($i = 0; $i < ob_get_level(); $i++) { |
|
175
|
|
|
// cleaning the buffers |
|
176
|
1 |
|
ob_end_clean(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
if (!self::isCLi() && self::getEnv()->getHttpRequest()->accepts('application/json')) { |
|
180
|
|
|
$output = json_encode($values); |
|
181
|
1 |
|
} elseif (self::isCLi() || self::getEnv()->getHttpRequest()->accepts('text/html')) { |
|
182
|
1 |
|
foreach ($values as $object) { |
|
183
|
1 |
|
ob_start(); |
|
184
|
1 |
|
var_dump($object); |
|
|
|
|
|
|
185
|
1 |
|
$output .= ob_get_clean(); |
|
186
|
|
|
|
|
187
|
1 |
|
if (!self::isCli()) { |
|
188
|
1 |
|
$output .= '<hr/>' . "\n"; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
1 |
|
if (!stristr($_SERVER['PHP_SELF'], 'phpunit')) { |
|
194
|
|
|
ob_start(); |
|
195
|
|
|
debug_print_backtrace(); |
|
196
|
|
|
$output .= ob_get_clean(); |
|
197
|
|
|
|
|
198
|
|
|
if (self::isCLi() || self::getEnv()->getHttpRequest()->accepts('application/json')) { |
|
199
|
|
|
echo StringUtils::stripTags(StringUtils::br2nl($output)); |
|
200
|
|
|
} elseif (self::getEnv()->getHttpRequest()->accepts('text/html')) { |
|
201
|
|
|
echo '<pre>' . $output . '</pre>'; |
|
202
|
|
|
} else { |
|
203
|
|
|
echo StringUtils::stripTags(StringUtils::br2nl($output)); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
exit (); |
|
207
|
|
|
} else { |
|
208
|
1 |
|
return $output; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
1 |
|
public static function getIncludePaths() { |
|
213
|
1 |
|
return explode(PATH_SEPARATOR, get_include_path()); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @returns ModuleMap |
|
218
|
|
|
*/ |
|
219
|
1 |
|
public function getCurrentModuleMap() { |
|
|
|
|
|
|
220
|
1 |
|
return $this->getDispatcher()->getCurrentModuleMap(); |
|
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
1 |
|
public static function getPaths() { |
|
224
|
1 |
|
return explode(PATH_SEPARATOR, get_include_path()); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|