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
|
<?php
/*
* File: ratatoeskr/sys/urlprocess.php
*
* Providing functions / classes to handle URLs
*
* License:
* This file is part of Ratatöskr.
* Ratatöskr is licensed unter the MIT / X11 License.
* See "ratatoeskr/licenses/ratatoeskr" for more information.
*/
/*
* Function: url_action_simple
* Generate an action in a more simple way.
*
* Parameters:
* $function - A callback that gets the $data var as an input and returns the new $data var. Can throw an <Redirect> Exception.
*
* Returns:
* A callback that can be used as an url action.
*/
function url_action_simple($function)
{
return function(&$data, $url_now, &$url_next) use ($function)
{
try
{
$data = call_user_func($function, $data);
$url_next = array();
}
catch(Redirect $e)
{
$url_next = $e->nextpath;
}
};
}
/*
* Function: url_action_subactions
* Generate an action that contains subactions. Subactions can redirect to ".." to go to the level above.
*
* Parameters:
* $actions - Associative array of actions.
*
* Returns:
* A callback that can be used as an url action.
*/
function url_action_subactions($actions)
{
return function(&$data, $url_now, &$url_next) use ($actions)
{
$result = url_process($url_next, $actions, $data);
if($result !== NULL)
$url_next = $result;
};
}
/*
* Function: url_action_alias
* Generate an action that is an alias for another one (i.e. redirects).
*
* Parameters:
* $for - Path (array) of the action this one should be an alias of.
*
* Returns:
* A callback that can be used as an url action.
*/
function url_action_alias($for)
{
return function(&$data, $url_now, &$url_next) use($for)
{
$url_next = array_merge($for, $url_next);
};
}
/*
* Function: url_process
* Choose an appropiate action for the given URL.
*
* Parameters:
* $url - Either an array containing the URL components or the URL (both relative).
* $actions - Associative array of actions.
* Key is the name (anything alphanumeric, should usually not start with '_', reserved for special URL names, see beneath).
* Value is a callback of the form: function(&$data, $url_now, &$url_next). $data can be used for shared data between subactions. $url_next can be modified in order to redirect to another action / stop the routing.
*
* Special actions:
* _default - If nothing was found, this is the default.
* _notfound - If not even _default exists or NotFoundError was thrown.
* _prelude - If existant, will be executed before everything else.
* _epilog - If existant, will be executed after evrything else.
*/
function url_process($url, $actions, &$data)
{
$epilog_running = 0;
if(is_string($url))
$url = explode("/", $url);
if(count($url) == 0)
$url = array("_index");
if(isset($actions["_prelude"]))
$url = array_merge(array("_prelude"), $url);
$url_now = $url[0];
$url_next = array_slice($url, 1);
while(is_string($url_now) and ($url_now != "") and ($url_now != ".."))
{
$cb = NULL;
if(isset($actions[$url_now]))
$cb = $actions[$url_now];
else if(isset($actions["_default"]))
$cb = $actions["_default"];
else if(isset($actions["_notfound"]))
$cb = $actions["_notfound"];
else
throw new NotFoundError();
try
{
$cb($data, $url_now, $url_next);
}
catch(NotFoundError $e)
{
if(isset($actions["_notfound"]))
$url_next = array("_notfound");
else
throw $e;
}
if(count($url_next) > 0)
{
$url_now = $url_next[0];
$url_next = array_slice($url_next, 1);
}
else if(isset($actions["_epilog"]) and ($epilog_running <= 0))
{
$epilog_running = 2;
$url_now = "_epilog";
}
else
$url_now = "";
--$epilog_running;
}
if($url_now == "..")
return $url_next;
else
return NULL;
}
/*
* Class: Redirect
* Exception that can be thrown inside an <url_action_simple>.
* throw new Redirect(array("..", "foo")); will redirect to "../foo" and won't touch $data.
*/
class Redirect extends Exception
{
public $nextpath;
public function __construct($nextpath)
{
$this->nextpath = $nextpath;
parent::__construct();
}
}
/*
* Class: NotFoundError
* An Exception
*/
class NotFoundError extends Exception { }
?>
|