private static $instance;
private $data; public static function & instance()
{
if (!isset(self::$instance))
{
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
} public function __construct()
{
$domain = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ?
$_SERVER['HTTP_X_FORWARDED_HOST'] :
(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''); $this->_load($domain);
} public function _load($domain)
{
$cache = Mycache::instance();
$key = $domain;
if(!($this->data = $cache->get($key)))
{
$this->data = DB::select()->from('sites')->where('domain','=',$domain)->execute()->current(); if($this->data)
{
$cache->set($key,$this->data);
}
else
{
//TODO not find the domain,trigger the error
echo 'no domain data';
exit;
//$this->request->status = 404;
//$this->request->redirect('system_error');
}
}
} /**
*
* get config data
* @return Array
*/
public function get($key = NULL)
{
if(empty($key))
{
return $this->data;
}
else
{
return isset($this->data[$key])?$this->data[$key]:'';
}
}
private static $instance;
private $data;
//单例
public static function & instance()
{
if (!isset(self::$instance))
{
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
//构造函数,根据访问的域名地址加载网站配置。
public function __construct()
{
$domain = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ?
$_SERVER['HTTP_X_FORWARDED_HOST'] :
(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''); $this->_load($domain);
}
//加载网站配置
public function _load($domain)
{
$cache = Mycache::instance();
$key = $domain;
//如果未命中缓存,则从数据库读取配置,并写入缓存。
if(!($this->data = $cache->get($key)))
{
$this->data = DB::select()->from('sites')->where('domain','=',$domain)->execute()->current(); if($this->data)
{
$cache->set($key,$this->data);
}
else //没有从数据库取到配置,提示错误信息。
{
//TODO not find the domain,trigger the error
echo 'no domain data';
exit;
//$this->request->status = 404;
//$this->request->redirect('system_error');
}
}
} /**
*
* get config data 获取配置信息,不指定$key的情况返回所有配置。
* @return Array
*/
public function get($key = NULL)
{
if(empty($key))
{
return $this->data;
}
else
{
return isset($this->data[$key])?$this->data[$key]:'';
}
}