nubs/which
Composer 安装命令:
composer require nubs/which
包简介
A library for locating commands in a PATH.
README 文档
README
A PHP library for locating commands in a PATH.
Requirements
This library requires PHP 5.6, or newer.
Installation
This package uses composer so you can just add
nubs/which as a dependency to your composer.json file or execute the
following command:
composer require nubs/which
Example
Here is a quick example to demonstrate how this library is generally meant to be used:
$locatorFactory = new \Nubs\Which\LocatorFactory\PlatformLocatorFactory(); $locator = $locatorFactory->create(); echo $locator->locate('php'); // /usr/bin/php
Usage
Constructing a Locator
There are several ways to create a locator. The preferred way is to use the
brianium/habitat constructor. Habitat
makes accessing the environment variables easy, even in cases where the $_ENV
superglobal isn't populated. You can use it like this:
$habitat = new \Habitat\Habitat(); $environment = $habitat->getEnvironment(); $locatorFactory = new \Nubs\Which\LocatorFactory\PlatformLocatorFactory(); $locator = $locatorFactory->create($environment);
Habitat environment is not necessary. Without passing an environment, PHP's
built-in getenv will be used:
$locatorFactory = new \Nubs\Which\LocatorFactory\PlatformLocatorFactory(); $locator = $locatorFactory->create();
There are also two platform-specific factories in case you don't want to rely
on the platform detection in the PlatformLocatorFactory. If you are on a
POSIXy system (e.g., Linux, OSX, BSD), you can use the PosixLocatorFactory
and if you are on a Windows system you can use the WindowsLocatorFactory.
$locatorFactory = new \Nubs\Which\LocatorFactory\PosixLocatorFactory(); $locator = $locatorFactory->create(); // or $locatorFactory = new \Nubs\Which\LocatorFactory\WindowsLocatorFactory(); $locator = $locatorFactory->create();
Finally, if you want full control over the paths that are searched, you can use specify exactly which paths to use:
$paths = ['/opt/special/bin', '/usr/local/bin', '/usr/bin', '/bin']; $pathBuilder = new \Nubs\Which\PathBuilder\PosixPathBuilder($paths); $locator = new \Nubs\Which\Locator($pathBuilder); // or $paths = ['C:\\Windows\\System32', 'C:\\Windows']; $pathExtensions = ['.exe', '.com']; $pathBuilder = new \Nubs\Which\PathBuilder\WindowsPathBuilder( $paths, $pathExtensions ); $locator = new \Nubs\Which\Locator($pathBuilder);
Locating commands
The locator can find commands based off of its configured paths and will return
null if the command could not be found:
$locatorFactory = new \Nubs\Which\LocatorFactory\PlatformLocatorFactory(); $locator = $locatorFactory->create(); echo $locator->locate('php'); // /usr/bin/php var_dump($locator->locate('asdf')); // NULL
It can also be given an absolute or a relative path, in which case the configured paths are ignored and pathing is done based off the current directory:
$locatorFactory = new \Nubs\Which\LocatorFactory\PlatformLocatorFactory(); $locator = $locatorFactory->create(); echo $locator->locate('/opt/php/bin/php'); // /opt/php/bin/php chdir('/opt/php'); echo $locator->locate('bin/php'); // /opt/php/bin/php
Finally, an additional locateAll method is included. If a command exists at
multiple places on the PATH, this will return all of them. It operates
under all the rules as the standard locate method.
$locatorFactory = new \Nubs\Which\LocatorFactory\PlatformLocatorFactory(); $locator = $locatorFactory->create(); var_dump($locator->locateAll('php')); // array(2) { // [0] => // string(12) "/usr/bin/php" // [1] => // string(16) "/opt/php/bin/php" // } var_dump($locator->locate('asdf')); // array(0) { // } var_dump($locator->locateAll('/opt/php/bin/php')); // array(1) { // [0] => // string(16) "/opt/php/bin/php" // } chdir('/opt/php'); var_dump($locator->locateAll('bin/php')); // array(1) { // [0] => // string(16) "/opt/php/bin/php" // }
CLI Interface
There is also a CLI interface for both POSIX systems and Windows that imitates
the standard which command. It is available as
nubs/which-cli.
Why?
I created which in order to fill a hole I came across: Detecting whether a user has a certain command installed. Primarily this was for sensible, a library that picks the user's preferred editor/browser/pager and falls back to a sensible default if no preference is specified. which provides the ability for sensible to decide whether the different command choices exist. Read more about it on my blog.
Similar Projects
I am also aware of a node.js library with a similar role: node-which.
License
which is licensed under the MIT license. See LICENSE for the full license text.
nubs/which 适用场景与选型建议
nubs/which 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.41k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 06 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「path」 「linux」 「windows」 「script」 「executable」 「which」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nubs/which 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nubs/which 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nubs/which 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Small library to access Microsoft Windows Azure Blob Storage with a Service or a StreamWrapper.
Brotli binary provided by google/brotli and compiled for amd64
Advanced local development experience for Linux.
JSONPath implementation for querying and updating JSON objects with support for json flattening into a table
The InstantConfigurationCopy module provides easy way to copy configuration field information for admin in back office Magento 2.
simple agnostic package for IO releted (files, dir, etc..)
统计信息
- 总下载量: 8.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 21
- 依赖项目数: 4
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2014-06-17