jstewmc/detect-environment

Detect an application's environment

2.0.0 2016-10-02 23:18 UTC

This package is auto-updated.

Last update: 2024-11-29 05:26:30 UTC


README

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

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

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 function.

That's it!

License

MIT

Author

Jack Clayton

Version

2.0.0, 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

  • Major release
  • Update composer.json

0.2.0, August 13, 2016

  • Refactor to support user-defined environments

0.1.0, June 25, 2016

  • Initial release