-
Notifications
You must be signed in to change notification settings - Fork 11
/
App.php
212 lines (178 loc) · 5.07 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
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
<?php
namespace MABI;
include_once __DIR__ . '/Slim/Slim.php';
include_once __DIR__ . '/Extension.php';
include_once __DIR__ . '/ErrorResponse.php';
include_once __DIR__ . '/DefaultAppErrors.php';
use Illuminate\Cache\ApcStore;
use Illuminate\Cache\ApcWrapper;
use \Slim\Slim;
use Slim\Exception\Stop;
use Illuminate\Cache\FileStore;
use Illuminate\Cache\Repository;
use Illuminate\Filesystem\Filesystem;
Slim::registerAutoloader();
/**
* todo: docs
*/
class App extends Extension {
/**
* @var \Slim\Slim;
*/
protected $slim;
/**
* @return \Slim\Slim
*/
public function getSlim() {
return $this->slim;
}
/**
* @return \Slim\Http\Request
*/
public function getRequest() {
return $this->slim->request();
}
/**
* @return \Slim\Http\Response
*/
public function getResponse() {
return $this->slim->response();
}
/**
* @var App
*/
protected static $singletonApp = NULL;
/**
* @var ErrorResponseDictionary
*/
protected $errorResponseDictionary = NULL;
/**
* @var \Illuminate\Cache\Repository[]
*/
protected $cacheRepositories = array();
/**
* @return \MABI\ErrorResponseDictionary
*/
public function getErrorResponseDictionary() {
return $this->errorResponseDictionary;
}
/**
* todo: docs
*/
static function getSingletonApp() {
if (empty(self::$singletonApp)) {
self::$singletonApp = new App();
}
return self::$singletonApp;
}
/**
* todo: docs
*/
static function clearSingletonApp() {
self::$singletonApp = NULL;
}
public function __construct() {
if (file_exists(__DIR__ . '/middleware')) {
array_push($this->middlewareDirectories, __DIR__ . '/middleware');
}
$this->slim = new Slim();
$this->errorResponseDictionary = new DefaultAppErrors();
parent::__construct($this);
}
public function setMiddlewareDirectories($middlewareDirectories) {
parent::setMiddlewareDirectories(array_merge($this->middlewareDirectories, $middlewareDirectories));
}
/**
* todo: docs
*
* @param $name string
* @param $driver string
* @param $config mixed
*/
function addCacheRepository($name, $driver, $config) {
switch (strtolower($driver)) {
case 'file':
$cacheStore = new FileStore(new Filesystem(), $config['path']);
break;
case 'apc':
$cacheStore = new ApcStore(new ApcWrapper());
break;
// todo: add memcached, etc.
default:
return;
}
$this->cacheRepositories[$name] = new Repository($cacheStore);
}
/**
* @param $name
*
* @return \Illuminate\Cache\Repository
*/
public function getCacheRepository($name) {
return array_key_exists($name, $this->cacheRepositories) ? $this->cacheRepositories[$name] : null;
}
/**
* Returns a JSON array displaying the error to the client and stops execution
*
* Example Error Message Definition:
* array('ERRORDEF_NO_ACCESS' => array('message' => 'No Access', 'code' => 1007, 'httpcode' => 402));
*
* @param $errorKeyOrDefinition string|array
* @param $replacementArray array
*
* @throws \Slim\Exception\Stop
*/
public function returnError($errorKeyOrDefinition, $replacementArray = array()) {
if (is_string($errorKeyOrDefinition)) {
$errorKey = $errorKeyOrDefinition;
}
else {
$errorKeys = array_keys($errorKeyOrDefinition);
$errorKey = $errorKeys[0];
}
$errorResponse = $this->errorResponseDictionary->getErrorResponse($errorKey);
if (empty($errorResponse)) {
$errorResponse = ErrorResponse::FromArray($errorKeyOrDefinition[$errorKey]);
}
$appCode = $errorResponse->getCode();
echo json_encode(array(
'error' => empty($appCode) ? array('message' => $errorResponse->getFormattedMessage($replacementArray)) :
array('code' => $appCode, 'message' => $errorResponse->getFormattedMessage($replacementArray))
));
$this->getResponse()->status($errorResponse->getHttpcode());
throw new Stop($errorResponse->getFormattedMessage($replacementArray));
}
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();
}
}