Merge pull request #5469 from cakephp/2.7-configure

Backport Configure::consume() to 2.x
This commit is contained in:
Mark S. 2014-12-25 22:23:44 +01:00
commit c7940f57ac
2 changed files with 44 additions and 0 deletions

View file

@ -190,6 +190,30 @@ class Configure {
return Hash::get(self::$_values, $var); return Hash::get(self::$_values, $var);
} }
/**
* Used to read and delete a variable from Configure.
*
* This is primarily used during bootstrapping to move configuration data
* out of configure into the various other classes in CakePHP.
*
* @param string $var The key to read and remove.
* @return array|null
*/
public static function consume($var) {
$simple = strpos($var, '.') === false;
if ($simple && !isset(self::$_values[$var])) {
return null;
}
if ($simple) {
$value = self::$_values[$var];
unset(self::$_values[$var]);
return $value;
}
$value = Hash::get(self::$_values, $var);
self::$_values = Hash::remove(self::$_values, $var);
return $value;
}
/** /**
* Returns true if given variable is set in Configure. * Returns true if given variable is set in Configure.
* *

View file

@ -148,6 +148,26 @@ class ConfigureTest extends CakeTestCase {
$this->assertEquals('4', $result); $this->assertEquals('4', $result);
} }
/**
* Test the consume method.
*
* @return void
*/
public function testConsume() {
$this->assertNull(Configure::consume('DoesNotExist'), 'Should be null on empty value');
Configure::write('Test', array('key' => 'value', 'key2' => 'value2'));
$result = Configure::consume('Test.key');
$this->assertEquals('value', $result);
$result = Configure::read('Test.key2');
$this->assertEquals('value2', $result, 'Other values should remain.');
$result = Configure::consume('Test');
$expected = array('key2' => 'value2');
$this->assertEquals($expected, $result);
}
/** /**
* test setting display_errors with debug. * test setting display_errors with debug.
* *