Пример #1
0
 /**
  * 载入缓存的 YAML 解析结果,如果缓存失效,则重新解析并生成缓存
  *
  * @param string $filename
  *   要解析的 yaml 文件名
  * @param array $replace
  *   对于 YAML 内容要进行自动替换的字符串对
  * @param string $cache_backend
  *   要使用的缓存后端
  *
  * @return array
  *   解析结果
  */
 static function loadCached($filename, array $replace = null, $cache_backend = null)
 {
     static $cache_obj = null;
     if (!is_file($filename)) {
         throw new QException_FileNotFound($filename);
     }
     $policy = array('lifetime' => 86400, 'serialize' => true);
     $mtime = filemtime($filename);
     $id = 'yaml_cache_' . md5($filename);
     if (is_null($cache_backend)) {
         if (is_null($cache_obj)) {
             $cache_obj = Q::getSingleton(Q::getIni('runtime_cache_backend'));
         }
         $cache = $cache_obj;
     } else {
         $cache = self::getSingleton($cache_backend);
     }
     /* @var $cache QCache_File */
     $data = $cache->get($id, $policy);
     if (!isset($data['yaml']) || empty($data['mtime']) || $data['mtime'] < $mtime) {
         // 缓存失效
         $data = array('mtime' => $mtime, 'yaml' => self::load($filename, $replace));
         $cache->set($id, $data, $policy);
     }
     return $data['yaml'];
 }
Пример #2
0
 /**
  * 获得一个数据库连接对象
  *
  * $dsn_name 参数指定要使用应用程序设置中的哪一个项目作为创建数据库连接的 DSN 信息。
  * 对于同样的 DSN 信息,只会返回一个数据库连接对象。
  *
  * @param string $dsn_name
  *
  * @return QDB_Adapter_Abstract
  */
 static function getConn($dsn_name = null)
 {
     $default = empty($dsn_name);
     if ($default && Q::isRegistered('dbo_default')) {
         return Q::registry('dbo_default');
     }
     if (empty($dsn_name)) {
         $dsn = Q::getIni('db_dsn_pool/default');
     } else {
         $dsn = Q::getIni('db_dsn_pool/' . $dsn_name);
     }
     if (empty($dsn)) {
         // LC_MSG: Invalid DSN.
         trigger_error('invalid dsn');
         throw new QException(__('Invalid DSN.'));
     }
     $dbtype = $dsn['driver'];
     $objid = "dbo_{$dbtype}_" . md5(serialize($dsn));
     if (Q::isRegistered($objid)) {
         return Q::registry($objid);
     }
     $class_name = 'QDB_Adapter_' . ucfirst($dbtype);
     $dbo = new $class_name($dsn, $objid);
     Q::register($dbo, $objid);
     if ($default) {
         Q::register($dbo, 'dbo_default');
     }
     return $dbo;
 }
Пример #3
0
 function __construct()
 {
     $dsn = Q::getIni('db_dsn_pool/default');
     if (empty($dsn)) {
         Q::setIni('db_dsn_pool/default', Q::getIni('db_dsn_mysql'));
     }
     parent::__construct();
 }
Пример #4
0
 /**
  * 构造函数
  *
  * @param 默认的缓存策略 $default_policy
  */
 function __construct(array $default_policy = null)
 {
     if (!is_null($default_policy)) {
         $this->_default_policy = array_merge($this->_default_policy, $default_policy);
     }
     if (empty($this->_default_policy['cache_dir'])) {
         $this->_default_policy['cache_dir'] = Q::getIni('runtime_cache_dir');
     }
 }
Пример #5
0
 function testUnsetIni()
 {
     Q::unsetIni('root/node/item');
     $test = Q::getIni('root/node');
     $this->assertTrue(empty($test), "Q::getIni('root/node') == empty");
     Q::unsetIni('root');
     $test = Q::getIni('root');
     $this->assertTrue(empty($test), "Q::getIni('root') == empty");
 }
Пример #6
0
 protected function setUp()
 {
     $dsn = Q::getIni('db_dsn_pool/default');
     if (empty($dsn)) {
         Q::setIni('db_dsn_pool/default', Q::getIni('db_dsn_mysql'));
     }
     $this->_conn = QDB::getConn();
     $this->_conn->startTrans();
 }
Пример #7
0
 protected function setUp()
 {
     $dsn = Q::getIni('db_dsn_pool/default');
     if (empty($dsn)) {
         Q::setIni('db_dsn_pool/default', Q::getIni('db_dsn_mysql'));
     }
     $conn = QDB::getConn();
     $params = array('name' => 'posts', 'pk' => 'post_id', 'conn' => $conn);
     $this->table = new QDB_Table($params);
 }
Пример #8
0
 /**
  * 设置当前数据表的元数据
  */
 protected function _setupMeta()
 {
     $table_name = $this->getFullTableName();
     $this->_cache_id = $this->_conn->getID() . '-' . $table_name;
     if (isset(self::$_meta[$this->_cache_id])) {
         return;
     }
     $cached = Q::getIni('db_meta_cached');
     if ($cached) {
         // 尝试从缓存读取
         $policy = array('encoding_filename' => true, 'serialize' => true, 'life_time' => Q::getIni('db_meta_lifetime'), 'cache_dir' => Q::getIni('runtime_cache_dir'));
         $backend = Q::getIni('db_meta_cache_backend');
         $data = Q::getCache($this->_cache_id, $policy, $backend);
         if (is_array($data) && !empty($data)) {
             self::$_meta[$this->_cache_id] = $data[0];
             self::$_fields[$this->_cache_id] = $data[1];
             return;
         }
     }
     // 从数据库获得 meta
     $meta = $this->_conn->metaColumns($table_name);
     $fields = array();
     foreach ($meta as $field) {
         $fields[$field['name']] = true;
     }
     self::$_meta[$this->_cache_id] = $meta;
     self::$_fields[$this->_cache_id] = $fields;
     $data = array($meta, $fields);
     if ($cached) {
         // 缓存数据
         Q::setCache($this->_cache_id, $data, $policy, $backend);
     }
 }
Пример #9
0
// $Id$
/**
 * @file
 * 定义 QView_Adapter_Smarty 类
 *
 * @ingroup view
 *
 * @{
 */
// {{{ includes
do {
    if (class_exists('Smarty', false)) {
        break;
    }
    $view_config = (array) Q::getIni('view_config');
    if (empty($view_config['smarty_dir']) && !defined('SMARTY_DIR')) {
        throw new QView_Exception(__('Application settings "view_config[\'smarty_dir\']" ' . 'and constant SMARTY_DIR must be defined for QView_Adapter_Smarty.'));
    }
    if (empty($view_config['smarty_dir'])) {
        $view_config['smarty_dir'] = SMARTY_DIR;
    }
    Q::loadClassFile('Smarty.class.php', $view_config['smarty_dir'], 'Smarty');
} while (false);
// }}}
/**
 * QView_Adapter_Smarty 提供了对 Smarty 模板引擎的支持
 */
class QView_Adapter_Smarty extends QView_Adapter_Abstract
{
    public $tpl_file_ext = '.html';
Пример #10
0
 function testGetTablePrefix()
 {
     $prefix = $this->dbo->getTablePrefix();
     $this->assertEquals(Q::getIni('db_dsn_pool/default/prefix'), $prefix);
 }
Пример #11
0
 /**
  * 断言
  *
  * 如果 $bool 为 false,则调用 assert() 方法。这会导致一个警告信息或中断执行。
  *
  * @param boolean $bool
  *   断言结果
  * @param string $message
  *   要显示的断言信息
  */
 static function assert($bool, $message = null)
 {
     if (!self::$_assert_enabled || $bool) {
         return;
     }
     if (self::$_firephp_enabled) {
         QDebug_FirePHP::assert($bool, $message);
         return;
     }
     if (Q::getIni('assert_warning')) {
         trigger_error($message . "\nAssertion failed", E_USER_WARNING);
         self::dumpTrace();
     }
     if (Q::getIni('assert_exception')) {
         throw new QDebug_Assert_Failed($message);
     }
 }
Пример #12
0
 /**
  * 追加日志到日志缓存
  *
  * @param string $msg
  * @param int $type
  */
 function append($msg, $type = self::DEBUG)
 {
     if (!Q::getIni('log_enabled')) {
         return;
     }
     if (isset($this->_priorities['index'][$type])) {
         $type_name = $this->_priorities['index'][$type];
     } elseif (isset($this->_priorities['names'][$type])) {
         $type_name = $type;
         $type = $this->_priorities['names'][$type];
     } else {
         return;
     }
     $msg = str_replace(array("\n", "\r"), '', $msg);
     $this->_log[] = array(time(), $msg, $type, $type_name);
     $this->_cached_size += strlen($msg);
     unset($msg);
     if ($this->_cached_size >= $this->_cache_chunk_size) {
         $this->flush();
     }
 }
Пример #13
0
/**
 * QeePHP 内部使用的多语言翻译函数
 *
 * 应用程序应该使用 QTranslate 组件实现多语言界面。
 *
 * @return $msg
 */
function __()
{
    $args = func_get_args();
    $msg = array_shift($args);
    $language = strtolower(Q::getIni('error_language'));
    $messages = Q::loadFile('lc_messages.php', Q_DIR . '/_lang/' . $language, false);
    if (isset($messages[$msg])) {
        $msg = $messages[$msg];
    }
    array_unshift($args, $msg);
    return call_user_func_array('sprintf', $args);
}