Skip to content

Commit

Permalink
增加 driver 的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
breath-co2 committed Sep 12, 2013
1 parent cd3ca34 commit 67d29e4
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 39 deletions.
123 changes: 96 additions & 27 deletions core/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@
*/
define('DIR_MODULE', DIR_SYSTEM.'modules'.DS);

/**
* 驱动目录
*
* @var string
*/
define('DIR_DRIVER', DIR_SYSTEM.'drivers'.DS);

/**
* 第三方类库目录
*
Expand Down Expand Up @@ -331,7 +338,8 @@ abstract class Bootstrap
'project' => array(), // 项目类库
'team-library' => array('default'=>DIR_TEAM_LIBRARY), // Team公共类库
'library' => array(), // 类库包
'module' => array(), // Module
'driver' => array(), // 驱动
'module' => array(), // 组件
'core' => array('core'=>DIR_CORE), // 核心类库
);

Expand Down Expand Up @@ -712,6 +720,13 @@ public static function auto_load($class_name)
list($ns_name) = explode('_', $m[1], 2);
$new_class_name = $m[1];
}
else if (preg_match('#^driver_([a-z0-9]+)_driver_([a-z0-9_]+)$#', $class_name, $m))
{
# 驱动
$ns = 'driver';
$ns_name = $m[1];
$new_class_name = $m[2];
}
else
{
$ns = '';
Expand Down Expand Up @@ -761,22 +776,29 @@ public static function auto_load($class_name)

if ($ns)
{
if ($ns=='core')
{
$file = DIR_CORE . $dir_setting[0] . DS;
}
elseif ($ns=='module')
switch ($ns)
{
$file = DIR_MODULE;
case 'core':
$file = DIR_CORE . $dir_setting[0] . DS;
break;
case 'module':
$file = DIR_MODULE;

if ($new_class_name==$ns_name)
{
$file .= $ns_name . DS;
}
}
else
{
$file = DIR_LIBRARY . $ns_name . DS . $dir_setting[0] . DS;
if ($new_class_name==$ns_name)
{
$file .= $ns_name . DS;
}
break;
case 'driver':
$file = DIR_DRIVER . $ns_name . DS;
if (false===strpos($new_class_name, '_'))
{
$file .= $new_class_name . DS;
}
break;
default:
$file = DIR_LIBRARY . $ns_name . DS . $dir_setting[0] . DS;
break;
}

$file .= str_replace('_', DS, $class_file_name) . $dir_setting[1] . EXT;
Expand Down Expand Up @@ -806,17 +828,20 @@ public static function auto_load($class_name)
}
}

$include_path = self::$include_path;

# 没有找到文件且为项目类库,尝试在某个命名空间的类库中寻找
static $module_dir = array();
static $driver_dir = array();


list($tmp_prefix) = explode('_', $new_class_name, 2);
# 处理组件
list($tmp_prefix, $tmp_ns, $tmp_driver) = explode('_', $new_class_name, 4);
if (!isset($module_dir[$tmp_prefix]))
{
$module_dir[$tmp_prefix] = is_dir(DIR_MODULE .$tmp_prefix. DS);
}

$include_path = self::$include_path;
$include_path['module'] = array();
if ($module_dir[$tmp_prefix])
{
# 生成一个module路径,比如 Database_Driver_MySQL 就是在 module/database 中
Expand All @@ -826,7 +851,27 @@ public static function auto_load($class_name)
);
}

foreach (array('library', 'module', 'core') as $type)

# 处理驱动
if ($tmp_driver && $tmp_ns=='driver')
{
$driver = $tmp_ns .'/'. $tmp_driver;
if (!isset($driver_dir[$driver]))
{
$driver_dir[$driver] = is_dir(DIR_DRIVER .$tmp_prefix. DS .$tmp_driver. DS);
}

if ($driver_dir[$driver])
{
$include_path['driver'] = array
(
'driver' => DIR_DRIVER,
);
}
}


foreach (array('library', 'driver', 'module', 'core') as $type)
{
foreach ($include_path[$type] as $lib_ns=>$path)
{
Expand Down Expand Up @@ -912,6 +957,8 @@ public static function find_file($dir, $file, $ext=null, $auto_require=false)
# 是否只需要寻找到第一个文件
$only_need_one_file = true;

$file = str_replace('\\', '/', $file);

switch ($dir)
{
case 'models':
Expand Down Expand Up @@ -969,7 +1016,7 @@ public static function find_file($dir, $file, $ext=null, $auto_require=false)

if ($dir == 'classes')
{
# 类库目录增加 module 目录
# 处理 module 和 driver
if (false===strpos($file, '/'))
{
list($module_name) = explode('/', $file, 2);
Expand All @@ -978,36 +1025,58 @@ public static function find_file($dir, $file, $ext=null, $auto_require=false)
else
{
$module_dir = DIR_MODULE;

$driver_dir = DIR_DRIVER;
list($tmp_prefix, $tmp_ns, $tmp_driver, $tmp_name) = explode('/', $file, 4);
if ($tmp_ns=='driver' && $tmp_driver)
{
$driver_dir .= $tmp_prefix .DS;
if (!$tmp_name)
{
$tmp_name = $tmp_driver;
}
$driver_dir .= $tmp_driver .DS;
}

if (is_dir($driver_dir))
{
$include_path['driver'] = array($driver_dir);
}
}

if (is_dir($module_dir))
{
$include_path['module'] = array($module_dir);
}
}
else
{
$include_path['module'] = array();
}

foreach ($include_path as $key => $the_path)
{
if (!$the_path)continue;

if ($key=='module')
{
$tmpdir = '';
$tmpfile = $file;
}
elseif ($key==='driver')
{
$tmpfile = $tmp_name;
$tmpdir = '';
}
else
{
$tmpdir = $dir . DS;
$tmpfile = $file;
}

foreach ($the_path as $path)
{
$tmpfile = $path . $tmpdir . $file . $the_ext;
$tmp_filename = $path . $tmpdir . $tmpfile . $the_ext;

if (is_file($tmpfile))
if (is_file($tmp_filename))
{
$found_files[] = $tmpfile;
$found_files[] = $tmp_filename;
if ($only_need_one_file) break;
}
}
Expand Down
32 changes: 20 additions & 12 deletions core/classes/core.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1240,51 +1240,59 @@ public static function debug_path($file, $highlight=false)

$file = str_replace('\\', DS, $file);

if ( strpos($file, DIR_CORE) === 0 )
if (strpos($file, DIR_CORE) === 0)
{
$file = $l . './core/' . $r . substr($file, strlen(DIR_CORE));
}
elseif ( strpos($file, DIR_TEAM_LIBRARY) === 0 )
elseif (strpos($file, DIR_TEAM_LIBRARY) === 0)
{
$file = $l . './team-library/' . $r . substr($file, strlen(DIR_TEAM_LIBRARY));
}
elseif ( strpos($file, DIR_LIBRARY) === 0 )
elseif (strpos($file, DIR_LIBRARY) === 0)
{
$file = $l . './libraries/' . $r . substr($file, strlen(DIR_LIBRARY));
}
elseif ( strpos($file, DIR_PROJECT) === 0 )
elseif (strpos($file, DIR_MODULE) === 0)
{
$file = $l . './modules/' . $r . substr($file, strlen(DIR_MODULE));
}
elseif (strpos($file, DIR_DRIVER) === 0)
{
$file = $l . './drivers/' . $r . substr($file, strlen(DIR_DRIVER));
}
elseif (strpos($file, DIR_PROJECT) === 0)
{
$file = $l . './projects/' . $r . substr($file, strlen(DIR_PROJECT));
}
elseif ( strpos($file, DIR_TEMP) === 0 )
elseif (strpos($file, DIR_TEMP) === 0)
{
$file = $l . './data/temp/' . $r . substr($file, strlen(DIR_TEMP));
}
elseif ( strpos($file, DIR_LOG) === 0 )
elseif (strpos($file, DIR_LOG) === 0)
{
$file = $l . './data/log/' . $r . substr($file, strlen(DIR_LOG));
}
elseif ( strpos($file, DIR_CACHE) === 0 )
elseif (strpos($file, DIR_CACHE) === 0)
{
$file = $l . './data/cache/' . $r . substr($file, strlen(DIR_CACHE));
}
elseif ( strpos($file, DIR_DATA) === 0 )
elseif (strpos($file, DIR_DATA) === 0)
{
$file = $l . './data/' . $r . substr($file, strlen(DIR_DATA));
}
elseif ( strpos($file, DIR_ASSETS) === 0 )
elseif (strpos($file, DIR_ASSETS) === 0)
{
$file = $l . './wwwroot/assets/' . $r . substr($file, strlen(DIR_ASSETS));
}
elseif ( strpos($file, DIR_UPLOAD) === 0 )
elseif (strpos($file, DIR_UPLOAD) === 0)
{
$file = $l . './wwwroot/upload/' . $r . substr($file, strlen(DIR_UPLOAD));
}
elseif ( strpos($file, DIR_WWWROOT) === 0 )
elseif (strpos($file, DIR_WWWROOT) === 0)
{
$file = $l . './wwwroot/' . $r . substr($file, strlen(DIR_WWWROOT));
}
elseif ( strpos($file, DIR_SYSTEM) === 0 )
elseif (strpos($file, DIR_SYSTEM) === 0)
{
$file = $l . './' . $r . substr($file, strlen(DIR_SYSTEM));
}
Expand Down
2 changes: 2 additions & 0 deletions core/classes/file.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Core_File
DIR_WWWROOT,
DIR_TEAM_LIBRARY,
DIR_PROJECT,
DIR_MODULE,
DIR_DRIVER,
DIR_SYSTEM,
DIR_ASSETS,
);
Expand Down
1 change: 1 addition & 0 deletions drivers/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 67d29e4

Please sign in to comment.