lithium\core\Environment::set()

public static method

Creates, modifies or switches to an existing environment configuration. To create a new configuration, or to update an existing configuration, pass an environment name and an array that defines its configuration:


You can then add to an existing configuration by calling the set() method again with the same environment name:


The settings for the environment will then be the aggregate of all `set()` calls:
``` embed:lithium\tests\cases\core\EnvironmentTest::testModifyEnvironmentConfig(7-7) ```

By passing an array to `$env`, you can assign the same configuration to multiple
environments:
``` embed:lithium\tests\cases\core\EnvironmentTest::testSetMultipleEnvironments(5-7) ```

The `set()` method can also be called to manually set which environment to operate in:
``` embed:lithium\tests\cases\core\EnvironmentTest::testSetAndGetCurrentEnvironment(5-5) ```

Finally, `set()` can accept a `Request` object, to automatically detect the correct
environment.

``` embed:lithium\tests\cases\core\EnvironmentTest::testEnvironmentDetection(9-10) ```

For more information on defining custom rules to automatically detect your application's
environment, see the documentation for `Environment::is()`.

Parameters

  • mixed $env

    The name(s) of the environment(s) you wish to create, update or switch to (string/array), or a Request object or $_SERVER / $_ENV array used to detect (and switch to) the application's current environment.

  • array $config

    If creating or updating a configuration, accepts an array of settings. If the environment name specified in $env already exists, the values in $config will be recursively merged with any pre-existing settings.

Returns

array

If creating or updating a configuration, returns an array of the environment's settings. If updating an existing configuration, this will be the newly-applied configuration merged with the pre-existing values. If setting the environment itself (i.e. $config is unspecified), returns null.

Source

	public static function set($env, $config = null) {
		if ($config === null) {
			if (is_object($env) || is_array($env)) {
				static::$_current = static::_detector()->__invoke($env);
			} elseif (isset(static::$_configurations[$env])) {
				static::$_current = $env;
			}
			return;
		}
		if (is_array($env)) {
			foreach ($env as $name) {
				static::set($name, $config);
			}
			return;
		}
		$env = ($env === true) ? static::$_current : $env;

		if (isset(static::$_configurations[$env])) {
			$config = Set::merge(static::$_configurations[$env], $config);
		}
		return static::$_configurations[$env] = $config;
	}