<?php
/**
* The router class file of ZenTaoPHP.
*
* ZenTaoPHP is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* ZenTaoPHP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ZenTaoPHP. If not, see <http://www.gnu.org/licenses/>.
*
* @copyright Copyright: 2009 Chunsheng Wang
* @author Chunsheng Wang <wwccss@263.net>
* @package ZenTaoPHP
* @version $Id: router.class.php 1176 2009-05-24 15:09:00Z wwccss $
* @link http://www.zentao.cn
*/
class router
{
private $pathFix = ''; // 文件系统的路径分隔符。
private $basePath = ''; // 框架的基准路径。
private $frameRoot = ''; // 框架基类路径。
private $coreLibRoot = ''; // 框架所带的library目录。
private $appRoot = ''; // 当前应用程序所在的目录。
private $appLibRoot = ''; // 应用程序的libraray目录。
private $cacheRoot = ''; // 缓存文件所在的根目录。
private $configRoot = ''; // 配置文件所在的根目录。
private $moduleRoot = ''; // 模块文件所在的根目录。
private $themeRoot = ''; // 主题文件所在的根目录。
private $clientLang = ''; // 用户所使用的语言。
private $clientTheme = ''; // 用户所使用的主题。
private $moduleName = ''; // 当前需要加载的模块名称。
private $controlFile = ''; // 当前模块对应的control文件。
private $methodName = ''; // 需要调用的controlFile的方法。
private $URI = ''; // 当前请求的URI。
private $params = array(); // 传递的参数。
private $viewType = ''; // 视图格式。html|json|xml|txt
private $config = ''; // config对象。
private $lang = ''; // lang对象。
private $dbh = ''; // dbh对象。
/* 构造函数。*/
private function __construct($appName = 'demo', $appRoot = '')
{
$this->setPathFix();
$this->setBasePath();
$this->setFrameRoot();
$this->setCoreLibRoot();
$this->setAppRoot($appName, $appRoot);
$this->setAppLibRoot();
$this->setCacheRoot();
$this->setConfigRoot();
$this->setModuleRoot();
$this->setThemeRoot();
}
/* 生成一个应用。*/
public static function createApp($appName = 'demo', $appRoot = '')
{
return new router($appName, $appRoot);
}
//-------------------- 路径相关的方法。--------------------//
/* 设置路径分隔符。*/
protected function setPathFix()
{
$this->pathFix = DIRECTORY_SEPARATOR;
}
/* 设置整个框架所在的根目录。*/
protected function setBasePath()
{
$this->basePath = realpath(dirname(dirname(__FILE__))) . $this->pathFix;
}
/* 设置框架核心类文件所在的根目录。*/
protected function setFrameRoot()
{
$this->frameRoot = $this->basePath . 'framework' . $this->pathFix;
}
/* 设置coreLib文件的根目录。*/
protected function setCoreLibRoot()
{
$this->coreLibRoot = $this->basePath . 'lib' . $this->pathFix;
}
/* 设置应用程序所在的根目录。默认情况下面根据appName来进行计算,如果指定了appRoot,直接用之。*/
protected function setAppRoot($appName = 'demo', $appRoot = '')
{
if(empty($appRoot))
{
$this->appRoot = $this->basePath . 'app' . $this->pathFix . $appName . $this->pathFix;
}
else
{
$this->appRoot = realpath($appRoot) . $this->pathFix;
}
if(!is_dir($this->appRoot)) $this->error("The app you call not noud in {$this->appRoot}", __FILE__, __LINE__, $exit = true);
}
/* 设置appLib文件的根目录。*/
protected function setAppLibRoot()
{
$this->appLibRoot = $this->appRoot . 'lib' . $this->pathFix;
}
/* 设置缓存文件所在的根目录。*/
protected function setCacheRoot()
{
$this->cacheRoot = $this->appRoot . 'cache' . $this->pathFix;
}
/* 设置配置文件所在的根目录。*/
protected function setConfigRoot()
{
$this->configRoot = $this->appRoot . 'config' . $this->pathFix;
}
/* 设置module所在的根目录。*/
protected function setModuleRoot()
{
$this->moduleRoot = $this->appRoot . 'module' . $this->pathFix;
}
/* 设置客户端主题文件所在的根目录。*/
protected function setThemeRoot()
{
$this->themeRoot = $this->appRoot . 'www' . $this->pathFix . 'theme' . $this->pathFix;
}
/* 返回路径分隔符。*/
public function getPathFix()
{
return $this->pathFix;
}
/* 返回整个框架的所在的目录。*/
public function getBasePath()
{
return $this->basePath;
}
/* 返回框架核心类文件所在的根目录。*/
public function getFrameRoot()
{
return $this->frameRoot;
}
/* 返回lib文件所在的根目录。*/
public function getCoreLibRoot()
{
return $this->coreLibRoot;
}
/* 返回应用程序所在的根目录。*/
public function getAppRoot()
{
return $this->appRoot;
}
/* 返回appLib文件所在的根目录。*/
public function getAppLibRoot()
{
return $this->appLibRoot;
}
/* 返回缓存文件所在的根目录。*/
public function getCacheRoot()
{
return $this->cacheRoot;
}
/* 返回配置文件所在的根目录。*/
public function getConfigRoot()
{
return $this->configRoot;
}
/* 返回模块文件所在的根目录。*/
public function getModuleRoot()
{
return $this->moduleRoot;
}
/* 返回主题文件所在的根目录。*/
public function getThemeRoot()
{
return $this->themeRoot;
}
//-------------------- 客户端环境设置。--------------------//
/* 设置客户端所使用的语言。*/
public function setClientLang($lang = '')
{
if(!empty($lang))
{
$this->clientLang = $lang;
}
elseif(isset($_SESSION['lang']))
{
$this->clientLang = $_SESSION['lang'];
}
elseif(isset($_COOKIE['lang']))
{
$this->clientLang = $_COOKIE['lang'];
}
elseif(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
$this->clientLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], ','));
}
if(!empty($this->clientLang))
{
$this->clientLang = strtolower($this->clientLang);
if(strpos($this->config->langs, $this->clientLang) === false)
{
$this->clientLang = $this->config->default->lang;
}
}
else
{
$this->clientLang = $this->config->default->lang;
}
setcookie('lang', $this->clientLang, $this->config->cookieLife, $this->config->cookiePath);
}
/* 返回客户端使用的语言。*/
public function getClientLang()
{
return $this->clientLang;
}
/* 设置客户端所使用的主题。*/
public function setClientTheme($theme = '')
{
if(!empty($theme))
{
$this->clientTheme = $theme;
}
elseif(isset($_SESSION['theme']))
{
$this->clientTheme = $_SESSION['theme'];
}
elseif(isset($_COOKIE['theme']))
{
$this->clientTheme = $_COOKIE['theme'];
}
if(!empty($this->clientTheme))
{
$this->clientTheme = strtolower($this->clientTheme);
if(strpos($this->config->themes, $this->clientTheme) === false)
{
$this->clientTheme = $this->config->default->theme