-
Notifications
You must be signed in to change notification settings - Fork 4
/
command.php
executable file
·471 lines (365 loc) · 10.5 KB
/
command.php
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
class TPLNotAllowedHereException extends Exception {}
class AbortCommand extends Exception {}
/*
Abstract Class:
<Command>
Serves as a base class for commands.
*/
abstract class Command
{
// Property: <Command::$aliases>
// Contains aliases for actions.
protected $aliases = array();
// Property: <Command::$objects>
// Object-dependencies used in the current commmand.
protected $objects = array();
// Property: <Command::$template>
// Holds an instance to the Templater class, which takes care of all templateing needs.
protected $template;
// Property: <Command::$request>
// The request object passed to <Command::run>
protected $request;
// Property: <Command::$js>
// Holds an array of JS-packages for the command.
protected $js = array('core');
// Property: <Command::$css>
// Holds an array of CSS-packages for the command
protected $css = array('core');
// Property: <Command::$view>
// The name of the view to include as template.
private $view;
// Property: <Command::$layouts>
// Translation table for layout types. Add a * (start) key for it to be used for every request type
protected $layouts = array();
protected $add_request_to_template = true;
/*
Constructor:
<Command::__construct>
This method should ONLY be overwritten by abstract base classes.
If you want to initialize objects or variables before an action is run, use the <Command::initialize> method instead.
*/
public function __construct()
{
foreach ( $this->objects as $map )
{
$this->{strtolower($map . 'mapper')} = DataMapper::get($map);
}
$this->template = new Templater();
}
/*
Method:
<Command::run>
Runs a method of the current class from the <RequestData>-object passed. To see how the data is formatted
please have a look at <Controller> and <RequestData>.
Parameters:
(RequestData) $request - Information about the request
*/
public final function run(RequestData $request)
{
$this->request = $request;
$view = explode('.', $request->command);
$view = $view[1];
$args = array_slice($request->argv, 1);
$method = (count($args)) ? $args[count($args) - 1] : false;
try {
$this->template->setType("html");
// Call initialize method, if one exists
if ( method_exists($this, 'initialize') )
{
$redirect = call_user_func_array(array($this, 'initialize'), $args);
if ( is_array($redirect) || is_string($redirect) )
{
$url = is_array($redirect) ? Cowl::url($redirect) : $redirect;
return $url;
}
}
// If aliases exists, "reroute" the method
if ( isset($this->aliases[$method]) && method_exists($this, $this->aliases[$method]) )
{
$method = $this->aliases[$method];
}
elseif ( ! $method || $method == 'run' || ! method_exists($this, $method) )
{
$method = 'index';
}
// Ensure that method is public
$reflection = new ReflectionMethod(get_class($this), $method);
if ( ! $reflection->isPublic() )
{
$method = 'index';
}
$request->method = $method;
// Set view to either the base-name of the class, which is default or the name of the method
if ( is_null($this->view) && $this->template->exists('view.' . $method . '.php') )
{
$this->setView($method);
}
else if ( is_null($this->view) )
{
$this->setView($view);
}
// If a global layout type has defined use that
// Set the appropriate layout for the response type
try {
$type = isset($this->layouts['*']) ? $this->layouts['*'] : $request->response_type;
$type = isset($this->layouts[$type]) ? $this->layouts[$type] : $type;
// Check if the user has restricted which layouts can automatically be set
$allowed_types = Current::$config->getOr('allowed_layouts', true);
if ( is_array($allowed_types) && ! in_array($type, $allowed_types) )
{
throw new TPLNotAllowedHereException($type);
}
$this->template->setType($type);
}
catch ( Exception $e )
{
$html_type = isset($this->layouts['html']) ? $this->layouts['html'] : 'html';
$this->template->setType($html_type);
$request->response_type = 'html';
}
if ( $this->add_request_to_template )
{
$this->template->add('request', $request);
}
Current::$request->setInfo('request', $request);
Current::$plugins->hook('commandRun', $this, $method, $request);
// Prepare some stuff before firing the command
$this->requestBegan();
$this->error_occured = false;
// _This_ is where all the magic happens
$ret = call_user_func_array(array($this, $method), $args);
// If an array is returned it is used as pieces for a <Cowl::url> redirect
if ( is_array($ret) || (is_string($ret) && strlen($ret)) )
{
$this->requestEnded();
return is_array($ret) ? Cowl::url($ret) : $ret;
}
}
catch (RegistryMemberNotFoundException $e)
{
$this->error_occured = 400;
header("HTTP/1.1", true, 400);
}
catch (AbortCommand $e)
{
$code = $e->getMessage();
$this->error_occured = $code;
header("HTTP/1.1", true, $code);
}
catch (CowlRedirectCommand $e)
{
$url = $e->getURL();
if ( is_array($url) || (is_string($url) && strlen($url)) )
{
$this->requestEnded();
return is_array($url) ? Cowl::url($url) : $url;
}
}
if ( $this->error_occured )
if ( method_exists($this, 'onError') )
$this->onError();
Current::$plugins->hook('postCommandRun', $this, $method, $request);
// Render the template if not in CLI environment
if ( ! COWL_CLI )
{
if ( ! $this->error_occured )
{
$this->template->render($this->view);
}
else
{
$this->template->renderError($this->error_occured);
}
}
$this->requestEnded();
}
/*
Method:
Command::requestBegan
Prepare some stuff before a command is fired.
*/
private function requestBegan()
{
try {
$info = Current::$store->get('cowl.flash');
Current::$request->setInfo('flash', $info['flash']);
Current::$request->setInfo('flashError', $info['error']);
Current::$request->setInfo('flashSuccess', $info['success']);
Current::$request->setInfo('flashNotice', $info['notice']);
}
catch ( RegistryMemberNotFoundException $e ) {}
}
/*
Method:
Command::requestEnded
Clean-up and other actions for when the request is finished. Be it redirect or after a template render.
*/
private function requestEnded()
{
$flash = Current::$request->getInfo('flash');
$error = Current::$request->getInfo('flashError');
$notice = Current::$request->getInfo('flashNotice');
$success = Current::$request->getInfo('flashSuccess');
Current::$store->set('cowl.flash', array(
'flash' => $flash,
'error' => $error,
'notice' => $notice,
'success' => $success
));
}
/*
Method:
<Command::setTemplateDir>
Sets the <Templater::$template_dir> using <Templater::setDir>
Parameters:
$dir - The directory in which the template resides.
*/
public function setTemplateDir($dir)
{
$this->template->setDir($dir);
}
/*
Method:
<Command::setView>
Sets the view, and path to view, for <Templater::render>.
Parameters:
$path - Path to view, directory etc.
$to - ...
$view - The last argument is the name of the view, without view....php
*/
public function setView()
{
$args = func_get_args();
$path = implode('/', array_slice($args, 0, -1));
$path .= 'view.' . end($args) . '.php';
$this->view = $path;
}
// Method: <Command::getJS>
// Returns <Command::$js>
public function getJS() { return $this->js; }
// Method: <Command::getCSS>
// Returns <Command::$css>
public function getCSS() { return $this->css; }
/*
Method:
<Command::assign>
Alias for <Command::$template::add>. See <Templater::add> for more details.
*/
public function assign()
{
$args = func_get_args();
return call_user_func_array(array($this->template, 'add'), $args);
}
/*
Method:
Command::flash
Flash a message on the next renderinf of a template. Used for messages on a successful POST request.
Use the standard helper method <flash> to output these flashes. There are several types of flashes:
- flash
- flashError
- flashNotice
- flashSuccess
For information about them see each method in the <Command> class.
Parameters:
(string or array) $message - The message to flash
*/
public function flash($message)
{
$this->setFlashMessage('flash', $message);
}
/*
Method:
Command::flashError
Flash a message with an error class. For more information see: <Command::flash>
*/
public function flashError($message)
{
$this->setFlashMessage('flashError', $message);
}
/*
Method:
Command::flashNotice
Flash a message with a notice class. For more information see: <Command::flash>
*/
public function flashNotice($message)
{
$this->setFlashMessage('flashNotice', $message);
}
/*
Method:
Command::flashSuccess
Flash a message with an success class. For more information see: <Command::flash>
*/
public function flashSuccess($message)
{
$this->setFlashMessage('flashSuccess', $message);
}
/*
Method:
Command::setFlashMessage
Set a flash message using specified type.
Parameters:
$type - The type of flash. It's basically just a key that is stored in the current request
$message - Either array or string, if array it is recursively added
*/
private function setFlashMessage($type, $message)
{
if ( is_array($message) )
{
foreach ( $message as $mess )
{
$this->setFlashMessage($type, $mess);
}
}
else
{
Current::$request->setInfo($type . '[]', $message);
}
}
/*
Method:
debug
Set a debug environment
*/
protected function debug()
{
header('Content-type: text/plain');
// This is hard-coded as mysql, because the only supported DB is mysql.
// Change this if that ever changes.
Current::db('mysql')->output_query = true;
}
/*
Method:
abort
Set error flag with error code specified. Will stop execution of current command, if not in try {} catch {} block.
*/
public function abort($code)
{
$this->error_occured = $code;
header("HTTP/1.1", true, $code);
throw new AbortCommand($code);
}
/*
Method:
redirect
Redirect the user without having to return anything.
*/
public function redirect($url)
{
throw new CowlRedirectCommand($url);
}
public abstract function index();
}
class CowlRedirectCommand extends Exception
{
private $url;
public function __construct($url)
{
$this->url = $url;
}
public function getURL()
{
return $this->url;
}
}