1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RSlim; |
4
|
|
|
|
5
|
|
|
use \Psr\Http\Message\RequestInterface as Request; |
6
|
|
|
use \Psr\Http\Message\ResponseInterface as Response; |
7
|
|
|
|
8
|
|
|
class RSlim |
9
|
|
|
{ |
10
|
|
|
public $config = [ |
11
|
|
|
'base_dir' => null, |
12
|
|
|
'app_dir' => 'apps/_skel', |
13
|
|
|
'base_url' => null, |
14
|
|
|
'app_name' => 'www', |
15
|
|
|
'default_return_type' => 'html', |
16
|
|
|
'bypass_error_handlers' => true |
17
|
|
|
]; |
18
|
|
|
public $container = null; |
19
|
|
|
public $app = null; |
20
|
|
|
public $twig = null; |
21
|
|
|
public $controller = null; |
22
|
|
|
public $method = null; |
23
|
|
|
|
24
|
|
|
public function __construct($config) |
25
|
|
|
{ |
26
|
|
|
$this->config = array_merge($this->config, $config); |
27
|
|
|
define('RSLIM_DEFAULT_RETURN_TYPE',$this->config['default_return_type']); |
28
|
|
|
ini_set("default_charset", "utf-8"); |
29
|
|
|
ini_set('date.timezone', $config['app']['timezone']); |
30
|
|
|
$configuration = [ |
31
|
|
|
'settings' => [ |
32
|
|
|
'displayErrorDetails' => $config['app']['debug'], |
33
|
|
|
], |
34
|
|
|
]; |
35
|
|
|
$this->container = new \Slim\Container($configuration); |
36
|
|
|
$this->app = new \Slim\App($this->container); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function runRoute($request, $response, $route, $action = 'main', $returnType = RSLIM_DEFAULT_RETURN_TYPE, $args = []) |
40
|
|
|
{ |
41
|
|
|
$this->controller = $route; |
42
|
|
|
$this->method = $action; |
43
|
|
|
$controller = $this->config['base_dir'] . '/' . $this->config['app_dir'] . '/controllers/' . $route . '/' . $action . '.php'; |
44
|
|
|
$template = '/' . $this->config['app_dir'] . '/templates/' . $route . '/' . $action . '.html'; |
45
|
|
|
|
46
|
|
|
if (file_exists($controller)) { |
47
|
|
|
require_once($controller); |
48
|
|
|
|
49
|
|
|
$functionName = $route . '_' . $action; |
50
|
|
|
|
51
|
|
|
if ($returnType == 'html') { |
52
|
|
|
$this->useTwig($request); |
53
|
|
|
} |
54
|
|
|
if (!function_exists($functionName)) { |
55
|
|
|
$message = 'Controller ' . $route . '/' . $action . " has not " . $functionName . " function"; |
56
|
|
|
return $this->notFound($response, $returnType, $message); |
57
|
|
|
} |
58
|
|
|
if ($returnType == 'json') { |
59
|
|
|
return $this->returnJson($functionName, $request, $args, $response); |
60
|
|
|
} |
61
|
|
|
return $this->returnHtml($functionName, $request, $args, $response, $route, $action, $template); |
62
|
|
|
} |
63
|
|
|
$message = 'Controller ' . $route . '/' . $action . " not found"; |
64
|
|
|
return $this->notFound($response, $returnType, $message); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function register($requestMethod, $pattern, $controller, $returnType = RSLIM_DEFAULT_RETURN_TYPE) |
68
|
|
|
{ |
69
|
|
|
$this->app->map([strtoupper($requestMethod)], $pattern, function(Request $req, Response $res, $args) { |
70
|
|
|
list($route, $action) = explode("/", $args['controller']); |
71
|
|
|
return $args['RSlim']->runRoute($req, $res, $route, $action, $args['returnType'], $args); |
72
|
|
|
})->setArguments(['controller'=>$controller, 'returnType'=>$returnType, 'RSlim'=>$this]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function run() |
76
|
|
|
{ |
77
|
|
|
$this->container['notFoundHandler'] = function($c) { |
78
|
|
|
return function(Request $req, Response $res) use ($c) { |
|
|
|
|
79
|
|
|
return $c['response'] |
80
|
|
|
->withStatus(404) |
81
|
|
|
->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By', "reformo/rslim") |
82
|
|
|
->write('<h1>404 - Requested URL not found</h1>'); |
83
|
|
|
}; |
84
|
|
|
}; |
85
|
|
|
if ($this->config['bypass_error_handlers'] === true) { |
86
|
|
View Code Duplication |
$this->container['errorHandler'] = function($container) { |
|
|
|
|
87
|
|
|
return function($request, $response, $exception) use ($container) { |
88
|
|
|
$response->getBody()->rewind(); |
89
|
|
|
return $response->withStatus(500) |
90
|
|
|
->withHeader('Content-Type', 'text/html') |
91
|
|
|
->write($exception->getMessage()); |
92
|
|
|
}; |
93
|
|
|
}; |
94
|
|
View Code Duplication |
$this->container['phpErrorHandler'] = function($container) { |
|
|
|
|
95
|
|
|
return function($request, $response, $error) use ($container) { |
96
|
|
|
$response->getBody()->rewind(); |
97
|
|
|
return $response->withStatus(500) |
98
|
|
|
->withHeader('Content-Type', 'text/html') |
99
|
|
|
->write($error->getMessage()); |
100
|
|
|
}; |
101
|
|
|
}; |
102
|
|
|
} |
103
|
|
|
$this->app->run(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function returnJson($functionName, $request, $args, $response) |
107
|
|
|
{ |
108
|
|
|
$functionOutput = call_user_func($functionName, $request, $args); |
109
|
|
|
if (isset($functionOutput['redirect'])) { |
110
|
|
|
return $response->withRedirect($functionOutput['redirect']); |
111
|
|
|
} |
112
|
|
|
if (!is_array($functionOutput)) { |
113
|
|
|
$functionOutput = ["status" => 500, "error" => "Internal Server Error"]; |
114
|
|
|
} elseif (!isset($functionOutput['status'])) { |
115
|
|
|
$functionOutput['status'] = 200; |
116
|
|
|
} |
117
|
|
|
$status = (int) $functionOutput['status']; |
118
|
|
|
$response->getBody()->write(json_encode($functionOutput)); |
119
|
|
|
return $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By', "reformo/rslim")->withStatus($status); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function returnHtml($functionName, $request, $args, $response, $route, $action, $template){ |
123
|
|
View Code Duplication |
if (!file_exists($this->config['base_dir'] . $template)) { |
|
|
|
|
124
|
|
|
throw new \Exception("<strong>Template file not found!</strong> " . $route . '/' . $action . " needs a template file at:" . $template); |
125
|
|
|
} |
126
|
|
|
$functionOutput = call_user_func($functionName, $request, $args); |
127
|
|
|
if (isset($functionOutput['redirect'])) { |
128
|
|
|
return $response->withRedirect($functionOutput['redirect']); |
129
|
|
|
} |
130
|
|
|
if (!isset($functionOutput['data'])) { |
131
|
|
|
$functionOutput['data'] = []; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$functionOutput['app_content'] = $this->twig->render($template, $functionOutput['data']); |
135
|
|
|
$mainTemplateName = 'default'; |
136
|
|
|
if (isset($functionOutput['app_main_template'])) { |
137
|
|
|
$mainTemplateName = $functionOutput['app_main_template']; |
138
|
|
|
} |
139
|
|
|
$mainTemplate = '/' . $this->config['app_dir'] . '/templates/_' . $mainTemplateName . '.html'; |
140
|
|
|
|
141
|
|
View Code Duplication |
if (!file_exists($this->config['base_dir'] . $mainTemplate)) { |
|
|
|
|
142
|
|
|
throw new \Exception("<strong>Main emplate file not found!</strong> " . $route . '/' . $action . |
143
|
|
|
" needs a main template file at:" . $mainTemplate); |
144
|
|
|
} |
145
|
|
|
$appContent = $this->twig->render($mainTemplate, $functionOutput); |
146
|
|
|
$newResponse = $response->withHeader('X-Powered-By', "reformo/rslim"); |
147
|
|
|
$newResponse->write($appContent); |
148
|
|
|
return $newResponse; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function useTwig($request) |
152
|
|
|
{ |
153
|
|
|
$loader = new \Twig_Loader_Filesystem($this->config['base_dir']); |
154
|
|
|
$this->twig = new \Twig_Environment($loader, [ |
155
|
|
|
'cache' => '/tmp', |
156
|
|
|
'debug' => $this->config['app']['debug'], |
157
|
|
|
'auto_reload' => 1 |
158
|
|
|
]); |
159
|
|
|
$filter = new \Twig_SimpleFunction( |
160
|
|
|
'rwidget_*_*', |
161
|
|
|
function($widgetName, $widgetAction, $args = []) { |
162
|
|
|
$widgetFile = $this->config['base_dir'] . '/' . $this->config['app_dir'] . '/widgets/' . $widgetName . '/' . $widgetAction . ".php"; |
163
|
|
|
$widgetTemplate = '/' . $this->config['app_dir'] . '/templates/_widgets/' . $widgetName . '/' . $widgetAction . '.html'; |
164
|
|
|
if (!file_exists($widgetFile)) { |
165
|
|
|
throw new \Exception("<strong>Widget file not found!</strong> " . $widgetName . '/' . $widgetAction . "!"); |
166
|
|
|
} |
167
|
|
|
require_once $widgetFile; |
168
|
|
|
$widgetContentFunc = "rwidget_" . $widgetName . "_" . $widgetAction; |
169
|
|
|
if (function_exists($widgetContentFunc)) { |
170
|
|
|
$widgetContent = call_user_func($widgetContentFunc, $args); |
171
|
|
View Code Duplication |
if (!file_exists($this->config['base_dir'] . $widgetTemplate)) { |
|
|
|
|
172
|
|
|
throw new \Exception("<strong>Widget should return string:</strong> " . $widgetName . '/' . $widgetAction . "!"); |
173
|
|
|
} |
174
|
|
|
return $this->twig->render($widgetTemplate, $widgetContent); |
175
|
|
|
} |
176
|
|
|
throw new \Exception("<strong>Widget function not found!</strong> " . $widgetName . '/' . $widgetAction . "!"); |
177
|
|
|
}, |
178
|
|
|
array('is_safe' => array('html')) |
179
|
|
|
); |
180
|
|
|
$this->twig->addFunction($filter); |
181
|
|
|
$this->twig->addGlobal('runtime_config', $this->config); |
182
|
|
|
$this->twig->addGlobal('url_params', $request->getParams()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
private function notFound($response, $returnType = 'html', $message = "") |
186
|
|
|
{ |
187
|
|
|
|
188
|
|
|
$notFoundTemplate = '/' . $this->config['app_dir'] . '/templates/_404.html'; |
189
|
|
|
if ($returnType == 'json') { |
190
|
|
|
$response->getBody()->write(json_encode(['status'=>404, 'message'=>$message])); |
191
|
|
|
return $response->withHeader('Content-Type', 'application/json;charset=utf-8') |
192
|
|
|
->withHeader('X-Powered-By', "reformo/rslim")->withStatus(404); |
193
|
|
|
} |
194
|
|
|
return $response->withStatus(404) |
195
|
|
|
->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By', "reformo/rslim") |
196
|
|
|
->write($this->twig->render($notFoundTemplate, ['message'=>$message])); |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.