|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Find and execute the best solution for a request |
|
5
|
|
|
* |
|
6
|
|
|
* PHP Version 5 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Core |
|
9
|
|
|
* @package Router |
|
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
11
|
|
|
* @copyright 2013 cSphere Team |
|
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
13
|
|
|
* @link http://www.csphere.eu |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
namespace csphere\core\router; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class controller knows best what to do with the request |
|
20
|
|
|
* |
|
21
|
|
|
* @category Core |
|
22
|
|
|
* @package Router |
|
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
24
|
|
|
* @copyright 2013 cSphere Team |
|
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
26
|
|
|
* @link http://www.csphere.eu |
|
27
|
|
|
**/ |
|
28
|
|
|
|
|
29
|
|
|
class Controller |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Store plugin with target to use |
|
33
|
|
|
**/ |
|
34
|
|
|
private $_target = ''; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Store if the target is a box |
|
38
|
|
|
**/ |
|
39
|
|
|
private $_box = false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Store if the request expects a json object |
|
43
|
|
|
**/ |
|
44
|
|
|
private $_xhr = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Creates a new router for executing a destination |
|
48
|
|
|
* |
|
49
|
|
|
* @param boolean $search Set this to true to auto discover the target |
|
50
|
|
|
* |
|
51
|
|
|
* @return \csphere\core\router\Controller |
|
52
|
|
|
**/ |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
public function __construct($search = false) |
|
55
|
|
|
{ |
|
56
|
|
|
// Check for xhr mode that needs a json object |
|
57
|
|
|
$xhr = \csphere\core\http\Input::get('get', 'xhr'); |
|
58
|
|
|
|
|
59
|
|
|
if ($xhr == 1) { |
|
60
|
|
|
|
|
61
|
|
|
$this->_xhr = true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Check if search is enabled |
|
65
|
|
|
if ($search == true) { |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
$this->_search(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Execute the target plugin target |
|
73
|
|
|
* |
|
74
|
|
|
* @param boolean $skip Whether to skip target execution |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
**/ |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
public function execute($skip = false) |
|
80
|
|
|
{ |
|
81
|
|
|
// Check if response was already send |
|
82
|
|
|
$status = \csphere\core\http\Response::status(); |
|
83
|
|
|
|
|
84
|
|
|
if ($status === false) { |
|
85
|
|
|
|
|
86
|
|
|
// If skip is true the view was already filled with content |
|
87
|
|
|
if ($skip === false) { |
|
88
|
|
|
|
|
89
|
|
|
\csphere\core\router\Sandbox::light($this->_target); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Get content from view |
|
93
|
|
|
$loader = \csphere\core\service\Locator::get(); |
|
94
|
|
|
$view = $loader->load('view'); |
|
95
|
|
|
$content = $view->assemble($this->_xhr, $this->_box); |
|
96
|
|
|
|
|
97
|
|
|
// Tell response whether to use compression |
|
98
|
|
|
$zlib = false; |
|
99
|
|
|
|
|
100
|
|
|
if ($skip == false) { |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$zlib = (boolean)$view->getOption('zlib'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
\csphere\core\http\Response::compression($zlib); |
|
106
|
|
|
|
|
107
|
|
|
// Send content to response |
|
108
|
|
|
\csphere\core\http\Response::send($content); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Gets a target action or box of a plugin |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $plugin Plugin name |
|
116
|
|
|
* @param string $action Action name |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
**/ |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
public function target($plugin, $action) |
|
122
|
|
|
{ |
|
123
|
|
|
$checks = new \csphere\core\plugins\Checks($plugin); |
|
124
|
|
|
$checks->setRoute($action); |
|
125
|
|
|
|
|
126
|
|
|
// Keep initial action while allowing changes to it |
|
127
|
|
|
$new_action = $action; |
|
128
|
|
|
|
|
129
|
|
|
// Fallback if action does not exist |
|
130
|
|
|
if ($checks->existance() == false) { |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
$new_action = 'dispatch'; |
|
133
|
|
|
$checks->setRoute($new_action); |
|
134
|
|
|
|
|
135
|
|
|
// Reset fallback if no plugin dispatcher was found |
|
136
|
|
|
if ($checks->existance() == false) { |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
$new_action = $action; |
|
139
|
|
|
$checks->setRoute($new_action); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Inform template engine about the target |
|
144
|
|
|
\csphere\core\template\Hooks::route($plugin, $new_action); |
|
145
|
|
|
|
|
146
|
|
|
$target = $checks->result(); |
|
147
|
|
|
|
|
148
|
|
|
return $target; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Gets a target action or box of a plugin |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
**/ |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
private function _search() |
|
158
|
|
|
{ |
|
159
|
|
|
|
|
160
|
|
|
$request=self::parseRequestAction(); |
|
161
|
|
|
|
|
162
|
|
|
$plugin=$request['plugin']; |
|
163
|
|
|
$action=$request['action']; |
|
164
|
|
|
$box=$request['box']; |
|
165
|
|
|
|
|
166
|
|
|
// Fetch defaults if no plugin is given |
|
167
|
|
|
if (!empty($box)) { |
|
168
|
|
|
|
|
169
|
|
|
$this->_box = true; |
|
170
|
|
|
|
|
171
|
|
|
// Special case for box requests |
|
172
|
|
|
$checks = new \csphere\core\plugins\Checks($plugin); |
|
173
|
|
|
$checks->setRoute($box, true); |
|
|
|
|
|
|
174
|
|
|
$this->_target = $checks->result(); |
|
175
|
|
|
|
|
176
|
|
|
} else { |
|
177
|
|
|
|
|
178
|
|
|
$this->_target = $this->target($plugin, $action); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get the current requested plugin, action and box |
|
184
|
|
|
* |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
public static function parseRequestAction() |
|
189
|
|
|
{ |
|
190
|
|
|
$box = \csphere\core\http\Input::get('get', 'box'); |
|
191
|
|
|
$plugin = \csphere\core\http\Input::get('get', 'plugin'); |
|
192
|
|
|
$action = \csphere\core\http\Input::get('get', 'action'); |
|
193
|
|
|
|
|
194
|
|
|
$request=[ |
|
195
|
|
|
'plugin'=>'', |
|
196
|
|
|
'action'=>'', |
|
197
|
|
|
'box'=>'' |
|
198
|
|
|
]; |
|
199
|
|
|
|
|
200
|
|
|
// Read config file |
|
201
|
|
|
$conf = new \csphere\core\init\Config(); |
|
202
|
|
|
$config = $conf->get(); |
|
203
|
|
|
|
|
204
|
|
|
// Check for favicon requests |
|
205
|
|
|
if ($plugin == 'favicon.ico') { |
|
206
|
|
|
$plugin = ''; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
if (empty($config['default']['action'])) { |
|
210
|
|
|
$config['default']['action']="list"; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
if (empty($plugin)) { |
|
214
|
|
|
|
|
215
|
|
|
if (empty($config['default']['plugin'])) { |
|
216
|
|
|
$config['default']['plugin']="default"; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$request['plugin']=$config['default']['plugin']; |
|
220
|
|
|
|
|
221
|
|
|
$request['action']=$config['default']['action']; |
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
} elseif (!empty($box)) { |
|
225
|
|
|
$request['plugin']=$plugin; |
|
226
|
|
|
$request['box']=$box; |
|
227
|
|
|
} else { |
|
228
|
|
|
|
|
229
|
|
|
$request['plugin']=$plugin; |
|
230
|
|
|
|
|
231
|
|
|
if (empty($action)) { |
|
232
|
|
|
$request['action']=$config['default']['action']; |
|
233
|
|
|
} else { |
|
234
|
|
|
$request['action']=$action; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
return $request; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.