PHPackages                             jstewmc/detect-environment - 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. jstewmc/detect-environment

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

jstewmc/detect-environment
==========================

Detect an application's environment

2.0.0(9y ago)0701MITPHPPHP ^7.0

Since Aug 13Pushed 9y ago1 watchersCompare

[ Source](https://github.com/jstewmc/detect-environment)[ Packagist](https://packagist.org/packages/jstewmc/detect-environment)[ RSS](/packages/jstewmc-detect-environment/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (4)Used By (1)

detect-environment
==================

[](#detect-environment)

Detect an application's environment.

```
namespace Jstewmc\DetectEnvironment;

// define our possible environments
$values = [
    'development' => 'foo',
    'testing'     => 'bar',
    'staging'     => 'baz',
    'production'  => 'qux'
];

// put the environment variable
putenv('APP_ENV=foo');

// instantiate the service
$service = new DetectEnvironment('APP_ENV', $values);

// detect the environment
$service->isDevelopment();  // returns true
$service->isTesting();      // returns false
$service->isStaging();      // returns false
$service->isProduction();   // returns false

// get the environment's name
$service();  // returns "development"
```

Usage
-----

[](#usage)

To instantiate the service, you MUST pass the *environment variable name* and the *environment variable values*, indexed by *application environment* name:

```
namespace Jstewmc\DetectEnvironment;

putenv('APP_ENV=foo');

$service = new DetectEnvironment(
    'APP_ENV',
    [
        'development' => 'foo',
        'testing'     => 'bar',
        'staging'     => 'baz',
        'production'  => 'qux'
    ]
);
```

In the example above, the service would detect the environment as *development*, *testing*, *staging*, and *production* when the `APP_ENV` environment was `'foo'`, `'bar'`, `'baz'`, and `'qux'`, respectively.

Keep in mind, the *environment variable name*, the *environment variable values*, and the *application environment names* MAY be any string. The *environment variable values* and the *application environment names* are case-insensitive.

You can check the application's environment using `isX()` methods, where `X` is any valid application environment name:

```
namespace Jstewmc\DetectEnvironment;

putenv('APP_ENV=foo');

$service = new DetectEnvironment(
    'APP_ENV',
    [
        'development' => 'foo',
        'testing'     => 'bar',
        'staging'     => 'baz',
        'production'  => 'qux'
    ]
);

$service->isDevelopment();  // returns true
$service->isTesting();      // returns false
$service->isStaging();      // returns false
$service->isProduction();   // returns false
$service->foo();            // throws exception (MUST start with "is")
$service->isFoo();          // throws exception (MUST be valid environment name)
```

Environment variable
--------------------

[](#environment-variable)

In the examples above, the environment variable was set using the `putenv()` function. However, in the real world, you should define the environment variable in your server configuration (e.g., `.htaccess`, `httpd.conf`, etc).

No matter where you define the environment variable, it MUST be accessible to PHP's [getenv](http://php.net/manual/en/function.getenv.php) function.

That's it!

License
-------

[](#license)

[MIT](https://github.com/jstewmc/detect-environment/blob/master/LICENSE)

Author
------

[](#author)

[Jack Clayton](mailto:jack@jahuty.com)

Version
-------

[](#version)

### 2.0.0, October 2, 2016

[](#200-october-2-2016)

- Add `__invoke()` method to return environment name.
- Rename class to `DetectEnvironment`. The longer name seems more intentional.

### 1.0.0, August 13, 2016

[](#100-august-13-2016)

- Major release
- Update `composer.json`

### 0.2.0, August 13, 2016

[](#020-august-13-2016)

- Refactor to support user-defined environments

### 0.1.0, June 25, 2016

[](#010-june-25-2016)

- Initial release

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~25 days

Total

3

Last Release

3561d ago

Major Versions

v0.2.0 → v1.0.02016-08-13

v1.0.0 → 2.0.02016-10-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/50fecae0a7fd2119681bc133e496e7166b01a59f850a3c909e100bd427c6b28b?d=identicon)[Jstewmc](/maintainers/Jstewmc)

---

Top Contributors

[![jstewmc](https://avatars.githubusercontent.com/u/1192893?v=4)](https://github.com/jstewmc "jstewmc (11 commits)")

---

Tags

phpenvironmentdetectjstewmc

### Embed Badge

![Health badge](/badges/jstewmc-detect-environment/health.svg)

```
[![Health](https://phpackages.com/badges/jstewmc-detect-environment/health.svg)](https://phpackages.com/packages/jstewmc-detect-environment)
```

PHPackages © 2026

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