https://github.com/morebec/filelocator
The FileLocator Component find files by their name using different strategies, such as recursive parent traversal, recursive (downward) traversal and at location
https://github.com/morebec/filelocator
file filelocator filelocator-component php traversal
Last synced: 10 months ago
JSON representation
The FileLocator Component find files by their name using different strategies, such as recursive parent traversal, recursive (downward) traversal and at location
- Host: GitHub
- URL: https://github.com/morebec/filelocator
- Owner: Morebec
- License: mit
- Created: 2019-12-07T22:47:29.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-12-10T17:03:40.000Z (over 6 years ago)
- Last Synced: 2025-02-01T23:45:15.631Z (over 1 year ago)
- Topics: file, filelocator, filelocator-component, php, traversal
- Language: PHP
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FileLocator
[](https://travis-ci.com/Morebec/FileLocator)
The FileLocator Component allows one to locate files using different strategies.
This is mostly useful to find configuration files or project roots, e.g. trying to find a possibly near `composer.json` file.
There are three strategies:
* Recursive up: Tries to find a file at specified location, and if not found goes one level up and repeats this process until the file is found, or until there is no longer a parent level.
* Recursive down: Tries to find a file in the at specified location, and if not found goes one level down
* At location: Tries to find a file only at the specified location without going up or down
## Installation
To install the library in a project, add these lines to your `composer.json` configuration file:
```
{
"repositories": [
{
"url": "https://github.com/Morebec/FileLocator.git",
"type": "git"
}
],
"require": {
"morebec/file-locator": "^1.0"
}
}
```
## Usage
The `FileLocator` has a single entry point where one needs to provide, the name of the file to find, the location and the strategy.
Here is an example to find composer in the parent directories of the current working directory:
```php
$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);
```
## Running Tests
```bash
composer test
```