PHPackages                             drevops/environment-detector - 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. drevops/environment-detector

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

drevops/environment-detector
============================

Universal environment detector

0.5.0(6mo ago)22[6 issues](https://github.com/drevops/environment-detector/issues)GPL-2.0-or-laterPHPPHP &gt;=8.2CI passing

Since Apr 18Pushed 1mo agoCompare

[ Source](https://github.com/drevops/environment-detector)[ Packagist](https://packagist.org/packages/drevops/environment-detector)[ Docs](https://github.com/drevops/environment-detector)[ GitHub Sponsors](https://github.com/alexskrypnyk)[ Patreon](https://www.patreon.com/alexskrypnyk)[ RSS](/packages/drevops-environment-detector/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (11)Versions (9)Used By (0)

  ![Environment Detector](logo.png)

Zero-config environment type detection
======================================

[](#zero-config-environment-type-detection)

[![GitHub Issues](https://camo.githubusercontent.com/52f4235c508f3332d0f790ea0f42fe87e80155f596996444864c985b3027561a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f647265766f70732f656e7669726f6e6d656e742d6465746563746f722e737667)](https://github.com/drevops/environment-detector/issues)[![GitHub Pull Requests](https://camo.githubusercontent.com/b65fc043e5c76e49fca75cc73638ca06b7c9dc9f836d3d8561a09ddbb5c3e7f9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d70722f647265766f70732f656e7669726f6e6d656e742d6465746563746f722e737667)](https://github.com/drevops/environment-detector/pulls)[![Test PHP](https://github.com/drevops/environment-detector/actions/workflows/test-php.yml/badge.svg)](https://github.com/drevops/environment-detector/actions/workflows/test-php.yml)[![codecov](https://camo.githubusercontent.com/94945ac77072398d3e9318eeaccd0fed6a5237f9fdffe903664615e432583cc6/68747470733a2f2f636f6465636f762e696f2f67682f647265766f70732f656e7669726f6e6d656e742d6465746563746f722f67726170682f62616467652e7376673f746f6b656e3d51325338304746534636)](https://codecov.io/gh/drevops/environment-detector)[![GitHub release (latest by date)](https://camo.githubusercontent.com/91ac5124c8eaefebfaf2cb0370087ebed70b015391ca31570468625f58e0da4e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f647265766f70732f656e7669726f6e6d656e742d6465746563746f72)](https://camo.githubusercontent.com/91ac5124c8eaefebfaf2cb0370087ebed70b015391ca31570468625f58e0da4e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f647265766f70732f656e7669726f6e6d656e742d6465746563746f72)[![LICENSE](https://camo.githubusercontent.com/5d1902edb54de54bc5f0b6b4fcacda9ef2b866d1632b35ed5b8e3f6cc43285ec/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f647265766f70732f656e7669726f6e6d656e742d6465746563746f72)](https://camo.githubusercontent.com/5d1902edb54de54bc5f0b6b4fcacda9ef2b866d1632b35ed5b8e3f6cc43285ec/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f647265766f70732f656e7669726f6e6d656e742d6465746563746f72)[![Renovate](https://camo.githubusercontent.com/35389190ce58a3690fe850342c1c3fd4f54e4c10ba8996741c8558ee24bf50dc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f72656e6f766174652d656e61626c65642d677265656e3f6c6f676f3d72656e6f76617465626f74)](https://camo.githubusercontent.com/35389190ce58a3690fe850342c1c3fd4f54e4c10ba8996741c8558ee24bf50dc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f72656e6f766174652d656e61626c65642d677265656e3f6c6f676f3d72656e6f76617465626f74)

---

Features
--------

[](#features)

- Detects environment type: `local`, `ci`, `dev`, `preview`, `stage`, `prod`, or user-defined
- Supports many popular providers out-of-the-box: [Acquia](src/Providers/Acquia.php), [CircleCI](src/Providers/CircleCi.php), [DDEV](src/Providers/Ddev.php), [Docker](src/Providers/Docker.php), [GitHub Actions](src/Providers/GitHubActions.php), [GitLab CI](src/Providers/GitLabCi.php), [Lagoon](src/Providers/Lagoon.php), [Lando](src/Providers/Lando.php), [Pantheon](src/Providers/Pantheon.php), [Platform.sh](src/Providers/PlatformSh.php), [Skpr](src/Providers/Skpr.php), [Tugboat](src/Providers/Tugboat.php)
- Detects custom contexts: [Drupal](src/Contexts/Drupal.php) (more to come)
- Simple API for checking current environment
- Extendable via custom providers and contexts
- Override and fallback support for precise control
- Optimised for performance with static caching

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

[](#installation)

```
composer require drevops/environment-detector
```

Quick Start
-----------

[](#quick-start)

```
use DrevOps\EnvironmentDetector\Environment;

Environment::init();
if (getenv('ENVIRONMENT_TYPE') === Environment::LOCAL) {
  // Apply local settings.
}
```

Alternatively, use the convenience methods:

```
use DrevOps\EnvironmentDetector\Environment;

// No need to init() - a first call to is*() will auto-initialize.
if (Environment::isLocal()) {
  // Apply local settings.
}

if (Environment::isProd()) {
  // Apply production settings.
}
```

How It Works
------------

[](#how-it-works)

1. **Provider detection:** Each provider checks for environment-specific variables or files to identify itself.
2. **Type mapping:** Once identified, the provider maps its internal state to a type like `dev`, `prod`, or a custom type.
3. **Context detection**: Optionally applies provider- or framework-specific changes (e.g., modify Drupal `$settings` global variable to add `$settings['environment']` value).

The resolved type is stored in the `ENVIRONMENT_TYPE` env var. If already set, this value takes precedence over the provider detection. The `contextualize`still applies context changes even if the type is pre-set via environment variable.

Advanced Usage
--------------

[](#advanced-usage)

### Advanced initialization with customization

[](#advanced-initialization-with-customization)

```
Environment::init(
  contextualize: TRUE,                            // Whether to apply the context automatically
  fallback: Environment::DEVELOPMENT,             // The fallback environment type
  override: function($provider, $type) {          // The override callback to change the environment type
    if ($type === Environment::DEVELOPMENT && $provider->id() === 'tugboat') {
      return 'qa';
    }
    return $type;
  },
  providers: [new MyCustomProvider()],            // Additional provider instances
  contexts: [new MyCustomContext()],              // Additional context instances
);
```

### Fallback Type

[](#fallback-type)

If an environment type is not detected, a fallback `Environment::DEVELOPMENT` will be returned by default. This is to ensure that, in case of misconfiguration, the application does not apply local settings in production or production settings in local - 'development' type is the safest default.

You can set a different fallback type during initialization:

```
Environment::init(fallback: Environment::PRODUCTION);
```

Providers
---------

[](#providers)

Only one provider can be active. If multiple match, or none match, an exception is thrown. Register custom providers using `init(providers:[MyCustomProvider::class])`.

Supported built-ins:

- [Acquia](src/Providers/Acquia.php)
- [CircleCI](src/Providers/CircleCi.php)
- [DDEV](src/Providers/Ddev.php)
- [Docker](src/Providers/Docker.php)
- [GitHub Actions](src/Providers/GitHubActions.php)
- [GitLab CI](src/Providers/GitLabCi.php)
- [Lagoon](src/Providers/Lagoon.php)
- [Lando](src/Providers/Lando.php)
- [Pantheon](src/Providers/Pantheon.php)
- [Platform.sh](src/Providers/PlatformSh.php)
- [Skpr](src/Providers/Skpr.php)
- [Tugboat](src/Providers/Tugboat.php)

### Accessing Provider Data

[](#accessing-provider-data)

```
// Initialize first to detect the active provider
Environment::init();

$provider = Environment::getActiveProvider();
if ($provider && $provider->id() === 'acquia') {
  // Acquia-specific logic
  $data = $provider->data();
  if (isset($data['AH_SITE_GROUP'])) {
    // Use Acquia-specific environment data
  }
}
```

### Adding a Custom Provider

[](#adding-a-custom-provider)

```
use DrevOps\EnvironmentDetector\Providers\ProviderInterface;
use DrevOps\EnvironmentDetector\Environment;

class CustomHosting implements ProviderInterface {
  public function active(): bool {
    return isset($_SERVER['CUSTOM_ENV']);
  }

  public function data(): array {
    return ['CUSTOM_ENV' => $_SERVER['CUSTOM_ENV'] ?? null];
  }

  public function type(): ?string {
    return match ($_SERVER['CUSTOM_ENV_TYPE'] ?? null) {
      'dev' => Environment::DEVELOPMENT,
      'qa' => 'qa',
      'live' => Environment::PRODUCTION,
      default => null,
    };
  }

  public function id(): string {
    return 'customhosting';
  }

  public function label(): string {
    return 'Custom Hosting';
  }

  public function contextualize(\DrevOps\EnvironmentDetector\Contexts\ContextInterface $context): void {
    // Optional: Apply provider-specific context changes
  }
}

// Register the custom provider during initialization
Environment::init(providers: [new CustomHosting()]);
```

### Contexts

[](#contexts)

Contexts apply environment-specific changes to frameworks or applications. A context may provide generic changes that are applied to the application. A provider may also provide provider-specific context changes.

For example, a **Drupal** context applies changes to the global `$settings` array, while a **Lagoon** provider's `contextualize()` method adds Lagoon-specific changes to the `$settings` array.

The goal is to have enough context changes to cover the most common use cases, but also to allow adding custom contexts to cover specific use cases within the application.

#### Adding a custom context

[](#adding-a-custom-context)

```
use DrevOps\EnvironmentDetector\Contexts\ContextInterface;

class CustomContext implements ContextInterface {
  public function active(): bool {
    return class_exists('MyFramework');
  }

  public function contextualize(): void {
    // Apply generic context changes
    global $configuration;
    $configuration['custom_value'] = $_SERVER['custom_value'] ?? 'default';
  }

  public function id(): string {
    return 'myframework';
  }

  public function label(): string {
    return 'My Framework';
  }
}

// Register the custom context during initialization
Environment::init(contexts: [new CustomContext()]);
```

Maintenance
-----------

[](#maintenance)

```
composer install
composer lint
composer test
```

---

*This repository was created using the *[*Scaffold*](https://getscaffold.dev/)*project template.*

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance60

Regular maintenance activity

Popularity5

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 58.2% 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 ~53 days

Total

5

Last Release

182d ago

PHP version history (2 changes)0.1.0PHP &gt;=8.3

0.5.0PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/b57b0fd0b96f77f2efa1a1889af0ae607fa139bcc1256e809ee3ebbb30907364?d=identicon)[alexdrevops](/maintainers/alexdrevops)

---

Top Contributors

[![AlexSkrypnyk](https://avatars.githubusercontent.com/u/378794?v=4)](https://github.com/AlexSkrypnyk "AlexSkrypnyk (32 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (23 commits)")

---

Tags

acquiaciddevenvironmentlagoonpantheonphpplatformskprtugboat

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Type Coverage Yes

### Embed Badge

![Health badge](/badges/drevops-environment-detector/health.svg)

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

###  Alternatives

[sensiolabs/minify-bundle

Assets Minifier (CSS, JS) for Symfony &amp; Minify integration in Asset Mapper

5694.9k1](/packages/sensiolabs-minify-bundle)[sider/phinder

PHP code piece finder

5445.5k](/packages/sider-phinder)

PHPackages © 2026

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