Example #1
0
 /**
  * 连接数据库
  *
  * @param string $dbFlag 数据库配置命名空间
  */
 public static function instance($dbFlag = '')
 {
     if (empty($dbFlag)) {
         throw new DbException('Empty param: dbFlag');
     }
     if (!isset(Y::$app->db[$dbFlag])) {
         throw new InvalidConfigException('Unknow db config: ' . $dbFlag);
     }
     if (!isset(static::$_links[$dbFlag]) || null === static::$_links[$dbFlag]) {
         $config = Y::$app->db[$dbFlag];
         $dsn = $config['dsn'];
         $driver = static::getDriverName($dsn);
         $dbClass = static::$_dbNamespace . '\\' . $driver . '\\Db';
         $dbFile = Y::namespaceTranslate($dbClass);
         if (!is_file($dbFile)) {
             throw new FileNotFoundException('The Classfile: ' . $dbFile . ' not found');
         }
         try {
             static::$_links[$dbFlag] = new $dbClass($dsn, $config['username'], $config['password']);
             static::$_links[$dbFlag]->initConnection($config);
         } catch (PDOException $e) {
             static::$_links[$dbFlag] = null;
             throw new DbException('Failed to connect to database');
         }
     }
     return static::$_links[$dbFlag];
 }
Example #2
0
 /**
  * 得到视图所在路径
  *
  * @return string 视图路径
  */
 public function findViewFile($view)
 {
     $app = Y::$app;
     $path = '';
     if (null !== $app->moduleId) {
         $path = Y::namespaceTranslate($app->modules[$app->moduleId], '');
     } else {
         $path = Y::namespaceTranslate('app', '');
     }
     // 模块无子目录 普通控制器有子目录
     // 注意转换 namespace path 为目录路径
     return $path . '/views/' . (null === $app->routePrefix ? '.' : str_replace('\\', '/', $app->routePrefix)) . '/' . $view . $this->defaultExtension;
 }
Example #3
0
 public static function instance($cacheFlag = '')
 {
     if (empty($cacheFlag)) {
         throw new CacheException('Empty param: cacheFlag');
     }
     if (!isset(Y::$app->cache[$cacheFlag])) {
         throw new InvalidConfigException('Unknow cache config: ' . $cacheFlag);
     }
     if (!isset(Y::$app->cache[$cacheFlag]['class'])) {
         throw new InvalidConfigException('Lost `class` config item of the cache class');
     }
     if (!isset(static::$_caches[$cacheFlag]) || null === static::$_caches[$cacheFlag]) {
         $config = Y::$app->cache[$cacheFlag];
         $cacheClass = $config['class'];
         $cacheFile = Y::namespaceTranslate($cacheClass);
         if (!is_file($cacheFile)) {
             throw new FileNotFoundException('The cacheFile: ' . $cacheFile . ' not found');
         }
         static::$_caches[$cacheFlag] = new $cacheClass($config);
         static::$_caches[$cacheFlag]->init();
     }
     return static::$_caches[$cacheFlag];
 }