承接 loilo/find-up 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

loilo/find-up

Composer 安装命令:

composer require loilo/find-up

包简介

Find a file by walking up ancestor directories

README 文档

README

FindUp logo: a folder icon with an upwards arrow in front

FindUp

Tests Version on packagist.org

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.json on the way up, the find method would return null.

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::STOP constant.

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::SKIP constant.

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 loilo/find-up 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 413
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 11
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-16