123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- class LtRouter
- {
- public $configHandle;
- public $routingTable;
- public $module;
- public $action;
- public function __construct()
- {
- if (! $this->configHandle instanceof LtConfig)
- {
- if (class_exists("LtObjectUtil"))
- {
- $this->configHandle = LtObjectUtil::singleton("LtConfig");
- }
- else
- {
- $this->configHandle = new LtConfig;
- }
- }
- }
- public function init()
- {
- $this->routingTable = $this->configHandle->get("router.routing_table");
- if (empty($this->routingTable))
- {
- $this->routingTable = array('pattern' => ":module/:action/*",
- 'default' => array('module' => 'default', 'action' => 'index'),
- 'reqs' => array('module' => '[a-zA-Z0-9\.\-_]+',
- 'action' => '[a-zA-Z0-9\.\-_]+'
- ),
- 'varprefix' => ':',
- 'delimiter' => '/',
- 'postfix' => '',
- 'protocol' => 'PATH_INFO',
- );
- }
- $delimiter = $this->routingTable['delimiter'];
- $postfix = $this->routingTable['postfix'];
- $protocol = strtoupper($this->routingTable['protocol']);
- $module = '';
- $action = '';
- $params = array();
-
- if (isset($_SERVER['SERVER_PROTOCOL']))
- {
- if (isset($_SERVER['PATH_INFO']) && !empty($_SERVER['PATH_INFO']))
- {
-
- $url = rtrim($_SERVER['PATH_INFO'], "$postfix");
- $url = explode($delimiter, trim($url, "/"));
- }
- else if (isset($_SERVER['REQUEST_URI']))
- {
- if ('REWRITE' == $protocol)
- {
- if (0 == strcmp($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']))
- {
- $url = array();
- }
- else
- {
- $url = substr($_SERVER['REQUEST_URI'], strlen(pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME)));
- $url = rtrim($url, "$postfix");
- $url = explode($delimiter, trim($url, "/"));
- }
- }
- else if ('PATH_INFO' == $protocol)
- {
- $url = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
- $url = rtrim($url, "$postfix");
- $url = explode($delimiter, trim($url, "/"));
- }
- else
- {
- $url = array();
- foreach($_GET as $v)
- {
- $url[] = $v;
- }
- }
- }
- else
- {
- $url = array();
- foreach($_GET as $v)
- {
- $url[] = $v;
- }
- }
- $params = $this->matchingRoutingTable($url);
- $module = $params['module'];
- $action = $params['action'];
- }
- else
- {
-
- $i = 0;
- while (isset($_SERVER['argv'][$i]) && isset($_SERVER['argv'][$i + 1]))
- {
- if (("-m" == $_SERVER['argv'][$i] || "--module" == $_SERVER['argv'][$i]))
- {
- $module = $_SERVER['argv'][$i + 1];
- }
- else if (("-a" == $_SERVER['argv'][$i] || "--action" == $_SERVER['argv'][$i]))
- {
- $action = $_SERVER['argv'][$i + 1];
- }
- else
- {
- $key = $_SERVER['argv'][$i];
- $params[$key] = $_SERVER['argv'][$i + 1];
- }
- $i = $i + 2;
- }
- }
-
- foreach($params as $k => $v)
- {
- !isset($_GET[$k]) && $_GET[$k] = $v;
- }
- $this->module = $module;
- $this->action = $action;
- }
-
- public function matchingRoutingTable($url)
- {
- $ret = $this->routingTable['default'];
- $reqs = $this->routingTable['reqs'];
- $delimiter = $this->routingTable['delimiter'];
- $varprefix = $this->routingTable['varprefix'];
- $postfix = $this->routingTable['postfix'];
- $pattern = explode($delimiter, trim($this->routingTable['pattern'], $delimiter));
-
- if (is_string($url))
- {
- $url = rtrim($url, $postfix);
- $url = explode($delimiter, trim($url, $delimiter));
- }
- foreach($pattern as $k => $v)
- {
- if ($v[0] == $varprefix)
- {
-
- $varname = substr($v, 1);
-
- if (isset($url[$k]))
- {
- if (isset($reqs[$varname]))
- {
- $regex = "/^{$reqs[$varname]}\$/i";
- if (preg_match($regex, $url[$k]))
- {
- $ret[$varname] = $url[$k];
- }
- }
- }
- }
- else if ($v[0] == '*')
- {
-
- $pos = $k;
- while (isset($url[$pos]) && isset($url[$pos + 1]))
- {
- $ret[$url[$pos ++]] = urldecode($url[$pos]);
- $pos++;
- }
- }
- else
- {
-
- }
- }
- return $ret;
- }
- }
|