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
|
|
|
|
11
|
|
|
public $config = [ |
12
|
|
|
'base_dir' => null, |
13
|
|
|
'app_dir' => 'apps/_skel', |
14
|
|
|
'base_url' => null, |
15
|
|
|
'app_name' => 'www', |
16
|
|
|
'bypass_error_handlers' => true |
17
|
|
|
]; |
18
|
|
|
public $container = null; |
19
|
|
|
public $app = null; |
20
|
|
|
private $twig = null; |
21
|
|
|
|
22
|
|
|
public function __construct($config) |
23
|
|
|
{ |
24
|
|
|
$this->config = array_merge($this->config, $config); |
25
|
|
|
ini_set("default_charset", "utf-8"); |
26
|
|
|
ini_set('date.timezone', $config['app']['timezone']); |
27
|
|
|
$configuration = [ |
28
|
|
|
'settings' => [ |
29
|
|
|
'displayErrorDetails' => $config['app']['debug'], |
30
|
|
|
], |
31
|
|
|
]; |
32
|
|
|
$this->container = new \Slim\Container($configuration); |
33
|
|
|
$this->app = new \Slim\App($this->container); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function runRoute($request, $response, $route, $action = 'main', $return_type = 'html', $args = []) |
37
|
|
|
{ |
38
|
|
|
$controller = $this->config['base_dir'] . '/' . $this->config['app_dir'] . '/controllers/' . $route . '/' . $action . '.php'; |
39
|
|
|
$template = '/' . $this->config['app_dir'] . '/templates/' . $route . '/' . $action . '.html'; |
40
|
|
|
|
41
|
|
|
if (file_exists($controller)) { |
42
|
|
|
require_once($controller); |
43
|
|
|
|
44
|
|
|
if ($return_type == 'html') { |
45
|
|
|
$this->useTwig($request); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$function_name = $route.'_'.$action; |
49
|
|
|
if (!function_exists($function_name)) { |
50
|
|
|
return $this->notFound($request, $response, $return_type, 'Controller ' . $route . '/' . $action . " has not " . $function_name . " function"); |
51
|
|
|
} |
52
|
|
|
if ($return_type == 'json') { |
53
|
|
|
$status = 500; |
54
|
|
|
$function_output = call_user_func($function_name, $request, $args); |
55
|
|
|
if (!is_array($function_output)) { |
56
|
|
|
$function_output = ["status" => 500, "error" => "Internal Server Error"]; |
57
|
|
|
} else { |
58
|
|
|
if (!isset($function_output['status'])) { |
59
|
|
|
$function_output['status'] = 200; |
60
|
|
|
} |
61
|
|
|
$status = (int) $function_output['status']; |
62
|
|
|
} |
63
|
|
|
$response->getBody()->write(json_encode($function_output)); |
64
|
|
|
$newResponse = $response->withHeader('Content-Type', 'application/json;charset=utf-8')->withHeader('X-Powered-By', "reformo/rslim")->withStatus($status); |
65
|
|
|
} else { |
66
|
|
View Code Duplication |
if (!file_exists($this->config['base_dir'] . $template)) { |
|
|
|
|
67
|
|
|
throw new \Exception("<strong>Template file not found!</strong> " . $route . '/' . $action . " needs a template file at:" . $template); |
68
|
|
|
} |
69
|
|
|
$function_output = call_user_func($function_name, $request, $args); |
70
|
|
|
if (!isset($function_output['data'])) { |
71
|
|
|
$function_output['data'] = []; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$function_output['app_content'] = $this->twig->render($template, $function_output['data']); |
75
|
|
|
$main_template_name = 'default'; |
76
|
|
|
if (isset($function_output['app_main_template'])) { |
77
|
|
|
$main_template_name = $function_output['app_main_template']; |
78
|
|
|
} |
79
|
|
|
$main_template = '/' . $this->config['app_dir'] . '/templates/_' . $main_template_name . '.html'; |
80
|
|
|
|
81
|
|
View Code Duplication |
if (!file_exists($this->config['base_dir'] . $main_template)) { |
|
|
|
|
82
|
|
|
throw new \Exception("<strong>Main emplate file not found!</strong> ".$route . '/' . $action . |
83
|
|
|
" needs a main template file at:" . $main_template); |
84
|
|
|
} |
85
|
|
|
$app_content = $this->twig->render($main_template, $function_output); |
86
|
|
|
$newResponse = $response->withHeader('X-Powered-By', "reformo/rslim"); |
87
|
|
|
$newResponse->write($app_content); |
88
|
|
|
} |
89
|
|
|
return $newResponse; |
90
|
|
|
} else { |
91
|
|
|
return $this->notFound($request, $response, $return_type, 'Controller ' . $route . '/' . $action . " not found"); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function useTwig($request) |
96
|
|
|
{ |
97
|
|
|
$loader = new \Twig_Loader_Filesystem($this->config['base_dir']); |
98
|
|
|
$this->twig = new \Twig_Environment($loader, [ |
99
|
|
|
'cache' => '/tmp', |
100
|
|
|
'debug' => $this->config['app']['debug'], |
101
|
|
|
'auto_reload' => 1 |
102
|
|
|
]); |
103
|
|
|
$filter = new \Twig_SimpleFunction( |
104
|
|
|
'rwidget_*_*', |
105
|
|
|
function($widget_name, $widget_action, $args = []) { |
106
|
|
|
|
107
|
|
|
$widget_file = $this->config['base_dir'] . '/' . $this->config['app_dir'] . '/widgets/' . $widget_name . '/' . $widget_action . ".php"; |
108
|
|
|
$widget_template = '/' . $this->config['app_dir'] . '/templates/_widgets/' . $widget_name . '/' . $widget_action . '.html'; |
109
|
|
|
if (!file_exists($widget_file)) { |
110
|
|
|
throw new \Exception("<strong>Widget file not found!</strong> " . $widget_name . '/' . $widget_action . "!"); |
111
|
|
|
} |
112
|
|
|
require_once $widget_file; |
113
|
|
|
$widget_content_function = "rwidget_" . $widget_name . "_" . $widget_action; |
114
|
|
|
if (function_exists($widget_content_function)) { |
115
|
|
|
$widget_content = call_user_func($widget_content_function, $args); |
116
|
|
|
if (!file_exists($this->config['base_dir'] . $widget_template)) { |
117
|
|
|
if (is_string($widget_content)) { |
118
|
|
|
return $widget_content; |
119
|
|
|
} else { |
120
|
|
|
throw new \Exception("<strong>Widget should return string:</strong> " . $widget_name . '/' . $widget_action . "!"); |
121
|
|
|
} |
122
|
|
|
} else { |
123
|
|
|
return $this->twig->render($widget_template, $widget_content); |
124
|
|
|
} |
125
|
|
|
} else { |
126
|
|
|
throw new \Exception("<strong>Widget function not found!</strong> " . $widget_name . '/' . $widget_action . "!"); |
127
|
|
|
} |
128
|
|
|
}, |
129
|
|
|
array('is_safe' => array('html')) |
130
|
|
|
); |
131
|
|
|
$this->twig->addFunction($filter); |
132
|
|
|
$this->twig->addGlobal('runtime_config', $this->config); |
133
|
|
|
$this->twig->addGlobal('url_params', $request->getParams()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function notFound($request, $response, $return_type = 'html', $message = "") |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
|
139
|
|
|
$not_found_template = '/' . $this->config['app_dir'] . '/templates/_404.html'; |
140
|
|
|
if ($return_type == 'json') { |
141
|
|
|
$response->getBody()->write(json_encode(['status'=>404, 'message'=>$message])); |
142
|
|
|
return $response->withHeader('Content-Type', 'application/json;charset=utf-8') |
143
|
|
|
->withHeader('X-Powered-By', "reformo/rslim")->withStatus(404); |
144
|
|
|
} else { |
145
|
|
|
return $response->withStatus(404) |
146
|
|
|
->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By', "reformo/rslim") |
147
|
|
|
->write($this->twig->render($not_found_template, ['message'=>$message])); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function register($request_method, $pattern, $controller, $return_type = 'html') |
152
|
|
|
{ |
153
|
|
|
$this->app->map([strtoupper($request_method)], $pattern, function(Request $req, Response $res, $args) { |
154
|
|
|
list($route, $action) = explode("/", $args['controller']); |
155
|
|
|
return $args['RSlim']->runRoute($req, $res, $route, $action, $args['return_type'], $args); |
156
|
|
|
})->setArguments(['controller'=>$controller, 'return_type'=>$return_type, 'RSlim'=>$this]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function run() |
160
|
|
|
{ |
161
|
|
|
$this->container['notFoundHandler'] = function($c) { |
162
|
|
|
return function(Request $req, Response $res) use ($c) { |
|
|
|
|
163
|
|
|
return $c['response'] |
164
|
|
|
->withStatus(404) |
165
|
|
|
->withHeader('Content-Type', 'text/html')->withHeader('X-Powered-By', "reformo/rslim") |
166
|
|
|
->write('<h1>404 - Requested URL not found</h1>'); |
167
|
|
|
}; |
168
|
|
|
}; |
169
|
|
|
if ($this->config['bypass_error_handlers'] === true) { |
170
|
|
View Code Duplication |
$this->container['errorHandler'] = function($container) { |
|
|
|
|
171
|
|
|
return function($request, $response, $exception) use ($container) { |
172
|
|
|
$response->getBody()->rewind(); |
173
|
|
|
return $response->withStatus(500) |
174
|
|
|
->withHeader('Content-Type', 'text/html') |
175
|
|
|
->write($exception->getMessage()); |
176
|
|
|
}; |
177
|
|
|
}; |
178
|
|
View Code Duplication |
$this->container['phpErrorHandler'] = function($container) { |
|
|
|
|
179
|
|
|
return function($request, $response, $error) use ($container) { |
180
|
|
|
$response->getBody()->rewind(); |
181
|
|
|
return $response->withStatus(500) |
182
|
|
|
->withHeader('Content-Type', 'text/html') |
183
|
|
|
->write($error->getMessage()); |
184
|
|
|
}; |
185
|
|
|
}; |
186
|
|
|
} |
187
|
|
|
$this->app->run(); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.