-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.php
153 lines (137 loc) · 3.22 KB
/
Router.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
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
<?php
/**
* @file Router.php
*
* description
*
* copyright (c) 2017 Frank Hellenkamp [[email protected]]
*
* @author Frank Hellenkamp [[email protected]]
*/
namespace Depage\Router;
/**
* @brief Router
* Class Router
*/
class Router
{
/**
* @brief handlers
**/
protected $handlers = [];
/**
* @brief options
**/
protected $options = [
'urlSubArgs' => -1,
];
/**
* @brief baseUrl
**/
public $baseUrl;
/**
* @brief languages
**/
public $languages;
// {{{ __construct()
/**
* @brief __construct
*
* @param mixed $baseUrl
* @return void
**/
public function __construct($baseUrl, $languages = [])
{
$this->baseUrl = $baseUrl;
$this->languages = $languages;
}
// }}}
// {{{ analyzeUrl()
/**
* @brief analyzeUrl
*
* @param mixed $url
* @return void
**/
public function analyzeUrl($url)
{
return Url::fromUrl($url, $this->baseUrl, $this->languages);
}
// }}}
// {{{ analyzeCurrentUrl()
/**
* @brief analyzeCurrentUrl
*
* @param mixed
* @return void
**/
public function analyzeCurrentUrl()
{
return Url::fromRequestUri($this->baseUrl, $this->languages);
}
// }}}
// {{{ addHandler()
/**
* @brief addHandler
*
* @param mixed $
* @return void
**/
public function addHandler($handler, $route = 'default')
{
$this->handlers[$route] = $handler;
return $this;
}
// }}}
// {{{ route()
/**
* @brief route
*
* @param mixed
* @return void
**/
public function route($url = null)
{
if (is_null($url)) {
$url = $this->analyzeCurrentUrl();
} else {
$url = $this->analyzeUrl($url);
}
$handler = $this->handlers['default'];
$handler->baseUrl = $this->baseUrl;
$options = (object) array_replace($this->options, $handler->options);
// @todo add option to handler to get offset of first element
$handler->url = $url;
$handler->urlSubArgs = $url->getParts(0, $options->urlSubArgs);
$handler->lang = $url->lang;
$action = $url->getPart($options->urlSubArgs);
$args = $url->getParts($options->urlSubArgs + 1);
$action = str_replace("-", "_", $action);
$action = preg_replace("/\.(html|php)$/", "", $action);
if (empty($action)) {
$action = "index";
}
$handler->action = $action;
if (is_callable([$handler, '_init'])) {
$handler->_init();
}
if (is_callable([$handler, $action])) {
$response = call_user_func_array([$handler, $action], $args);
} else {
$response = $handler->notFound($action, $args);
}
if (method_exists($response, 'sendHeaders')) {
$response->sendHeaders();
}
return $response;
}
// }}}
// {{{ redirect
public static function redirect($url)
{
header('Location: ' . $url);
die( "Tried to redirect you to <a href=\"$url\">$url</a>");
}
// }}}
}
// vim:set ft=php sw=4 sts=4 fdm=marker et :