From 515302528da096311d871f0c8db430fecdc61c95 Mon Sep 17 00:00:00 2001 From: euromark Date: Tue, 23 Dec 2014 03:26:03 +0100 Subject: [PATCH] Backport Configure::consume() to 2.x --- lib/Cake/Core/Configure.php | 24 +++++++++++++++++++++++ lib/Cake/Test/Case/Core/ConfigureTest.php | 20 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/Cake/Core/Configure.php b/lib/Cake/Core/Configure.php index 18c85589d..c778fc0be 100644 --- a/lib/Cake/Core/Configure.php +++ b/lib/Cake/Core/Configure.php @@ -190,6 +190,30 @@ class Configure { 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. * diff --git a/lib/Cake/Test/Case/Core/ConfigureTest.php b/lib/Cake/Test/Case/Core/ConfigureTest.php index f91218418..717b52bc5 100644 --- a/lib/Cake/Test/Case/Core/ConfigureTest.php +++ b/lib/Cake/Test/Case/Core/ConfigureTest.php @@ -148,6 +148,26 @@ class ConfigureTest extends CakeTestCase { $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', ['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 = ['key2' => 'value2']; + $this->assertEquals($expected, $result); + } + /** * test setting display_errors with debug. *