-
Notifications
You must be signed in to change notification settings - Fork 11
/
Extension.php
279 lines (234 loc) · 6.28 KB
/
Extension.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
<?php
namespace MABI;
include_once __DIR__ . '/DataConnection.php';
include_once __DIR__ . '/ModelLoader.php';
include_once __DIR__ . '/Controller.php';
include_once __DIR__ . '/ControllerLoader.php';
interface Parser {
function Parse($text);
}
/**
* todo: docs
*/
class Extension {
/**
* @var DataConnection[]
*/
protected $dataConnections = array();
/**
* @var ModelLoader[]
*/
protected $modelLoaders = array();
/**
* @var ControllerLoader[]
*/
protected $controllerLoaders = array();
/**
* @var Array
*/
protected $config = array();
/**
* @var Extension[]
*/
protected $extensions = array();
/**
* @var App
*/
protected $app;
/**
* @return \MABI\App
*/
public function getApp() {
return $this->app;
}
/**
* @var string[]
*/
protected $middlewareDirectories = array();
/**
* @param $middlewareDirectories string[]
*/
public function setMiddlewareDirectories($middlewareDirectories) {
$this->middlewareDirectories = $middlewareDirectories;
}
protected $combinedMiddlewareDirectories = array();
protected $loadedMiddleware = array();
/**
* @return string[]
*/
protected function getCombinedMiddlewareDirectories() {
$extensionDirs = array();
foreach ($this->extensions as $extension) {
$extensionDirs = array_merge($extensionDirs, $extension->getCombinedMiddlewareDirectories());
}
return array_merge($extensionDirs, $this->middlewareDirectories);
}
public function loadMiddleware($middlewareFile) {
if(empty($this->combinedMiddlewareDirectories)) {
$this->combinedMiddlewareDirectories = $this->getCombinedMiddlewareDirectories();
}
if(!empty($this->loadedMiddleware[$middlewareFile])) {
return;
}
// Finds the file to include for this middleware using the app's middleware directory listing
foreach ($this->combinedMiddlewareDirectories as $middlewareDirectory) {
if (file_exists($middlewareDirectory . '/' . $middlewareFile)) {
include_once $middlewareDirectory . '/' . $middlewareFile;
$this->loadedMiddleware[$middlewareFile] = $middlewareDirectory . '/' . $middlewareFile;
return;
}
}
throw new \Exception('Could not find middleware file for: ' . $middlewareFile);
}
/**
* @param $app App
*/
public function __construct($app) {
$this->app = $app;
}
/**
* todo: docs
*
* @param $name string
* @param $dataConnection DataConnection
*/
function addDataConnection($name, $dataConnection) {
$this->dataConnections[$name] = $dataConnection;
}
/**
* todo: docs
*
* @param $key
* @param $value
*/
public function setConfig($key, $value) {
$this->config[$key] = $value;
}
/**
* todo: docs
*
* @param $key
*
* @return mixed
*/
public function getConfig($key) {
$extensionConfigs = array();
foreach ($this->extensions as $extension) {
$extensionConfigs = array_merge($extensionConfigs, $extension->config);
}
$configs = array_merge($extensionConfigs, $this->config);
return array_key_exists($key, $configs) ? $configs[$key] : NULL;
}
/**
* todo: docs
*
* @param $modelClass string
*/
function addModel($modelClass) {
// todo: implement
}
/**
* todo: docs
*
* @param $modelLoaders ModelLoader[]
*/
public function setModelLoaders(array $modelLoaders) {
$this->modelLoaders = $modelLoaders;
}
/**
* @param $name
*
* @return DataConnection
*/
public function getDataConnection($name) {
$extensionDataConnections = array();
foreach ($this->extensions as $extension) {
$extensionDataConnections = array_merge($extensionDataConnections, $extension->dataConnections);
}
$dataConnections = array_merge($extensionDataConnections, $this->dataConnections);
return $dataConnections[$name];
}
public function getExtensionModelClasses() {
$modelClasses = array();
foreach ($this->modelLoaders as $modelLoader) {
$loadModelClasses = $modelLoader->loadModels();
foreach ($loadModelClasses as $modelClass) {
$modelClasses[] = $modelClass;
}
}
return $modelClasses;
}
public function getModelClasses() {
$modelClasses = array();
foreach ($this->extensions as $extension) {
$modelClasses = array_merge($modelClasses, $extension->getModelClasses());
}
foreach ($this->modelLoaders as $modelLoader) {
$loadModelClasses = $modelLoader->loadModels();
foreach ($loadModelClasses as $modelClass) {
// allow model overrides
foreach ($modelClasses as $k => $overriddenModel) {
if (ReflectionHelper::stripClassName($modelClass) == ReflectionHelper::stripClassName($overriddenModel)) {
unset($modelClasses[$k]);
break;
}
}
$modelClasses[] = $modelClass;
}
}
return $modelClasses;
}
public function addExtension(Extension $extension) {
$this->extensions[] = $extension;
}
/**
* todo: docs
*
* @param $controllerLoaders ControllerLoader[]
*/
public function setControllerLoaders(array $controllerLoaders) {
$this->controllerLoaders = $controllerLoaders;
}
/**
* todo: docs
*
* @return Controller[]
*/
public function getControllers() {
/**
* @var $controllers Controller[]
*/
$controllers = array();
foreach ($this->extensions as $extension) {
$controllers = array_merge($controllers, $extension->getControllers());
}
foreach ($this->controllerLoaders as $controllerLoader) {
$loadControllers = $controllerLoader->getControllers();
foreach ($loadControllers as $controller) {
// allow controller overrides
foreach ($controllers as $k => $overriddenController) {
if ($controller->getBase() == $overriddenController->getBase()) {
unset($controllers[$k]);
break;
}
}
$controllers[] = $controller;
}
}
return $controllers;
}
/**
* todo: docs
*
* @param Parser $parser
*
* @return array
*/
public function getDocJSON(Parser $parser) {
$docOut = array();
foreach ($this->getControllers() as $controller) {
$docOut['endpoints'][] = $controller->getDocJSON($parser);
}
return $docOut;
}
}