Example #1
0
 /**
  * load config from db
  * @access public
  * @static
  * @return boolean
  * @example
  * <p>
  * config::loadDbConfig();
  * </p>
  */
 public static function loadDbConfig()
 {
     $cacheConfig = self::get('cache');
     if ($cacheConfig) {
         //从cache中读取dbConfig,如中没有命中,则继续从DB中读取
         cacheLayer::setConfig($cacheConfig);
         $dbConfigCacheKey = cacheLayer::getKey('co', 'platform', 'config', 'loadDbConfig');
         $dbConfig = cacheLayer::read($dbConfigCacheKey);
         if ($dbConfig) {
             self::$_config = array_merge_recursive(self::$_config, $dbConfig);
             log::accessLog('Execute loadDbConfig() successful(from cache)');
             log::returnLog(true);
             return true;
         }
     }
     $mongoConfig = self::get('database.default.params');
     if (is_array($mongoConfig) && count($mongoConfig)) {
         //从mongo Db中读取config
         $accessMongo = new accessMongo($mongoConfig);
         $accessMongo->connect();
         $dbo = $accessMongo->getDb();
         $connection = $dbo->selectCollection('sg_configuration');
         $configures = iterator_to_array($connection->find(array('published' => 1), array('name', 'value', 'default_value')));
         $config = array();
         foreach ($configures as $configure) {
             if (is_numeric($configure['default_value'])) {
                 if (!is_numeric($configure['value'])) {
                     $config[$configure['name']] = $configure['default_value'];
                 } else {
                     $config[$configure['name']] = $configure['value'];
                 }
             } else {
                 if (empty($configure['value'])) {
                     $config[$configure['name']] = $configure['default_value'];
                 } else {
                     $config[$configure['name']] = $configure['value'];
                 }
             }
         }
         if (isset($dbConfigCacheKey)) {
             cacheLayer::write($dbConfigCacheKey, $config);
         }
         self::$_config = array_merge_recursive(self::$_config, $config);
         log::accessLog('Execute loadDbConfig() successful(from mongo db)');
         log::returnLog(true);
         return true;
     } else {
         log::errorLog('mongo config error');
         return false;
     }
 }