Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / verysimple / Phreeze / ActionRouter.php
bloba7ee93fbbf86e6c84368a854c11111cd16c81e67
1 <?php
2 /** @package verysimple::Phreeze */
3 require_once("verysimple/HTTP/RequestUtil.php");
4 require_once("verysimple/Util/UrlWriterMode.php");
5 require_once("verysimple/Phreeze/IRouter.php");
7 /**
8 * class for dealing with URLs
10 * @package verysimple::HTTP
11 * @author VerySimple Inc.
12 * @copyright 1997-2007 VerySimple, Inc. http://www.verysimple.com
13 * @license http://www.gnu.org/licenses/lgpl.html LGPL
14 * @version 1.0
16 class ActionRouter implements IRouter
18 private $_mode;
19 private $_appRoot;
20 private $_defaultRoute;
21 protected $stripApi = true;
22 protected $delim = '&';
23 protected static $_format;
25 /**
26 * Constructor allows a rewriting pattern to be specified
28 * @param string $format
29 * sprintf compatible format
31 public function __construct($format = "%s.%s.page?%s", $mode = UrlWriterMode::WEB, $appRoot = '', $defaultRoute = '')
33 self::$_format = $format;
34 $this->_mode = $mode;
35 $this->_appRoot = $appRoot;
36 $this->_defaultRoute = $defaultRoute;
39 /**
40 * @inheritdocs
42 public function GetUri()
44 return implode('/', RequestUtil::GetUrlParts($this->_appRoot));
47 /**
48 * @inheritdocs
50 public function GetUrlParams()
52 return $_REQUEST;
55 /**
56 * @inheritdocs
58 public function GetUrlParam($key, $default = '')
60 // make the route params case insensitive
61 return RequestUtil::Get($key, $default, false, true);
64 /**
65 * @inheritdocs
67 public function GetUrl($controller, $method, $params = '', $requestMethod = '')
69 $format = str_replace("{delim}", $this->delim, self::$_format);
71 $qs = "";
72 $d = "";
73 if (is_array($params)) {
74 foreach ($params as $key => $val) {
75 // if no val, the omit the equal sign (this might be used in rest-type requests)
76 $qs .= $d . $key . (strlen($val) ? ("=" . urlencode($val)) : "");
77 $d = $this->delim;
79 } else {
80 $qs = $params;
83 $url = sprintf($format, $controller, $method, $qs);
85 // strip off trailing delimiters from the url
86 $url = (substr($url, - 5) == "&amp;") ? substr($url, 0, strlen($url) - 5) : $url;
87 $url = (substr($url, - 1) == "&" || substr($url, - 1) == "?") ? substr($url, 0, strlen($url) - 1) : $url;
89 $api_check = explode("/api/", RequestUtil::GetCurrentUrl());
90 if ($this->stripApi && count($api_check) > 1) {
91 $url = $api_check [0] . "/" . $url;
94 return $url;
97 /**
98 * @inheritdocs
100 public function GetRoute($uri = "")
102 if ($uri == "") {
103 $action = RequestUtil::Get('action');
104 if (! $action) {
105 $action = $this->_defaultRoute;
108 $uri = $action ? $action : RequestUtil::GetCurrentURL();
111 // get the action requested
112 $params = explode(".", str_replace("/", ".", $uri));
113 $controller_param = isset($params [0]) && $params [0] ? $params [0] : "";
114 $controller_param = str_replace(array (
115 ".",
116 "/",
117 "\\"
118 ), array (
122 ), $controller_param);
124 if (! $controller_param) {
125 throw new Exception("Invalid or missing Controller parameter");
128 $method_param = isset($params [1]) && $params [1] ? $params [1] : "";
129 if (! $method_param) {
130 $method_param = "DefaultAction";
133 return array (
134 $controller_param,
135 $method_param
140 * Returns true or false based on the $value passed in as to whether or not the
141 * URL Writer is currently in that mode.
143 * @param $value String
144 * mode to check against the current mode
145 * @return boolean TRUE if arg passed in is the current mode
147 public function ModeIs($value)
149 if (strcmp($this->_mode, $value) == 0) {
150 return true;
151 } else {
152 return false;
157 * Returns how the Dispatcher plucks it's controller and method from the URL.
159 * @param $default_action The
160 * Default action in case the argument hasn't been supplied
162 public function GetAction($url_param = "action", $default_action = "Account.DefaultAction")
164 switch ($this->_mode) {
165 // TODO: Determine mobile/joomla URL action (if different from default)
167 * case UrlWriterMode::JOOMLA:
168 * break;
169 * case UrlWriterMode::MOBILE:
170 * break;
172 default:
173 // default is to return the standard browser-based action=%s.%s&%s:
174 return RequestUtil::Get($url_param, $default_action);
175 break;