/** * 获得一个数据库连接对象 * * $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; }
/** * 构造函数 * * @param array $app_config * * 构造应用程序对象 */ protected function __construct(array $app_config) { // #IFDEF DEBUG global $g_boot_time; QLog::log('--- STARTUP TIME --- ' . $g_boot_time, QLog::DEBUG); // #ENDIF /** * 初始化运行环境 */ // 禁止 magic quotes set_magic_quotes_runtime(0); // 处理被 magic quotes 自动转义过的数据 if (get_magic_quotes_gpc()) { $in = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST); while (list($k, $v) = each($in)) { foreach ($v as $key => $val) { if (!is_array($val)) { $in[$k][$key] = stripslashes($val); continue; } $in[] =& $in[$k][$key]; } } unset($in); } // 设置异常处理函数 set_exception_handler(array($this, 'exception_handler')); // 初始化应用程序设置 $this->_app_config = $app_config; $this->_initConfig(); Q::replaceIni('app_config', $app_config); // 设置默认的时区 date_default_timezone_set(Q::ini('l10n_default_timezone')); // 设置 session 服务 if (Q::ini('runtime_session_provider')) { Q::loadClass(Q::ini('runtime_session_provider')); } // 打开 session if (Q::ini('runtime_session_start')) { session_start(); // #IFDEF DEBUG QLog::log('session_start()', QLog::DEBUG); QLog::log('session_id: ' . session_id(), QLog::DEBUG); // #ENDIF } // 导入类搜索路径 Q::import($app_config['APP_DIR']); Q::import($app_config['APP_DIR'] . '/model'); Q::import($app_config['MODULE_DIR']); // 注册应用程序对象 Q::register($this, 'app'); }
/** * 测试对象注册 */ function testRegister() { $obj1 = new Class2(); Q::register($obj1); Q::register($obj1, 'object-1-1'); $this->assertTrue(Q::isRegistered('class2')); $this->assertTrue(Q::isRegistered('Class2')); $this->assertFalse(Q::isRegistered('class22')); $obj = Q::registry('class2'); $this->assertSame($obj1, $obj); $obj = Q::registry('object-1-1'); $this->assertSame($obj1, $obj); }
/** * 根据类名称获得表数据入口对象 * * @param string $table_class * @param array $table_config * * @return QDB_Table */ protected function _tableByClass($table_class, array $table_config = array()) { $obj_id = 'activerecord_table_' . strtolower($table_class); if (Q::isRegistered($obj_id)) { return Q::registry($obj_id); } else { $table = new $table_class($table_config); Q::register($table, $obj_id); return $table; } }
/** * 渲染指定的视图文件 * * 渲染时,视图要使用的数据保存在控件的 $view 属性中。 * * @param string $viewname * @param boolean $return * * @return string */ protected function _renderBlock($viewname, $return = false) { if (!is_object($this->_view_adapter)) { $adapter_class = is_null($this->_view_adapter_class) ? $this->context->getIni('view_adapter') : $this->_view_adapter_class; $adapter_obj_id = "webcontrols_{$adapter_class}"; if (Q::isRegistered($adapter_obj_id)) { /** * @var QView_Adapter_Abstract */ $adapter = Q::registry($adapter_obj_id); } else { /** * @var QView_Adapter_Abstract */ $adapter = new $adapter_class($this->context); Q::register($adapter, $adapter_obj_id); } } else { $adapter = $this->_view_adapter; } $adapter->clear(); $adapter->assign($this->view); $adapter->assign('_ctx', $this->context); $filename = QView::getControlViewFilename($this->context, $adapter, $viewname, $this->_controls_view_dir); if ($return) { return $adapter->fetch($filename); } else { return $adapter->display($filename); } }
/** * 渲染表单 * * @param QContext $context * @param boolean $return * * @return string */ function render(QContext $context, $return = false) { $this->_event(self::BEFORE_RENDER_FORM, $this); if (!is_object($this->_view_adapter)) { $adapter_class = is_null($this->_view_adapter_class) ? $context->getIni('view_adapter') : $this->_view_adapter_class; $adapter_obj_id = "form_{$adapter_class}"; if (Q::isRegistered($adapter_obj_id)) { $adapter = Q::registry($adapter_obj_id); } else { $adapter = new $adapter_class($context); Q::register($adapter, $adapter_obj_id); } } else { $adapter = $this->_view_adapter; } /* @var $adapter QView_Adapter_Abstract */ $viewname = !empty($this->_viewname) ? $this->_viewname : 'form'; $filename = QView::getViewLayoutsFilename($adapter->context, $adapter, $viewname); $adapter->clear(); $adapter->assign('form', $this); $adapter->assign('_ctx', $context); $output = $adapter->fetch($filename); $this->_event(self::AFTER_RENDER_FORM, $this); if (!$return) { echo $output; $output = null; } return $output; }