2017 © Pedro Peláez
 

library detect-environment

Detect an application's environment

image

jstewmc/detect-environment

Detect an application's environment

  • Monday, October 3, 2016
  • by Jstewmc
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 13 % Grown

The README.md

detect-environment

Detect an application's environment., (*1)

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:, (*2)

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., (*3)

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., (*4)

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

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)., (*6)

No matter where you define the environment variable, it MUST be accessible to PHP's getenv function., (*7)

That's it!, (*8)

License

MIT, (*9)

Author

Jack Clayton, (*10)

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

The Versions

03/10 2016

dev-master

9999999-dev

Detect an application's environment

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Jack Clayton

php environment detect jstewmc

03/10 2016

2.0.0

2.0.0.0

Detect an application's environment

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Jack Clayton

php environment detect jstewmc

14/08 2016

v1.0.0

1.0.0.0

Detect an application's environment

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Jack Clayton

php environment detect jstewmc

13/08 2016

v0.2.0

0.2.0.0

Detect an application's environment

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Jack Clayton

php environment detect jstewmc