Download the PHP package nubs/which without Composer
On this page you can find all versions of the php package nubs/which. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package which
Which
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:
Example
Here is a quick example to demonstrate how this library is generally meant to be used:
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 environment is not necessary. Without passing an environment, PHP's
built-in getenv
will be used:
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
.
Finally, if you want full control over the paths that are searched, you can use specify exactly which paths to use:
Locating commands
The locator can find commands based off of its configured paths and will return
null
if the command could not be found:
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:
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.
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.