From 2eeb281a46a7cdcd387ed69baaeafcceca4f8548 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Wed, 3 Aug 2011 16:16:11 +0200 Subject: URL processing implemented. --- ratatoeskr/sys/urlprocess.php | 110 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 ratatoeskr/sys/urlprocess.php diff --git a/ratatoeskr/sys/urlprocess.php b/ratatoeskr/sys/urlprocess.php new file mode 100644 index 0000000..e81a0d4 --- /dev/null +++ b/ratatoeskr/sys/urlprocess.php @@ -0,0 +1,110 @@ +nextpath = $nextpath; + parent::__construct(); + } +} + +class NotFoundError extends Exception { } + +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($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_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; +} + +?> -- cgit v1.2.3-54-g00ecf