Permalink
Browse files

Allowing `Environment::set()` to assign to multiple environments.

  • Loading branch information...
1 parent becc3b6 commit 73a07d87bb22e024fde15cdd7d291b8661c95bd5 @nateabele nateabele committed Sep 21, 2012
Showing with 38 additions and 14 deletions.
  1. +21 −14 core/Environment.php
  2. +17 −0 tests/cases/core/EnvironmentTest.php
View
@@ -260,22 +260,26 @@ protected static function _processDotPath($path, &$arrayPointer) {
* 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, or the `$_SERVER` or `$_ENV` superglobals, to
- * automatically detect the correct environment.
+ * 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()`.
*
- * @see lithium\http\Request
+ * @see lithium\action\Request
* @see lithium\core\Environment::is()
- * @param mixed $env The name of the environment you wish to create, update or switch to
- * (string), or a `Request` object or `$_SERVER` / `$_ENV` array used to detect
- * (and switch to) the application's current environment.
+ * @param 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.
* @param 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.
@@ -285,14 +289,17 @@ protected static function _processDotPath($path, &$arrayPointer) {
* itself (i.e. `$config` is unspecified), returns `null`.
*/
public static function set($env, $config = null) {
- if (is_null($config)) {
- switch (true) {
- case is_object($env) || is_array($env):
- static::$_current = static::_detector()->__invoke($env);
- break;
- case isset(static::$_configurations[$env]):
- static::$_current = $env;
- break;
+ 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;
}
@@ -42,6 +42,23 @@ public function testSetAndGetCurrentEnvironment() {
}
/**
+ * Tests that a set of configuration keys can be assigned to multiple environments.
+ */
+ public function testSetMultipleEnvironments() {
+ foreach (array('foo', 'bar', 'baz') as $key) {
+ $this->assertNull(Environment::get($key));
+ }
+
+ Environment::set(array('foo', 'bar', 'baz'), array(
+ 'key' => array('subkey' => 'value')
+ ));
+
+ foreach (array('foo', 'bar', 'baz') as $key) {
+ $this->assertEqual(array('key' => array('subkey' => 'value')), Environment::get($key));
+ }
+ }
+
+ /**
* Tests creating a custom environment, and verifies that settings are properly retrieved.
*/
public function testCreateNonStandardEnvironment() {

0 comments on commit 73a07d8

Please sign in to comment.