forked from prolificinteractive/mabi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.php
144 lines (117 loc) · 3.29 KB
/
App.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
<?php
namespace MABI;
include_once __DIR__ . '/Slim/Slim.php';
include_once __DIR__ . '/Extension.php';
use \Slim\Slim;
use Slim\Exception\Stop;
Slim::registerAutoloader();
/**
* todo: docs
*/
class App extends Extension {
/**
* @var \Slim\Slim;
*/
protected $slim;
/**
* @return \Slim\Slim
*/
public function getSlim() {
return $this->slim;
}
public function getRequest() {
return $this->slim->request();
}
public function getResponse() {
return $this->slim->response();
}
/**
* @var App
*/
protected static $singletonApp = NULL;
/**
* todo: docs
*/
static function getSingletonApp() {
if (empty(self::$singletonApp)) {
self::$singletonApp = new App();
}
return self::$singletonApp;
}
static function clearSingletonApp() {
self::$singletonApp = NULL;
}
public function __construct() {
if (file_exists(__DIR__ . '/middleware')) {
array_push($this->middlewareDirectories, __DIR__ . '/middleware');
}
$this->slim = new Slim();
parent::__construct($this);
}
/**
* Returns a JSON array displaying the error to the client and stops execution
*
* Example Error Message Definition:
* define('ERRORDEF_NO_ACCESS', array('message' => 'No Access', 'code' => 1007, 'httpcode' => 402));
*
* @param $message string|array
* @param $httpStatusCode int|null
* @param $applicationErrorCode int|null
*
* @throws \Slim\Exception\Stop
* @throws \Exception
*/
public function returnError($message, $httpStatusCode = NULL, $applicationErrorCode = NULL) {
if (is_array($message)) {
if (array_key_exists('message', $message) && array_key_exists('httpcode', $message) &&
array_key_exists('code', $message)
) {
$message = $message['message'];
$httpStatusCode = $message['httpcode'];
$applicationErrorCode = $message['code'];
}
else {
throw new \Exception('Improper error message definition');
}
}
echo json_encode(array(
'error' => empty($applicationErrorCode) ? array('message' => $message) :
array('code' => $applicationErrorCode, 'message' => $message)
));
$this->getResponse()->status($httpStatusCode);
throw new Stop($message);
}
public function errorHandler($e) {
$this->slim->getLog()->error($e);
$this->getResponse()->status(500);
echo json_encode(array(
'error' => array('code' => 1020, 'message' => 'A system error occurred')
));
}
public function run() {
foreach ($this->getControllers() as $controller) {
$controller->loadRoutes($this->slim);
}
if (!$this->slim->config('debug')) {
$this->slim->error(array($this, 'errorHandler'));
}
$this->slim->run();
}
public function call() {
foreach ($this->getControllers() as $controller) {
$controller->loadRoutes($this->slim);
}
if (!$this->slim->config('debug')) {
$this->slim->error(array($this, 'errorHandler'));
}
$this->slim->call();
}
public function getIOSModel() {
$iosModel = IOSModelInterpreter::getIOSDataModel();
foreach ($this->getModelClasses() as $modelClass) {
$model = call_user_func($modelClass . '::init', $this);
IOSModelInterpreter::addModel($iosModel, $model);
}
return $iosModel->asXML();
}
}