<?php
/**
* The router, config and lang class file of zentaophp framework.
*
* The author disclaims copyright to this source code. In place of
* a legal notice, here is a blessing:
*
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*/
/**
* The router class.
*
* @package framework
*/
class router
{
/**
* The base path of the zentaophp framework.
*
* @var string
* @access private
*/
private $basePath;
/**
* The root directory of the framwork($this->basePath/framework)
*
* @var string
* @access private
*/
private $frameRoot;
/**
* The root directory of the core library($this->basePath/lib)
*
* @var string
* @access private
*/
private $coreLibRoot;
/**
* The root directory of the app.
*
* @var string
* @access private
*/
private $appRoot;
/**
* The root directory of the app library($this->appRoot/lib).
*
* @var string
* @access private
*/
private $appLibRoot;
/**
* The root directory of temp.
*
* @var string
* @access private
*/
private $tmpRoot;
/**
* The root directory of cache.
*
* @var string
* @access private
*/
private $cacheRoot;
/**
* The root directory of log.
*
* @var string
* @access private
*/
private $logRoot;
/**
* The root directory of config.
*
* @var string
* @access private
*/
private $configRoot;
/**
* The root directory of module.
*
* @var string
* @access private
*/
private $moduleRoot;
/**
* The root directory of theme.
*
* @var string
* @access private
*/
private $themeRoot;
/**
* The lang of the client user.
*
* @var string
* @access private
*/
private $clientLang;
/**
* The theme of the client user.
*
* @var string
* @access private
*/
private $clientTheme;
/**
* The control object of current module.
*
* @var object
* @access public
*/
public $control;
/**
* The module name
*
* @var string
* @access private
*/
private $moduleName;
/**
* The control file of the module current visiting.
*
* @var string
* @access private
*/
private $controlFile;
/**
* The name of the method current visiting.
*
* @var string
* @access private
*/
private $methodName;
/**
* The action extension file of current method.
*
* @var string
* @access private
*/
private $extActionFile;
/**
* The URI.
*
* @var string
* @access private
*/
private $URI;
/**
* The params passed in through url.
*
* @var array
* @access private
*/
private $params;
/**
* The view type.
*
* @var string
* @access public
*/
public $viewType;
/**
* The global $config object.
*
* @var object
* @access public
*/
public $config;
/**
* The global $lang object.
*
* @var object
* @access public
*/
public $lang;
/**
* The global $dbh object, the database connection handler.
*
* @var object
* @access private
*/
public $dbh;
/**
* The slave database handler.
*
* @var object
* @access private
*/
public $slaveDBH;
/**
* The $post object, used to access the $_POST var.
*
* @var ojbect
* @access public
*/
public $post;
/**
* The $get object, used to access the $_GET var.
*
* @var ojbect
* @access public
*/
public $get;
/**
* The $session object, used to access the $_SESSION var.
*
* @var ojbect
* @access public
*/
public $session;
/**
* The $server object, used to access the $_SERVER var.
*
* @var ojbect
* @access public
*/
public $server;
/**
* The $cookie object, used to access the $_COOKIE var.
*
* @var ojbect
* @access public
*/
public $cookie;
/**
* The $global object, used to access the $_GLOBAL var.
*
* @var ojbect
* @access public
*/
public $global;
/**
* The construct function.
*
* Prepare all the paths, classes, super objects and so on.
* Notice:
* 1. You should use the createApp() method to get an instance of the router.
* 2. If the $appRoot is empty, the framework will comput the appRoot according the $appName
*
* @param string $appName the name of the app
* @param string $appRoot the root path of the app
* @access protected
* @return void
*/
protected function __construct($appName = 'demo', $appRoot = '')
{
$this->setPathFix();
$this->setBasePath();
$this->setFrameRoot();
$this->setCoreLibRoot();
$this->setAppRoot($appName, $appRoot);
$this->setAppLibRoot();
$this->setTmpRoot();
$this->setCacheRoot();
$this->setLogRoot();
$this->setConfigRoot();
$this->setModuleRoot();
$this->setThemeRoot();
$this->setSuperVars();
$this->loadConfig('common');
$this->setDebug();
$this->setErrorHandler();
$this->connectDB();
$this->setTimezone();
$this->setClientLang();
$this->loadLang('common');
$this->setClientTheme();
$this->loadClass('front', $static = true);
$this->loadClass('filter', $static = true);
$this->loadClass('dao', $static = true);
}
/**
* Create an application.
*
* <code>
* <?php
* $demo = router::createApp('demo');
* ?>
* or specify the root path of the app. Thus the app and framework can be seperated.
* <?php
* $demo = router::createApp('demo', '/home/app/demo');
* ?>
* </code>
* @param string $appName the name of the app
* @param string $appRoot the root path of the app
* @param string $className the name of the router class. When extends a child, you should pass in the child router class name.
* @static
* @access public
* @return object the app object
*/
public static function createApp($appName = 'demo', $appRoot = '', $className = 'router')
{
if(empty($className)) $className = __CLASS__;
return new $className($appName, $appRoot);
}
//-------------------- path related methods --------------------//
/**
* Set the path directory.
*
* @access protected
* @return void
*/
protected function setPathFix()
{
$this->pathFix = DIRECTORY_SEPARATOR;
}
/**
* Set the base path.
*
* @access protected
* @return void
*/
protected function setBasePath()
{
$this->basePath = realpath(dirname(dirname(__FILE__))) . $this->pathFix;
}
/**
* Set the frame root.
*
* @access protected
* @return void
*/
protected function setFrameRoot()
{
$this->frameRoot = $this->basePath . 'framework' . $this->pathFix;
}
/**
* Set the core library root.
*
* @access protected
* @return void
*/
protected function setCoreLibRoot()
{
$this->coreLibRoot = $this->basePath . 'lib' . $this->pathFix;
}
/**
* Set the app root.
*
* @param string $appName
* @param string $appRoot
* @access protected
* @return void
*/
protected function setAppRoot($appName = 'demo', $appRoot = '')
{
i