This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
/
App.php
244 lines (199 loc) · 5.27 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
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
<?php
namespace Swiftlet;
class App implements Interfaces\App
{
protected
$action = 'index',
$args = array(),
$config = array(),
$controller,
$hooks = array(),
$plugins = array(),
$rootPath = '/',
$singletons = array(),
$view
;
/**
* Run the application
*/
public function run()
{
// Determine the client-side path to root
if ( !empty($_SERVER['REQUEST_URI']) ) {
$this->rootPath = preg_replace('/(index\.php)?(\?.*)?$/', '', $_SERVER['REQUEST_URI']);
}
// Run from command line, e.g. "php index.php -q index"
$opt = getopt('q:');
if ( isset($opt['q']) ) {
$_GET['q'] = $opt['q'];
}
if ( !empty($_GET['q']) ) {
$this->rootPath = preg_replace('/' . preg_quote(rawurlencode($_GET['q']), '/') . '$/', '', $this->rootPath);
}
// Extract controller name, view name, action name and arguments from URL
$controllerName = 'Index';
if ( !empty($_GET['q']) ) {
$this->args = explode('/', $_GET['q']);
if ( $this->args ) {
$controllerName = str_replace(' ', '/', ucwords(str_replace('_', ' ', str_replace('-', '', array_shift($this->args)))));
}
if ( $action = $this->args ? array_shift($this->args) : '' ) {
$this->action = str_replace('-', '', $action);
}
}
if ( !is_file('Swiftlet/Controllers/' . $controllerName . '.php') ) {
$controllerName .= '/Index';
if ( !is_file('Swiftlet/Controllers/' . $controllerName . '.php') ) {
$controllerName = 'Error404';
}
}
$this->view = new View($this, strtolower($controllerName));
// Instantiate the controller
$controllerName = 'Swiftlet\Controllers\\' . str_replace('/', '\\', $controllerName);
$this->controller = new $controllerName($this, $this->view);
// Load plugins
if ( $handle = opendir('Swiftlet/Plugins') ) {
while ( ( $file = readdir($handle) ) !== FALSE ) {
if ( is_file('Swiftlet/Plugins/' . $file) && preg_match('/^(.+)\.php$/', $file, $match) ) {
$pluginName = 'Swiftlet\Plugins\\' . $match[1];
$this->plugins[$pluginName] = array();
foreach ( get_class_methods($pluginName) as $methodName ) {
$method = new \ReflectionMethod($pluginName, $methodName);
if ( $method->isPublic() && !$method->isFinal() && !$method->isConstructor() ) {
$this->plugins[$pluginName][] = $methodName;
}
}
}
}
ksort($this->plugins);
closedir($handle);
}
// Call the controller action
$this->registerHook('actionBefore');
if ( method_exists($this->controller, $this->action) ) {
$method = new \ReflectionMethod($this->controller, $this->action);
if ( $method->isPublic() && !$method->isFinal() && !$method->isConstructor() ) {
$this->controller->{$this->action}();
} else {
$this->controller->notImplemented();
}
} else {
$this->controller->notImplemented();
}
$this->registerHook('actionAfter');
return array($this->view, $this->controller);
}
/**
* Serve the page
*/
public function serve()
{
$this->view->render();
}
/**
* Get a configuration value
* @param string $variable
* @return mixed
*/
public function getConfig($variable)
{
if ( isset($this->config[$variable]) ) {
return $this->config[$variable];
}
}
/**
* Set a configuration value
* @param string $variable
* @param mixed
*/
public function setConfig($variable, $value)
{
$this->config[$variable] = $value;
}
/**
* Get the client-side path to root
* @return string
*/
public function getRootPath()
{
return $this->rootPath;
}
/**
* Get the action name
* @return string
*/
public function getAction()
{
return $this->action;
}
/**
* Get the arguments
* @return array
*/
public function getArgs()
{
return $this->args;
}
/**
* Get a model
* @param string $modelName
* @return object
*/
public function getModel($modelName)
{
$modelName = 'Swiftlet\Models\\' . ucfirst($modelName);
// Instantiate the model
return new $modelName($this);
}
/**
* Get a model singleton
* @param string $modelName
* @return object
*/
public function getSingleton($modelName)
{
if ( isset($this->singletons[$modelName]) ) {
return $this->singletons[$modelName];
}
$model = $this->getModel($modelName);
$this->singletons[$modelName] = $model;
return $model;
}
/**
* Register a hook for plugins to implement
* @param string $hookName
* @param array $params
*/
public function registerHook($hookName, array $params = array())
{
$this->hooks[] = $hookName;
foreach ( $this->plugins as $pluginName => $hooks ) {
if ( in_array($hookName, $hooks) ) {
$plugin = new $pluginName($this, $this->view, $this->controller);
$plugin->{$hookName}($params);
}
}
}
/**
* Class autoloader
* @param $className
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
*/
public function autoload($className)
{
preg_match('/(^.+\\\)?([^\\\]+)$/', ltrim($className, '\\'), $match);
$file = str_replace('\\', '/', $match[1]) . str_replace('_', '/', $match[2]) . '.php';
require $file;
}
/**
* Error handler
* @param int $number
* @param string $string
* @param string $file
* @param int $line
*/
public function error($number, $string, $file, $line)
{
throw new \Exception('Error #' . $number . ': ' . $string . ' in ' . $file . ' on line ' . $line);
}
}