-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMF.php
More file actions
361 lines (317 loc) · 11.1 KB
/
MF.php
File metadata and controls
361 lines (317 loc) · 11.1 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**!
* mf(minimal framework) v0.9.2
* https://github.com/webgoto/mf
*
* Copyright 2016, webgoto.net
* Released under the MIT license
*/
Class MF{
public $src_dir;
public $asset_dir;
public $root_path;
public $sub_path;
public $site_path;
public $asset_path;
public $src_path;
public $root_url;
public $sub_url;
public $site_url;
public $asset_url;
public $slug;
public $route;
public $title;
public $option;
public $slugs = array('404'=>array('title'=>'ページが見つかりません。','url'=>''),'405'=>array('title'=>'送信されたメソッドは許可されていません。','url'=>''));
protected $router;
/**
* コンストラクタ
*
* @param array $opt_arr src_dir='/src', asset_dir'/asset', root_path, root_url, sub_path, sub_url
*/
public function __construct($opt_arr=array()){
// Microsoft IIS doesn't set REQUEST_URI by default
if(!isset($_SERVER['REQUEST_URI'])){
$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'], 1);
if(isset($_SERVER['QUERY_STRING'])){
$_SERVER['REQUEST_URI'] .= '?'.$_SERVER['QUERY_STRING'];
}
}
$this->src_dir = $this->array_get($opt_arr['src_dir'], '/src');
$this->asset_dir = $this->array_get($opt_arr['asset_dir'], '/asset');
$this->router = new TreeRoute();
//pathの設定
$backtrace = debug_backtrace();
$index_path = rtrim(str_replace('\\', '/', dirname($backtrace[count($backtrace)-1]['file'])), '/');
$this->root_path = $this->array_get($opt_arr['root_path'], rtrim(str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']), '/'));
if(mb_strpos($index_path, $this->root_path)===false) echo 'MF:ドキュメントルートとインデックスファイルのPATHが一致しないため、root_pathが正しく推測できませんでした。<br>index_pathから[/サブディレクトリ]を除いたPATHをコンストラクタのroot_pathで指定してみて下さい。<br>index_path:'.$index_path.'<br> root_path: '.$this->root_path;
$length = mb_strlen($index_path)-mb_strlen($this->root_path)-mb_strpos($index_path, $this->root_path);
$this->sub_path = $this->array_get($opt_arr['sub_path'], mb_substr($index_path, -$length, $length));
$this->site_path = $this->root_path.$this->sub_path;
$this->asset_path = $this->site_path.$this->asset_dir;
$this->src_path = $this->site_path.$this->src_dir;
//urlの設定
$scheme = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http';
$this->root_url = $scheme.'://'.$_SERVER['SERVER_NAME'];
if(($scheme==='https' && $_SERVER['SERVER_PORT']!=443) || ($scheme==='http' && $_SERVER['SERVER_PORT']!=80)){
$this->root_url .= sprintf(':%s', $_SERVER['SERVER_PORT']);
}
$this->root_url = $this->array_get($opt_arr['root_url'], $this->root_url);
$this->sub_url = $this->array_get($opt_arr['sub_url'], mb_substr($index_path, -$length, $length));
$this->site_url = $this->root_url.$this->sub_url;
$this->asset_url = $this->site_url.$this->asset_dir;
}
/**
* サイトのページを設定
*
* @param string $route
* @param array $handler
* @param string|array $method
*/
public function addRoute($route, $handler, $method = array('GET', 'POST', 'OPTIONS', 'HEAD', 'PUT', 'DELETE', 'TRACE', 'CONNECT')){
$this->slugs[$handler[0]] = array('url'=>$route, 'title'=>$handler[1]);
$this->router->addRoute($method, $this->sub_url.$route, $handler);
}
/**
* アクセスされたページを特定
*/
public function dispatch(){
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$cut = mb_strlen($this->sub_url);
$this->route = mb_substr($uri, $cut, mb_strlen($uri)-$cut);
$result = $this->router->dispatch($httpMethod, $uri);
if(isset($result['error'])){
//404, 405エラーの場合
$code = strval($result['error']['code']);
$this->slug = $code;
$this->title = isset($this->slugs[$code]['title']) ? $this->slugs[$code]['title'] : $result['error']['message'];
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
header($protocol.' '.$code.' '. $result['error']['message']);
}else{
$this->slug = $result['handler'][0];
$this->title = $result['handler'][1];
$this->option = $result['params'];
}
return $result;
}
/**
* 現在のスラッグと比較し、一致した場合にtrueまたは$output_textで指定した文字列を返す。
*
* @param string|array $slug
* @param string $output_text
*
* @return bool|string
*/
public function match_slug($slug, $output_text = ''){
foreach((array)$slug as $value){
if(mb_strpos($this->slug, $value)!==false){
if($output_text===''){
return true;
}else{
return $output_text;
}
}
}
if($output_text===''){
return false;
}else{
return '';
}
}
/**
* スラッグ名とオプション値から登録されてるurlを返す。
*
* @param string $slug
* @param array $option
*
* @return string
*/
public function slug_url($slug = '', $option = array()){
if($slug==='') $slug = $this->slug;
$url = $this->slugs[$slug]['url'];
foreach($option as $key=>$val){
$url = preg_replace('/\{'.$key.':.*?\}/', $val, $url);
}
$url = preg_replace('/\{.*?:.*?\}\/?/', '', $url);
return $this->site_url.$url;
}
/**
* 配列の値などをエラーを出さずに取り出す。
*
* @param mixed $var
* @param mixed $default
*
* @return mixed
*/
private function array_get(&$var, $default = null){
if(isset($var)) return $var;
return $default;
}
}
/*
* TreeRoute
* https://github.com/baryshev/TreeRoute
* Copyright (c) 2015, Vadim Baryshev All rights reserved.
*/
class TreeRoute
{
const PARAM_REGEXP = '/^{((([^:]+):(.+))|(.+))}$/';
const SEPARATOR_REGEXP = '/^[\s\/]+|[\s\/]+$/';
private $routes = array('childs' => array(), 'regexps' => array());
private function match($url)
{
$parts = explode('?', $url, 2);
$parts = explode('/', preg_replace(self::SEPARATOR_REGEXP, '', $parts[0]));
if (sizeof($parts) === 1 && $parts[0] === '') {
$parts = array();
}
$params = array();
$current = $this->routes;
for ($i = 0, $length = sizeof($parts); $i < $length; $i++) {
if (isset($current['childs'][$parts[$i]])) {
$current = $current['childs'][$parts[$i]];
} else {
foreach ($current['regexps'] as $regexp => $route) {
if (preg_match('/^' . addcslashes($regexp, '/') . '$/', $parts[$i])) {
$current = $route;
$params[$current['name']] = $parts[$i];
continue 2;
}
}
if (!isset($current['others'])) {
return null;
}
$current = $current['others'];
$params[$current['name']] = $parts[$i];
}
}
if (!isset($current['methods'])) {
return null;
}
return array(
'methods' => $current['methods'],
'route' => $current['route'],
'params' => $params
);
}
public function addRoute($methods, $route, $handler)
{
$methods = (array) $methods;
$parts = explode('/', preg_replace(self::SEPARATOR_REGEXP, '', $route));
if (sizeof($parts) === 1 && $parts[0] === '') {
$parts = array();
}
$current = &$this->routes;
for ($i = 0, $length = sizeof($parts); $i < $length; $i++) {
$paramsMatch = preg_match(self::PARAM_REGEXP, $parts[$i], $paramsMatches);
if ($paramsMatch) {
if (!empty($paramsMatches[2])) {
if (!isset($current['regexps'][$paramsMatches[4]])) {
$current['regexps'][$paramsMatches[4]] = array('childs' => array(), 'regexps' => array(), 'name' => $paramsMatches[3]);
}
$current = &$current['regexps'][$paramsMatches[4]];
} else {
if (!isset($current['others'])) {
$current['others'] = array('childs' => array(), 'regexps' => array(), 'name' => $paramsMatches[5]);
}
$current = &$current['others'];
}
} else {
if (!isset($current['childs'][$parts[$i]])) {
$current['childs'][$parts[$i]] = array('childs' => array(), 'regexps' => array());
}
$current = &$current['childs'][$parts[$i]];
}
}
$current['route'] = $route;
for ($i = 0, $length = sizeof($methods); $i < $length; $i++) {
if (!isset($current['methods'])) {
$current['methods'] = array();
}
$current['methods'][strtoupper($methods[$i])] = $handler;
}
}
public function getOptions($url)
{
$route = $this->match($url);
if (!$route) {
return null;
}
return array_keys($route['methods']);
}
public function dispatch($method, $url)
{
$route = $this->match($url);
if (!$route) {
return array(
'error' => array(
'code' => 404,
'message' => 'Not Found'
),
'method' => $method,
'url' => $url
);
}
if (isset($route['methods'][$method])) {
return array(
'method' => $method,
'url' => $url,
'route' => $route['route'],
'params' => $route['params'],
'handler' => $route['methods'][$method]
);
}
return array(
'error' => array(
'code' => 405,
'message' => 'Method Not Allowed'
),
'method' => $method,
'url' => $url,
'route' => $route['route'],
'params' => $route['params'],
'allowed' => array_keys($route['methods'])
);
}
public function getRoutes()
{
return $this->routes;
}
public function setRoutes($routes)
{
$this->routes = $routes;
}
public function options($route, $handler)
{
$this->addRoute('OPTIONS', $route, $handler);
}
public function get($route, $handler)
{
$this->addRoute('GET', $route, $handler);
}
public function head($route, $handler)
{
$this->addRoute('HEAD', $route, $handler);
}
public function post($route, $handler)
{
$this->addRoute('POST', $route, $handler);
}
public function put($route, $handler)
{
$this->addRoute('PUT', $route, $handler);
}
public function delete($route, $handler)
{
$this->addRoute('DELETE', $route, $handler);
}
public function trace($route, $handler)
{
$this->addRoute('TRACE', $route, $handler);
}
public function connect($route, $handler)
{
$this->addRoute('CONNECT', $route, $handler);
}
}