morebec / file-locator
文件定位组件通过不同的策略(如递归父级遍历、递归(向下)遍历和定位处)根据文件名查找文件。
2.0
2019-12-10 17:01 UTC
Requires
- morebec/value-objects: ^1.0
- symfony/finder: ^5.0
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-09-09 12:40:50 UTC
README
文件定位组件允许您使用不同的策略来定位文件。这通常用于查找配置文件或项目根目录,例如尝试查找可能接近的 composer.json
文件。
有以下三种策略
- 递归向上:尝试在指定位置查找文件,如果找不到,则向上一级重复此过程,直到找到文件或没有更多父级级别。
- 递归向下:尝试在指定位置查找文件,如果找不到,则向下一级
- 在位置:尝试只在指定位置查找文件,不向上或向下移动
安装
要在项目中安装库,请将这些行添加到您的 composer.json
配置文件中
{
"repositories": [
{
"url": "https://github.com/Morebec/FileLocator.git",
"type": "git"
}
],
"require": {
"morebec/file-locator": "^1.0"
}
}
使用方法
FileLocator
有一个单一的入口点,需要提供要查找的文件名、位置和策略。
以下是一个示例,展示如何在当前工作目录的父目录中查找 composer
$locator = new FileLocator(); $file = $locator->find( 'composer.json', Directory::fromStringPath(getcwd()), FileLocatorStrategy::RECURSIVE_UP() ); if(!$file) { throw new \Exception('Composer could not be found.'); } // Parse composer file $composerConfig = json_decode($file->getContents(), true);
运行测试
composer test