Example #1
0
 /**
  * 注册驱动
  * @param Yaf\Dispatcher $dispatcher 分发对象
  * @return void
  */
 public function _initDriver(Dispatcher $dispatcher)
 {
     if ($drivers = new Ini(sprintf("%sdriver.ini", CONF_PATH))) {
         // 注册数据库
         foreach ($drivers->get('database') as $name => $driver) {
             $database = PDOLib::getInstance($driver->type, $driver->host, $driver->port, $driver->dbname, $driver->charset, $driver->username, $driver->password);
             \Yaf\ENVIRON != 'product' and $database->setDebug();
             Registry::set("database.{$name}", $database);
         }
         // 注册redis
         foreach ($drivers->get('redis') as $name => $driver) {
             // 创建redis对象
             $redis = new \Redis();
             // 持久性连接
             $redis->pconnect($driver->host, $driver->port, (double) $driver->timeout);
             // 选项设置
             foreach ($driver->options as $key => $option) {
                 $redis->setOption(constant(sprintf("\\Redis::OPT_%s", strtoupper($key))), $option);
             }
             // 密码验证
             $driver->auth and $redis->auth($driver->auth);
             // 全局保存
             Registry::set("redis.{$name}", $redis);
         }
     }
 }
 /**
  * Redis数据库
  *
  * @throws Exception 'Redis is need redis Extension!
  */
 public function _initRedis()
 {
     if (!extension_loaded('redis')) {
         throw new Exception('Redis is need redis Extension!');
     }
     $conf = $this->config->get('resources.redis');
     if (!$conf) {
         throw new Exception('Not redis configure!', 503);
     }
     $redis = new KuRedis();
     /*
      * 连接Redis
      *
      * 当没有定义 port 时, 可以支持 sock.
      * 但是, 需要注意: 如果host是IP或者主机名时, port 的默认值是 6379
      */
     if ($conf->get('port')) {
         $status = $redis->pconnect($conf->get('host'), $conf->get('port'));
     } else {
         $status = $redis->pconnect($conf->get('host'));
     }
     if (!$status) {
         throw new \Exception('Unable to connect to the redis:' . $conf->get('host'));
     }
     // 是否有密码
     if ($conf->get('auth')) {
         $redis->auth($conf->get('auth'));
     }
     // 是否要切换Db
     if ($conf->get('db')) {
         $redis->select($conf->get('db'));
     }
     // Key前缀
     if ($conf->get('options.prefix')) {
         $redis->setOption(\Redis::OPT_PREFIX, $conf->get('options.prefix'));
     }
     Registry::set('redis', $redis);
 }
 /**
  * 连接 MySQL
  */
 public function _initMySQL()
 {
     $conf = new \Yaf\Config\Ini(APPLICATION_PATH . '/conf/database.ini', \Yaf\Application::app()->environ());
     $connects = $conf->get('resources.database');
     if (!$connects) {
         throw new \Exception('Not database configure', 503);
     }
     if (isset($connects['multi'])) {
         $connect = $connects['multi']['default'];
     } else {
         $connect = $connects;
     }
     $dbAdapter = new ZendDbAdapter($connect->toArray());
     Registry::set('defaultAdapter', $dbAdapter);
 }