/** * 初始化Redis配置数据和前缓存结构 */ private static function init() { $config = Config::getEnv("database.redis"); if (empty($config)) { throw new \InvalidArgumentException("配置项'database.redis'不能为空"); } $config = (array) $config; $db_conn_name_list = \Qp\Kernel\Database\QpDB::getConnectionNameList(); foreach ($config as $name => $data) { $name = strval($name); if (in_array($name, $db_conn_name_list)) { throw new \InvalidArgumentException("配置项'database.redis'的连接名'{$name}'和其他类型的数据库连接名冲突"); } $data = (array) $data; if (empty($data)) { throw new \InvalidArgumentException("配置项'database.redis.{$name}'必须为非空数组"); } $conn_name = $name; self::handleConfigArr($data, 'host', $host); self::handleConfigArr($data, 'auth', $auth); self::handleConfigArr($data, 'database', $database); self::handleConfigArr($data, 'port', $port); self::handleConfigArr($data, 'prefix', $prefix); $prefix = strval($prefix); if ($prefix == "") { $data['prefix'] = ":"; } else { $data['prefix'] = "_" . $prefix . ":"; } self::$config_list[$conn_name] = new ConfigObj($conn_name, $host, $auth, $database, $port, $data['prefix'], $data); } if (self::$frontCache === null) { self::$frontCache = new \Phalcon\Cache\Frontend\Data(self::$frontCacheData); } }
/** * 设置DI的数据库连接 * * @param \Phalcon\DI\FactoryDefault $di Phalcon的DI类 */ private function handleDBConnection(&$di) { foreach (Database\QpDB::getConnectionNameList() as $connection_name) { $di->set($connection_name, function () use($connection_name) { return DB::connection($connection_name); }); } }
/** * 获取链接对象 * * @param null $connection_name * @return \Phalcon\Db\Adapter\Pdo\Mysql|\Phalcon\Db\Adapter\Pdo\Oracle|\Phalcon\Db\Adapter\Pdo\Postgresql|\Phalcon\Db\Adapter\Pdo\Sqlite */ public static function connection($connection_name = null) { return Base::getConnection($connection_name); }