PHPackages                             nubs/which - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nubs/which

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nubs/which
==========

A library for locating commands in a PATH.

v1.0.1(11y ago)58.2k13MITPHPPHP ~5.4 || ~7.0

Since Jun 17Pushed 9y ago1 watchersCompare

[ Source](https://github.com/nubs/which)[ Packagist](https://packagist.org/packages/nubs/which)[ RSS](/packages/nubs-which/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (5)Used By (3)

Which
=====

[](#which)

A PHP library for locating commands in a PATH.

[![Build Status](https://camo.githubusercontent.com/7d3a50b4b726e0cd38a59d115a8b695f3c660b2d88f50c936b77a0b59ac73916/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f6e7562732f77686963682e7376673f7374796c653d666c6174)](https://travis-ci.org/nubs/which)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/967d5d5dbc339202b6ecbf59e5f2ba2dc4968e4f7f0e8d25b7341b6d9628ba5e/687474703a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6e7562732f77686963682e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/nubs/which/)[![Code Coverage](https://camo.githubusercontent.com/9944fda4e3b6cb3b017ba521e2909c4c928522bb0df20c477803ccfca94dbbcc/687474703a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6e7562732f77686963682e7376673f7374796c653d666c6174)](https://coveralls.io/r/nubs/which)

[![Latest Stable Version](https://camo.githubusercontent.com/8d9c4942734beab6e5cc104ae5d60f70cfbb86004279fbee18113e1963557071/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e7562732f77686963682e7376673f7374796c653d666c6174)](https://packagist.org/packages/nubs/which)[![Total Downloads](https://camo.githubusercontent.com/c44dcebeca2eabba10ddb6282e7853740b0e267d510e7b3b13adb242c1658aa9/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e7562732f77686963682e7376673f7374796c653d666c6174)](https://packagist.org/packages/nubs/which)[![License](https://camo.githubusercontent.com/2f11fcf8840980c6ba45d06a19741b4a46ee9c5ebb2c1e9d091ac6dd606bb3b3/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6e7562732f77686963682e7376673f7374796c653d666c6174)](https://packagist.org/packages/nubs/which)

[![Dependency Status](https://camo.githubusercontent.com/5ecd4a169415a0b2a7926ba15138f7cff4f3238478d54871fd00081d738e41dc/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3533613031663762383361646437343961333030303031652f62616467652e7376673f7374796c653d666c6174)](https://www.versioneye.com/user/projects/53a01f7b83add749a300001e)

Requirements
------------

[](#requirements)

This library requires PHP 5.6, or newer.

Installation
------------

[](#installation)

This package uses [composer](https://getcomposer.org) 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
-------

[](#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
-----

[](#usage)

### Constructing a Locator

[](#constructing-a-locator)

There are several ways to create a locator. The preferred way is to use the [brianium/habitat](https://github.com/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

[](#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

[](#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`](https://github.com/nubs/which-cli).

### Why?

[](#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](https://github.com/nubs/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](http://www.overthemonkey.com/blog/which-which-is-which).

### Similar Projects

[](#similar-projects)

I am also aware of a node.js library with a similar role: [node-which](https://github.com/isaacs/node-which).

License
-------

[](#license)

which is licensed under the MIT license. See [LICENSE](LICENSE) for the full license text.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 98% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~103 days

Total

4

Last Release

4041d ago

Major Versions

v0.1.1 → v1.0.02014-07-10

PHP version history (3 changes)v0.1.0PHP &gt;=5.3.2

v1.0.0PHP ~5.4

v1.0.1PHP ~5.4 || ~7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/43e7568db49cb140fe4deef1e585de0874e2a962a1140662003a5c070f536879?d=identicon)[nubs](/maintainers/nubs)

---

Top Contributors

[![nubs](https://avatars.githubusercontent.com/u/57673?v=4)](https://github.com/nubs "nubs (48 commits)")[![DanAtDE](https://avatars.githubusercontent.com/u/1520336?v=4)](https://github.com/DanAtDE "DanAtDE (1 commits)")

---

Tags

executablepathlinuxwindowsscriptwhich

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/nubs-which/health.svg)

```
[![Health](https://phpackages.com/badges/nubs-which/health.svg)](https://phpackages.com/packages/nubs-which)
```

###  Alternatives

[bamarni/composer-bin-plugin

No conflicts for your bin dependencies

52722.0M859](/packages/bamarni-composer-bin-plugin)[league/uri-components

URI components manipulation library

31932.3M67](/packages/league-uri-components)[matthiasmullie/path-converter

Relative path converter

10229.6M7](/packages/matthiasmullie-path-converter)[jolicode/php-os-helper

Helpers to detect the OS of the machine where PHP is running.

212.8M4](/packages/jolicode-php-os-helper)[sebastianfeldmann/camino

Path management the OO way

176.4M1](/packages/sebastianfeldmann-camino)[lbaey/chromedriver

Utility for installing Chromedriver, whatever platform you are on, for use with Behat

1459.5k1](/packages/lbaey-chromedriver)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
