loilo/find-up
Composer 安装命令:
composer require loilo/find-up
包简介
Find a file by walking up ancestor directories
README 文档
README
FindUp
Find a file by walking up ancestor directories (e.g. a composer.json from inside a project).
Installation
composer require loilo/find-up
Usage
Basic Example
/var/www
└── project
├── composer.json
└── src
└── foo
├── bar
└── example.php
example.php:
use Loilo\FindUp\Up; // Get the project's composer.json's path by // walking up from /var/www/project/src/foo Up::find('composer.json') === '/var/www/project/composer.json';
This is the most basic example how to use this package. The finder will look for a composer.json file, starting at the directory where Up::find() was called (which is /var/www/project/src/foo).
Note: If there was no
composer.jsonon the way up, thefindmethod would returnnull.
Starting Directory
As can be seen in the basic example above, the default starting directory is the one from where Up::find() is called. Alternatively, a starting directory can be passed to the find method as a second argument:
// Start from the current working directory Up::find('composer.json', getcwd());
Advanced Matching
Instead of a file name to search for, a (non-string) callable may be passed as the find method's first argument. It will receive each encountered file on the way up and decides whether it is the searched file.
Assuming we're starting in the example.php from the basic example directory tree above, we can find the path to the composer.json with the following call:
Up::find(function ($file, $directory) { return $file === 'composer.json'; });
Stop Searching Prematurely
You can stop the search before reaching the filesystem root by returning an Up::stop() call. For example, if you know that your composer.json is not found upwards from the /var/www folder, you can break out of the search:
Up::find(function ($file, $directory) { if ($directory === '/var/www') { return Up::stop(); } return $file === 'composer.json'; });
When returning Up::stop(), the Up::find() method will return null by default.
You may however pass a path to the stop() method that will be used as the result. If the path is not absolute, it will be resolved against the current $directory:
Up::find(function ($file, $directory) { if ($directory === '/var/www') { return Up::stop('stop.txt'); } return false; }) === '/var/www/stop.txt';
Note: In previous versions, the way to stop searching was to return the
Up::STOPconstant.This technique still works, but it's deprecated and it's recommended to use
Up::stop()instead.
Skip Folder
If you are in a directory of which you're sure it does not contain the searched file, you may avoid walking through all its files by returning Up::skip():
Up::find(function ($file, $directory) { // Skip "src" directories if (basename($directory) === 'src') { return Up::skip(); } return $file === 'composer.json'; });
By default, the Up::skip() method will only skip the currently scanned directory. You may however pass a (positive) number that indicates how many levels of directories should be skipped.
... // skip the current directory and its parent directory, // continue with grandparent directory return Up::skip(2)
Note: In previous versions, the way to skip scanning a folder was to return the
Up::SKIPconstant.This technique still works, but it's deprecated and it's recommended to use
Up::skip()instead.
Jump to Other Folder
If you want to stop searching the current directory tree altogether and continue from another path, you can return an Up::jump() call:
Up::find(function ($file, $directory) { if ($directory === '/var/www/project') { return Up::jump('/var/www/other-project'); } return $file === 'composer.json'; });
Note: You can only jump to a directory you have not previously visited in the current search. This serves to avoid infinite loops.
loilo/find-up 适用场景与选型建议
loilo/find-up 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 413 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 07 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 loilo/find-up 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 loilo/find-up 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 413
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-16