-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModuleJson.php
More file actions
executable file
·130 lines (106 loc) · 4.35 KB
/
ModuleJson.php
File metadata and controls
executable file
·130 lines (106 loc) · 4.35 KB
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
<?php
namespace Level2\Router\Config;
class ModuleJson implements \Level2\Router\Rule {
private $dice;
private $moduleDir;
private $configFile;
private $request;
public function __construct(\Dice\Dice $dice, \Level2\Core\Request $request, $moduleDir = 'Modules', $configFile = 'manifest.json') {
$this->dice = $dice;
$this->moduleDir = $moduleDir;
$this->configFile = $configFile;
$this->request = $request;
}
public function find(array $route) {
if (count($route) == 0 || $route[0] == '') return false;
$config = $this->getConfig($route);
array_shift($route);
$method = $this->request->server('REQUEST_METHOD');
if (empty($route[0]) || !isset($config[$method][$route[0]])) $routeName = $config[$method]['defaultRoute'] ?? 'index';
else $routeName = array_shift($route);
if (isset($config[$method][$routeName])) {
$matchedRoute = $config[$method][$routeName];
// This allows POST to inherit from GET if "inherit" : "GET" is set (or vice versa)
if (isset($matchedRoute['inherit']) && isset($config[$matchedRoute['inherit']][$routeName])) {
$inheritMethod = $matchedRoute['inherit'];
$matchedRoute = array_merge($config[$inheritMethod][$routeName], $matchedRoute);
}
}
else return false;
return $this->getRoute($matchedRoute, $route);
}
private function getRouteDir($moduleName) {
$files = glob($this->moduleDir . '/*');
$match = preg_grep('/^' . $this->moduleDir . '\/' . $moduleName . '$/i', $files);
return array_values($match)[0] ?? false;
}
public function getConfig($route) {
$moduleName = $route[0] ?? '';
$directory = $this->getRouteDir($moduleName);
$file = $directory . '/' . $this->configFile;
return $this->getRouteModuleFile($file);
}
private function getRouteModuleFile($file) {
if (file_exists($file)) {
$config = json_decode(str_replace('"./', '"' . dirname($file) . '/', file_get_contents($file)), true);
// Extend property
if (isset($config['extend'])) {
$extended = $this->getRouteModuleFile($config['extend']);
$config = $this->mergeConfig($config, $extended);
}
return $config;
}
else return false;
}
private function mergeConfig($orig, $ext) {
foreach (['GET', 'POST', 'conditions'] as $index) {
if (isset($orig[$index]) || isset($ext[$index])) $orig[$index] = array_merge($ext[$index], $orig[$index] ?? []);
}
return $orig;
}
private function getModel($settings, $name = '') {
if (class_exists($settings['instanceOf'])) {
$this->dice = $this->dice->addRule('$Model_' . $name, $settings);
$model = $this->dice->create('$Model_' . $name, [], []);
}
else {
$model = $this->dice->create($settings['instanceOf']);
}
return $model;
}
private function getRoute($routeSettings, $route) {
$this->dice = $this->dice->addRule('$View', $routeSettings['view']);
if (isset($routeSettings['model'])) {
$model = $this->getModel($routeSettings['model']);
}
else if (isset($routeSettings['models'])) {
$model = [];
foreach ($routeSettings['models'] as $name => $diceRule) {
$model[$name] = $this->getModel($diceRule, $name);
}
}
else $model = null;
if (isset($routeSettings['controller'])) {
$controllerRule = $routeSettings['controller'];
if ($routeSettings['action'] == '$1') {
$action = isset($route[0]) && method_exists($controllerRule['instanceOf'], $route[0]) ? array_shift($route) : $routeSettings['defaultAction'];
}
else $action = $routeSettings['action'];
$controllerRule['call'] = [];
$controllerRule['call'][] = [$action, $route, function ($return) use (&$model) {
if (is_object($return)) $model = $return;
}];
$this->dice = $this->dice->addRule('$controller', $controllerRule);
if (is_array($model)) {
$controller = $this->dice->create('$Controller', [], array_merge(array_values($model), [$this->request]));
}
else {
$controller = $this->dice->create('$Controller', [], [$model, $this->request]);
}
}
else $controller = null;
$view = $this->dice->create('$View');
$route = new \Level2\Router\Route($model, $view, $controller, getcwd());
return $route;
}
}