【发布时间】:2015-06-06 12:01:02
【问题描述】:
是的,所以我正在制作一个配置类,它将在 4 个主要位置使用不同文件类型的数组。现在我想让配置类在我使用以下命令时按顺序搜索这些位置
if (file_exists(ROOT . DS . 'Application/Config/' . APP_ENV . DS . $file)) {
$this->filePath = ROOT . DS . 'Application/Config/' . APP_ENV . DS . $file;
echo $this->filePath;
} else {
if (file_exists(ROOT . DS . "Application/Config/$file")) {
$this->filePath = ROOT . DS . "Application/Config/$file";
echo $this->filePath;
} else {
if (file_exists(CARBON_PATH . 'Config' . DS . APP_ENV . DS . $file)) {
$this->filePath = CARBON_PATH . 'Config' . DS . APP_ENV . DS . $file;
echo $this->filePath;
} else {
if (file_exists(CARBON_PATH . "Config/$file")) {
$this->filePath = CARBON_PATH . "Config/$file";
echo $this->filePath;
} else {
throw new \Exception("Unable to locate: $file, Please check it exists");
}
}
}
}
相当凌乱,不是很灵活。
我想要做的是仅在找到第一个匹配项后按文件名按相同顺序搜索位置然后返回带有配置类扩展名的文件,以使用正确的方法解析为php数组等等。
在这些位置搜索文件名的最佳方法是什么
示例 假设我们想要一个数据库配置文件,你可以看到有 2 个
ConfigLocation1/Dev/
/file.php
/database.json
ConfigLocation1/
/database.ini
/anotherfile.json
我想像这样使用函数
config::findFile('database');
然后它返回
$result = ConfigLocation1/Dev/database.json
如果这里没有找到,那么
$result = ConfigLocation1/database.ini
不太擅长解释事情,所以希望这个例子对你有帮助
【问题讨论】:
标签: php string file preg-match