forked from juliomatcom/one-php-microframework
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathone_framework.php
More file actions
324 lines (283 loc) · 9.8 KB
/
one_framework.php
File metadata and controls
324 lines (283 loc) · 9.8 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* One PHP MVC Micro Framework
* @author Julio Cesar Martin
* Twitter @juliomatcom - Email juliomatcom@gmail.com
* Feel free to contact me any time
* http://oneframework.julces.com/
*
* Controllers must be in APP_DIR/controllers
* Views must be in APP_DIR/views
* Translations must be in APP_DIR/translations
* Assets must be in APP_DIR/assets/
*/
class OneFramework{
protected $request;
protected $db;
protected $routes = array();
//set this value to True if you want to get access to translations
protected $translate = false;
//here the value of the locale requested by url (segment 1)
protected $locale = null;
protected $locales = ['es','en','fr'];
protected $translations = array();
protected $prod = false;
public function __construct($prod = false){
$this->prod = $prod;
$this->defineConstants();
$this->buildRequest();
$this->loadTrans();
}
public function listen(){
$slugs = array();
$run = 0;
foreach($this->routes as $route)
if($func = $this->processUri($route,$slugs)){
//call callback function with params in slugs
$run = 1;
call_user_func_array($func,$slugs);
}
if(!$run) $this->error('Not route found',1);
}
/**
* Connect if needed, retrieve the database connection
* @return mysqli
*/
public function getDB(){
$this->db = $this->db ? $this->db : new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
if ($this->db->connect_errno) {
$this->error("Error connecting to database: ".DB_DATABASE."<br/> Info:". $this->db->connect_error);
}
return $this->db;
}
private function defineConstants(){
define('APP_DIR',__DIR__);
define('CONTROLLERS_ROUTE',APP_DIR.'/controllers/');
define('VIEWS_ROUTE',APP_DIR.'/views/');
define('TRANSLATION_DIR',APP_DIR.'/translations/');
define('DB_HOST','127.0.0.1');
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_DATABASE','');
define('APP_NAME','');
}
private function buildRequest(){
$this->request = new stdClass();
$this->request->query = $_GET;
$this->request->request = $_POST;
$this->request->server = $_SERVER;
$this->request->cookie = $_COOKIE;
}
/**
* Create a route to the app
* @param $uri
* @return string new URL with locale if needed
*/
public function getRoute($uri){
return $this->translate ? ((APP_NAME != '') ? ('/'.APP_NAME.'/'.$this->locale.$uri) : ('/'.$this->locale.$uri) ):
((APP_NAME != '') ?('/'.APP_NAME.$uri) : $uri);
}
/**
* Process the request and Return a Response
* @param uri key for the Route example: /book/{number}/edit
* @param action
*/
public function get($uri, $function){
$routeKey = $this->translate ? ('/{_locale}'.$uri) : $uri;
$route = new stdClass();
$route->route = $routeKey;
$route->function = $function;
//save route and function
$this->routes[] = $route;
}
//REGION PRIVATE FUNCTIONS FOR THE APP CORE
private function processUri($route,&$slugs = array()){
$uri = isset($this->request->server['REQUEST_URI']) ? $this->request->server['REQUEST_URI'] : '/' ;
$func = $this->matchUriWithRoute($uri,$route,$slugs);
return $func ? $func : false;
}
private function matchUriWithRoute($uri,$route,&$slugs){
$uri_segments = preg_split('/[\/]+/',$uri,null,PREG_SPLIT_NO_EMPTY);
//redirect if no locale is set
if(count($uri_segments) == 0 && $this->translate){
APP_NAME!= '' ? $this->redirect("/".APP_NAME."/{$this->locales[0]}/") : $this->redirect("/{$this->locales[0]}/");
}
$route_segments = preg_split('/[\/]+/',$route->route,null,PREG_SPLIT_NO_EMPTY);
if($this->compareSegments($uri_segments,$route_segments,$slugs)){
//route matched
if($this->translate) $this->locale = $this->getSegment(0); //save locale
return $route->function; //Object route
}
return false;
}
/**
* If locale is set return locale from values in array $locales
* @return bool|null
*/
public function getLocale(){
return $this->locale ? $this->locale : false;
}
/**
* Get current enviroment
* @return bool
*/
public function getEnviroment(){
return $this->prod;
}
/**
* Get request Object
* @return Request class
*/
public function getRequest(){
return $this->request;
}
/**
* Translate a string
* @param string $key
* @param bool $lang Language
* @return string|exception Error
*/
public function trans($key,$lang = false){
if($this->translate){
if(!$lang) $lang= $this->locale;
return isset($this->translations[$lang][$key]) ? $this->translations[$lang][$key] : 'translation_'.$key;
}
else return $this->error(
'You can not use translation because is disabled in the Framework.<br/>
Set the value of \'translate\' property to true.');
}
/**
* Load translations from disk
*/
private function loadTrans(){
if($this->translate){//include translations
foreach($this->locales as $locale){
$filename = 'trans.'.$locale.'.txt';
if(file_exists(TRANSLATION_DIR.$filename)){
$text = file_get_contents(TRANSLATION_DIR.$filename);
$lines = explode(PHP_EOL,$text);
$arr = array();
foreach($lines as $line){//add pairs
$par = explode(':',$line);
if(!empty($par[0]) && !empty($par[1]))
$arr[utf8_decode($par[0])] = trim($par[1]); //save
}
$this->translations[$locale] = $arr;
}
}
}
}
/**
* Returns al the translations loaded
* @return array
*/
public function getTranslations(){
return $this->translations;
}
public function Redirect($href){
echo header('Location: '.$href);
exit;
}
/**
* Return a new HTTP response.
* @param string $view_filename Source to the file
* @param array $vars Data to pass to the View
* @param array $headers Http Headers
*/
public function Response($view_filename,array $vars = array(),array $headers=array()){
if(count($headers)){//add extra headers
foreach($headers as $key=>$header){
header($key.': '.$header);
}
}
//pass to the view
$view = new View(VIEWS_ROUTE.$view_filename,$vars,$this);
$view->load();
exit;
}
/**
* Return a new Json Object Response
* @param array $data Array to encode
*/
public function JsonResponse(array $data = array()){
header('Content-Type: application/json');//set headers
echo json_encode($data);
exit;
}
/** Match 2 uris
* @param $uri_segments
* @param $route_segments
* @return bool
*/
private function CompareSegments($uri_segments,$route_segments,&$slugs){
if(count($uri_segments) != count($route_segments)) return false;
foreach($uri_segments as $segment_index=>$segment){
$segment_route = $route_segments[$segment_index];
//different segments must be an {slug}
if(preg_match('/^{[^\/]*}$/',$segment_route) && $segment_route!='{_locale}')
$slugs[] = $segment;//save slug key => value
else if($segment_route != $segment && preg_match('/^{[^\/]*}$/',$segment_route) != 1) return false;
}
//match with every segment
return true;
}
private function getSegment($segment_number){
$uri = isset($this->request->server['REQUEST_URI']) ? $this->request->server['REQUEST_URI'] : '/' ;
$uri_segments = preg_split('/[\/]+/',$uri,null,PREG_SPLIT_NO_EMPTY);
return isset($uri_segments[$segment_number]) ? $uri_segments[$segment_number] : false;
}
/**
* Show framework's errors
* @param string $msg
* @param int $number
*/
private function error($msg='',$number = 0){
if($this->prod){
//do something here
return false;
}
else{
$frw_msg =
"<h1>One Framework: Error</h1>
<p>$msg</p><br/>";
switch($number){
case 1:
$frw_msg = $frw_msg."<b>Note</b>: Routes begin always with '/' character. If app add a /{lang}/ path is because is enabled the framework's translations.";
break;
default: break;
}
$frw_msg = $frw_msg." <h2>Trace:</h2>";
echo $frw_msg;
throw new Exception();
}
}
}
/**
* Class View
* Load a view File with access to data and Framework
*/
class View
{
protected $data;
protected $framework;
protected $src;
public function __construct($src,array $vars,$framework = null){
$this->data = $vars;
$this->framework = $framework;
$this->src = $src;
}
/**
* Renders a view
* @throws Exception if View not found
*/
public function load(){
$app = $this->framework;
$data = $this->data;
if(file_exists($this->src))
include_once($this->src); //scoped to this class
else{
if($this->framework && !$this->framework->getEnviroment())
throw new Exception("ONE Framework: View filename: {$this->src} NOT found in ". VIEWS_ROUTE);
}
}
}