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:
embed:lithium\tests\cases\core\EnvironmentTest::testCustomDetector(1-9)
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.
public static is ( mixed $detect ) : boolean | ||
$detect | mixed | 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. |
Результат | 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`. |