查找文件
// 查找类文件路径
$file = Bootstrap::find_file('classes', 'Database');
// 查找一个视图文件
$file = Bootstrap::find_file('views', 'test');
// 查找一个自定义文件,注意第3个参数设置空表示后缀在文件名中
$file = Bootstrap::find_file('assets', 'test.css', '');
// 等价于
$file = Bootstrap::find_file('assets', 'test', '.css');
string Bootstrap::find_file( string $dir , string $file [, string $ext = null , boolean $auto_require = bool false ] )
参数列表
参数 类型 描述 默认值 $dir
string
目录 $file
string
文件 $ext
string
后缀 例如:.html,不指定(null)的话则自动设置后缀 null $auto_require
boolean
是否自动加载上来,对config,i18n无效 bool false
string
public static function find_file($dir, $file, $ext=null, $auto_require=false)
{
# 处理后缀
if (null===$ext)
{
$the_ext = EXT;
}
elseif (false===$ext || ''===$ext)
{
$the_ext = '';
}
elseif ($ext[0]!='.')
{
$the_ext = '.'.$ext;
}
# 是否只需要寻找到第一个文件
$only_need_one_file = true;
$file = str_replace('\\', '/', $file);
switch ($dir)
{
case 'models':
$file = strtolower(str_replace('_', '/', $file));
if (null===$ext)$the_ext = '.model' . EXT;
break;
case 'controllers':
$file = strtolower(str_replace('_', '/', $file));
if (null===$ext)$the_ext = '.controller' . EXT;
if (IS_SYSTEM_MODE)
{
$dir .= '-system';
}
elseif (IS_CLI)
{
$dir .= '-shell';
}
elseif (IS_ADMIN_MODE)
{
$dir .= '-admin';
}
elseif (IS_REST_MODE)
{
$dir .= '-rest';
}
break;
case 'i18n':
if (null===$ext)$the_ext = '.lang';
$only_need_one_file = false;
break;
case 'config':
if (null===$ext)$the_ext = '.config'. EXT;
break;
case 'views':
if (null===$ext)$the_ext = '.view' . EXT;
$file = strtolower($file);
break;
case 'orm':
if (null===$ext)$the_ext = '.orm' . EXT;
#orm
$file = preg_replace('#^(.*)_[a-z0-9]+$#i', '$1', $file);
$file = strtolower(str_replace('_', '/', $file));
break;
case 'classes':
$file = strtolower(str_replace('_', '/', $file));
if (null===$ext)
{
$the_ext = '.class' . EXT;
}
break;
default:
$the_ext = $ext;
break;
}
# 寻找到的文件
$found_files = array();
# 采用当前项目目录
$include_path = self::$include_path;
if ($dir == 'classes')
{
# 处理 module 和 driver
if (false===strpos($file, '/'))
{
list($module_name) = explode('/', $file, 2);
$module_dir = DIR_MODULE . $module_name . DS;
}
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);
}
}
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)
{
$tmp_filename = $path . $tmpdir . $tmpfile . $the_ext;
if (is_file($tmp_filename))
{
$found_files[] = $tmp_filename;
if ($only_need_one_file) break;
}
}
}
if ($found_files)
{
if ($only_need_one_file)
{
if ($auto_require)
{
require $found_files[0];
}
return $found_files[0];
}
else
{
return $found_files;
}
}
}