lithium\core\Environment::is()

public static method

A simple boolean detector that can be used to test which environment the application is running under. For example Environment::is('development') will return true if 'development' is, in fact, the current environment.

This method also handles how the environment is detected at the beginning of the request.

Custom Detection

While the default detection rules are very simple (if the 'SERVER_ADDR' variable is set to 127.0.0.1, the environment is assumed to be 'development', or if the string 'test' is found anywhere in the host name, it is assumed to be 'test', and in all other cases it is assumed to be 'production'), you can define your own detection rule set easily using a closure that accepts an instance of the Request object, and returns the name of the correct environment, as in the following example:


In the above example, the user-specified closure takes in a `Request` object, and using the
server data which it encapsulates, returns the correct environment name as a string.

#### Host Mapping

The most common use case is to set the environment depending on host name. For convenience,
the `is()` method also accepts an array that matches host names to environment names, where
each key is an environment, and each value is either an array of valid host names, or a
regular expression used to match a valid host name.

``` embed:lithium\tests\cases\core\EnvironmentTest::testDetectionWithArrayMap(1-5) ```

In this example, a regular expression is being used to match local domains
(i.e. `localhost`), as well as the built-in `.console` domain, for console requests. Note
that in the console, the environment can always be overridden by specifying the `--env`
option.

Then, one or more host names are matched up to `'test'` and `'staging'`, respectively. Note
that no rule is present for production: this is because `'production'` is the default value
if no other environment matches.

Parameters

  • mixed $detect

    Either the name of an environment to check against the current, i.e. 'development' or 'production', or a closure which Environment will use to determine the current environment name, or an array mapping environment names to host names.

Returns

boolean

If $detect is a string, returns true if the current environment matches the value of $detect, or false if no match. If used to set a custom detector, returns null.

Source

	public static function is($detect) {
		if (is_callable($detect)) {
			static::$_detector = $detect;
		}
		if (!is_array($detect)) {
			return (static::$_current == $detect);
		}
		static::$_detector = function($request) use ($detect) {
			if ($request->env || $request->command == 'test') {
				return $request->env ?: 'test';
			}
			$host = method_exists($request, 'get') ? $request->get('http:host') : '.console';

			foreach ($detect as $environment => $hosts) {
				if ($host && is_string($hosts) && preg_match($hosts, $host)) {
					return $environment;
				}
				if (is_array($hosts) && in_array($host, $hosts)) {
					return $environment;
				}
			}
			return "production";
		};
	}