/
framework2.php
115 lines (91 loc) · 2.53 KB
/
framework2.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
<?php
class Response {
public static function render_template($file_name, $data = []) {
include 'templates/'.$file_name.'.php' ;
}
public static function render_xml($xml_info) {
header('Access-Control-Allow-Origin: *');
header('Content-Type: text/xml;');
echo $xml_info;
}
public static function render_json($data) {
header('Access-Control-Allow-Origin: *');
header('Content-Type: text/json;');
echo json_encode($data);
}
public static function render_csv($data, $name = 'data') {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$name.'.csv');
$output = fopen('php://output', 'w');
foreach ($data as $row) {
fputcsv($output, $row);
}
}
public static function render_text($text) {
echo $text;
}
public static function redirect_to($name) {
header('Location: ?r=/'.$name);
}
}
class Session {
public static function start() {
session_start();
}
public static function get($name) {
return $_SESSION[$name];
}
public static function set($key, $value) {
$_SESSION[$key] = $value;
}
public static function end() {
session_destroy();
}
}
class Request {
public static function get_request() {
return $_SERVER['REQUEST_METHOD'];
}
public static function is_get() {
return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
}
public static function is_post() {
return $_SERVER['REQUEST_METHOD'] == 'POST' ? true : false;
}
public static function is_head() {
return $_SERVER['REQUEST_METHOD'] == 'HEAD' ? true : false;
}
public static function is_put() {
return $_SERVER['REQUEST_METHOD'] == 'PUT' ? true : false;
}
public static function is_delete() {
return $_SERVER['REQUEST_METHOD'] == 'DELETE' ? true : false;
}
}
class Lily {
private $event_array = array();
public function route($name, $function) {
$regex_rule = str_replace('<string>','[a-zA-Z0-9_][a-zA-Z0-9_]*', $name);
$regex_rule = str_replace('<int>', '[0-9]+', $regex_rule);
$regex_rule = str_replace("/", "\/", $regex_rule);
$regex_rule = "/^$regex_rule$/";
$this->event_array[$regex_rule] = $function;
}
public function start($name, $data = []) {
if (strlen($name) <= 1) {
$this->event_array['/^\/index$/']();
return;
}
foreach ($this->event_array as $event_rule => $event_response) {
if (preg_match($event_rule, $name, $match)) {
$data = explode('/', $name);
$this->event_array[$event_rule]($data);
return;
}
}
}
public function model($name) {
include 'models/' . $name . '.php';
return new $name;
}
}