1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package application |
4
|
|
|
* @subpackage controllers |
5
|
|
|
* @author marius orcsik <[email protected]> |
6
|
|
|
* @date 09.08.30 |
7
|
|
|
*/ |
8
|
|
|
namespace vsc\application\controllers; |
9
|
|
|
|
10
|
|
|
use vsc\application\processors\ErrorProcessor; |
11
|
|
|
use vsc\application\processors\ProcessorA; |
12
|
|
|
use vsc\application\sitemaps\ClassMap; |
13
|
|
|
use vsc\application\sitemaps\ContentTypeMappingInterface; |
14
|
|
|
use vsc\application\sitemaps\MappingA; |
15
|
|
|
use vsc\domain\models\EmptyModel; |
16
|
|
|
use vsc\domain\models\ErrorModel; |
17
|
|
|
use vsc\domain\models\ModelA; |
18
|
|
|
use vsc\infrastructure\Object; |
19
|
|
|
use vsc\infrastructure\vsc; |
20
|
|
|
use vsc\presentation\responses\ExceptionResponseRedirect; |
21
|
|
|
use vsc\presentation\responses\HttpResponseA; |
22
|
|
|
use vsc\presentation\responses\HttpResponseType; |
23
|
|
|
use vsc\presentation\views\ExceptionView; |
24
|
|
|
use vsc\presentation\requests\HttpRequestA; |
25
|
|
|
use vsc\presentation\views\ViewA; |
26
|
|
|
use vsc\ExceptionPath; |
27
|
|
|
use vsc\presentation\responses\ExceptionResponse; |
28
|
|
|
|
29
|
|
|
abstract class FrontControllerA extends Object { |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $sTemplatePath; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ContentTypeMappingInterface |
37
|
|
|
*/ |
38
|
|
|
private $oCurrentMap; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ViewA |
42
|
|
|
*/ |
43
|
|
|
private $oView; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @returns ViewA |
47
|
|
|
*/ |
48
|
|
|
abstract public function getDefaultView(); |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @throws ExceptionView |
52
|
|
|
* @returns ClassMap |
53
|
|
|
*/ |
54
|
6 |
|
public function getMap() { |
55
|
6 |
|
if (!ClassMap::isValid($this->oCurrentMap)) { |
|
|
|
|
56
|
|
|
$Mirror = new \ReflectionClass($this); |
|
|
|
|
57
|
|
|
$this->oCurrentMap = new ClassMap($Mirror->getName(), '.*'); |
58
|
|
|
} |
59
|
6 |
|
return $this->oCurrentMap; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ContentTypeMappingInterface $oMap |
64
|
|
|
*/ |
65
|
5 |
|
public function setMap(ContentTypeMappingInterface $oMap) { |
66
|
5 |
|
$this->oCurrentMap = $oMap; |
67
|
5 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $sIncPath |
71
|
|
|
* @return bool |
72
|
|
|
* @throws ExceptionController |
73
|
|
|
*/ |
74
|
2 |
|
public function setTemplatePath($sIncPath) { |
75
|
2 |
|
if (is_dir($sIncPath)) { |
76
|
1 |
|
$this->sTemplatePath = $sIncPath; |
77
|
1 |
|
return true; |
78
|
|
|
} else { |
79
|
1 |
|
throw new ExceptionController('The template path [' . $sIncPath . '] is not a valid folder.'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param HttpRequestA $oRequest |
85
|
|
|
* @param ProcessorA $oProcessor |
|
|
|
|
86
|
|
|
* @return ViewA |
87
|
|
|
* @throws ExceptionPath |
88
|
|
|
*/ |
89
|
2 |
|
protected function loadView($oRequest, $oProcessor = null) { |
90
|
|
|
// we didn't set any special view |
91
|
|
|
// this means that the developer needs to provide his own views |
92
|
2 |
|
$oView = $this->getView(); |
|
|
|
|
93
|
2 |
|
$oMyMap = $this->getMap(); |
94
|
|
|
|
95
|
2 |
|
if (ProcessorA::isValid($oProcessor)) { |
96
|
1 |
|
$oProcessor->init(); |
|
|
|
|
97
|
1 |
|
$oModel = $oProcessor->handleRequest($oRequest); |
98
|
|
|
/* @var ClassMap $oMap */ |
99
|
1 |
|
$oMap = $oProcessor->getMap(); |
100
|
1 |
|
if (MappingA::isValid($oMap)) { |
101
|
1 |
|
if (MappingA::isValid($oMyMap)) { |
102
|
1 |
|
$oMap->merge($oMyMap); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
// setting the processor map |
106
|
1 |
|
$oView->setMap($oMap); |
107
|
|
|
try { |
108
|
1 |
|
if ((ClassMap::isValid($oMap) && !$oMap->isStatic() && !$oMyMap->isStatic()) && ClassMap::isValid($oMyMap)) { |
|
|
|
|
109
|
1 |
|
$oView->setMainTemplate( |
110
|
1 |
|
$oMyMap->getMainTemplatePath() . |
111
|
1 |
|
$oView->getViewFolder() . DIRECTORY_SEPARATOR . |
112
|
1 |
|
$oMyMap->getMainTemplate() |
113
|
|
|
); |
114
|
|
|
} |
115
|
1 |
|
} catch (ExceptionPath $e) { |
116
|
|
|
// fallback to html5 |
117
|
|
|
// @todo verify main template path and main template exist |
118
|
1 |
|
$oView->setMainTemplate($oMyMap->getMainTemplatePath() . DIRECTORY_SEPARATOR . 'html5' . DIRECTORY_SEPARATOR . $oMyMap->getMainTemplate()); |
119
|
|
|
} |
120
|
1 |
|
if (!ModelA::isValid($oModel)) { |
121
|
|
|
$oModel = new EmptyModel(); |
122
|
|
|
if (!ClassMap::isValid($oMap) || $oMap->getTitle() == '') { |
123
|
|
|
$oModel->setPageTitle('Warning'); |
124
|
|
|
} |
125
|
|
|
$oModel->setPageContent('Warning: the processor didn\'t return a valid model. This is probably an error'); |
126
|
|
|
} |
127
|
1 |
|
$oView->setModel($oModel); |
128
|
|
|
} |
129
|
2 |
|
return $oView; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param HttpRequestA $oRequest |
134
|
|
|
* @param ProcessorA $oProcessor |
|
|
|
|
135
|
|
|
* @throws ExceptionPath |
136
|
|
|
* @throws ExceptionResponse |
137
|
|
|
* @throws ExceptionView |
138
|
|
|
* @returns HttpResponseA |
139
|
|
|
*/ |
140
|
2 |
|
public function getResponse(HttpRequestA $oRequest, $oProcessor = null) { |
141
|
2 |
|
$oResponse = vsc::getEnv()->getHttpResponse(); |
142
|
2 |
|
if ($oResponse->getStatus() == 0) { |
143
|
|
|
$oResponse->setStatus(HttpResponseType::OK); |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
$oModel = null; |
|
|
|
|
147
|
|
|
try { |
148
|
2 |
|
$oView = $this->loadView($oRequest, $oProcessor); |
149
|
2 |
|
$oResponse->setView($oView); |
150
|
2 |
|
if (ProcessorA::isValid($oProcessor)) { |
151
|
1 |
|
$oMap = $oProcessor->getMap(); |
|
|
|
|
152
|
1 |
|
if (MappingA::isValid($oMap)) { |
153
|
1 |
|
$aHeaders = $oMap->getHeaders(); |
154
|
1 |
|
if (count($aHeaders) > 0) { |
155
|
|
|
foreach ($aHeaders as $sName => $sHeader) { |
156
|
|
|
$oResponse->addHeader($sName, $sHeader); |
157
|
|
|
} |
158
|
|
|
} |
159
|
1 |
|
$iProcessorSetStatus = $oMap->getResponseStatus(); |
160
|
1 |
|
if (HttpResponseType::isValidStatus($iProcessorSetStatus)) { |
161
|
|
|
$oResponse->setStatus($iProcessorSetStatus); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
2 |
|
return $oResponse; |
166
|
|
|
} catch (ExceptionResponseRedirect $e) { |
167
|
|
|
$oResponse->setStatus($e->getRedirectCode()); |
168
|
|
|
$oResponse->setLocation($e->getLocation()); |
169
|
|
|
|
170
|
|
|
return $oResponse; |
171
|
|
|
} catch (\Exception $e) { |
172
|
|
|
// we had error in the controller |
173
|
|
|
// @todo make more error processors |
174
|
|
|
return $this->getErrorResponse($e, $oRequest); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param \Exception $e |
180
|
|
|
* @param HttpRequestA $oRequest |
|
|
|
|
181
|
|
|
* @return HttpResponseA |
182
|
|
|
* @throws ExceptionPath |
183
|
|
|
* @throws ExceptionResponse |
184
|
|
|
*/ |
185
|
1 |
|
public function getErrorResponse(\Exception $e, $oRequest = null) { |
186
|
1 |
|
$oResponse = vsc::getEnv()->getHttpResponse(); |
187
|
|
|
|
188
|
1 |
|
$oProcessor = new ErrorProcessor($e); |
189
|
|
|
|
190
|
|
|
/* @var ClassMap $oMyMap */ |
191
|
1 |
|
$oMyMap = $this->getMap(); |
192
|
|
|
|
193
|
1 |
|
$oMyMap->setMainTemplatePath(VSC_RES_PATH . 'templates'); |
194
|
1 |
|
$oMyMap->setMainTemplate('main.php'); |
195
|
|
|
|
196
|
1 |
|
if (!HttpRequestA::isValid($oRequest)) { |
197
|
1 |
|
$oRequest = vsc::getEnv()->getHttpRequest(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** @var ErrorModel $oModel */ |
201
|
1 |
|
$oModel = $oProcessor->handleRequest($oRequest); |
|
|
|
|
202
|
|
|
|
203
|
1 |
|
$iCode = $oModel->getHttpStatus(); |
204
|
1 |
|
if (HttpResponseType::isValidStatus($iCode)) { |
205
|
1 |
|
$oResponse->setStatus($iCode); |
206
|
|
|
} else { |
207
|
|
|
$oResponse->setStatus(500); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// we didn't set any special view |
211
|
|
|
// this means that the developer needs to provide his own views |
212
|
1 |
|
$oView = $this->getView(); |
213
|
|
|
|
214
|
|
|
/* @var ClassMap $oMap */ |
215
|
1 |
|
$oMap = $oProcessor->getMap(); |
216
|
1 |
|
$oMap->merge($oMyMap); |
217
|
1 |
|
$oProcessorResponse = $oMap->getResponse(); |
218
|
|
|
|
219
|
1 |
|
if (HttpResponseA::isValid($oProcessorResponse)) { |
220
|
|
|
$oResponse = $oProcessorResponse; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// setting the processor map |
224
|
1 |
|
$oView->setMap($oMap); |
225
|
1 |
|
if (ClassMap::isValid($oMyMap)) { |
226
|
1 |
|
$oView->setMainTemplate($oMyMap->getMainTemplatePath() . DIRECTORY_SEPARATOR . $oView->getViewFolder() . DIRECTORY_SEPARATOR . $oMyMap->getMainTemplate()); |
227
|
|
|
} |
228
|
1 |
|
$oView->setModel($oModel); |
229
|
|
|
|
230
|
1 |
|
$oResponse->setView($oView); |
231
|
|
|
|
232
|
1 |
|
$aHeaders = $oMap->getHeaders(); |
233
|
1 |
|
if (count($aHeaders) > 0) { |
234
|
|
|
foreach ($aHeaders as $sName => $sHeader) { |
235
|
|
|
$oResponse->addHeader($sName, $sHeader); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
return $oResponse; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @returns ViewA |
244
|
|
|
* @throws ExceptionView |
245
|
|
|
*/ |
246
|
4 |
|
public function getView() { |
|
|
|
|
247
|
4 |
|
if (ViewA::isValid($this->oView)) { |
248
|
|
|
return $this->oView; |
249
|
|
|
} |
250
|
4 |
|
$oMapView = $this->getMap()->getView(); |
|
|
|
|
251
|
4 |
|
if (ViewA::isValid($oMapView)) { |
252
|
2 |
|
return $oMapView; |
253
|
|
|
} |
254
|
2 |
|
$this->oView = $this->getDefaultView(); |
255
|
2 |
|
return $this->oView; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.