diff --git a/lib/Cake/Controller/Component/CookieComponent.php b/lib/Cake/Controller/Component/CookieComponent.php index a950e2337..31d2778f6 100644 --- a/lib/Cake/Controller/Component/CookieComponent.php +++ b/lib/Cake/Controller/Component/CookieComponent.php @@ -190,8 +190,9 @@ class CookieComponent extends Component { public function startup(Controller $controller) { $this->_expire($this->time); + $this->_values[$this->name] = array(); if (isset($_COOKIE[$this->name])) { - $this->_values = $this->_decrypt($_COOKIE[$this->name]); + $this->_values[$this->name] = $this->_decrypt($_COOKIE[$this->name]); } } @@ -215,6 +216,10 @@ class CookieComponent extends Component { * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::write */ public function write($key, $value = null, $encrypt = true, $expires = null) { + if (empty($this->_values[$this->name])) { + $this->read(); + } + if (is_null($encrypt)) { $encrypt = true; } @@ -227,14 +232,14 @@ class CookieComponent extends Component { foreach ($key as $name => $value) { if (strpos($name, '.') === false) { - $this->_values[$name] = $value; + $this->_values[$this->name][$name] = $value; $this->_write("[$name]", $value); } else { $names = explode('.', $name, 2); - if (!isset($this->_values[$names[0]])) { - $this->_values[$names[0]] = array(); + if (!isset($this->_values[$this->name][$names[0]])) { + $this->_values[$this->name][$names[0]] = array(); } - $this->_values[$names[0]] = Set::insert($this->_values[$names[0]], $names[1], $value); + $this->_values[$this->name][$names[0]] = Set::insert($this->_values[$this->name][$names[0]], $names[1], $value); $this->_write('[' . implode('][', $names) . ']', $value); } } @@ -252,26 +257,28 @@ class CookieComponent extends Component { * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::read */ public function read($key = null) { - if (empty($this->_values) && isset($_COOKIE[$this->name])) { - $this->_values = $this->_decrypt($_COOKIE[$this->name]); + if (empty($this->_values[$this->name]) && isset($_COOKIE[$this->name])) { + $this->_values[$this->name] = $this->_decrypt($_COOKIE[$this->name]); + } + if (empty($this->_values[$this->name])) { + $this->_values[$this->name] = array(); } - if (is_null($key)) { - return $this->_values; + return $this->_values[$this->name]; } if (strpos($key, '.') !== false) { $names = explode('.', $key, 2); $key = $names[0]; } - if (!isset($this->_values[$key])) { + if (!isset($this->_values[$this->name][$key])) { return null; } if (!empty($names[1])) { - return Set::extract($this->_values[$key], $names[1]); + return Set::extract($this->_values[$this->name][$key], $names[1]); } - return $this->_values[$key]; + return $this->_values[$this->name][$key]; } /** @@ -288,22 +295,22 @@ class CookieComponent extends Component { * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::delete */ public function delete($key) { - if (empty($this->_values)) { + if (empty($this->_values[$this->name])) { $this->read(); } if (strpos($key, '.') === false) { - if (isset($this->_values[$key]) && is_array($this->_values[$key])) { - foreach ($this->_values[$key] as $idx => $val) { + if (isset($this->_values[$this->name][$key]) && is_array($this->_values[$this->name][$key])) { + foreach ($this->_values[$this->name][$key] as $idx => $val) { $this->_delete("[$key][$idx]"); } } $this->_delete("[$key]"); - unset($this->_values[$key]); + unset($this->_values[$this->name][$key]); return; } $names = explode('.', $key, 2); - if (isset($this->_values[$names[0]])) { - $this->_values[$names[0]] = Set::remove($this->_values[$names[0]], $names[1]); + if (isset($this->_values[$this->name][$names[0]])) { + $this->_values[$this->name][$names[0]] = Set::remove($this->_values[$this->name][$names[0]], $names[1]); } $this->_delete('[' . implode('][', $names) . ']'); } @@ -319,17 +326,17 @@ class CookieComponent extends Component { */ public function destroy() { if (isset($_COOKIE[$this->name])) { - $this->_values = $this->_decrypt($_COOKIE[$this->name]); + $this->_values[$this->name] = $this->_decrypt($_COOKIE[$this->name]); } - foreach ($this->_values as $name => $value) { + foreach ($this->_values[$this->name] as $name => $value) { if (is_array($value)) { foreach ($value as $key => $val) { - unset($this->_values[$name][$key]); + unset($this->_values[$this->name][$name][$key]); $this->_delete("[$name][$key]"); } } - unset($this->_values[$name]); + unset($this->_values[$this->name][$name]); $this->_delete("[$name]"); } } @@ -503,5 +510,5 @@ class CookieComponent extends Component { } return $array; } - } + diff --git a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php index e45a84562..eafe154d1 100644 --- a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php @@ -160,7 +160,6 @@ class CookieComponentTest extends CakeTestCase { */ public function testReadPlainCookieData() { $this->_setCookieData(); - $data = $this->Cookie->read('Plain_array'); $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'); $this->assertEquals($data, $expected); @@ -170,6 +169,26 @@ class CookieComponentTest extends CakeTestCase { $this->assertEquals($data, $expected); } +/** + * test read() after switching the cookie name. + * + * @return void + */ + public function testReadWithNameSwitch() { + $_COOKIE = array( + 'CakeTestCookie' => array( + 'key' => 'value' + ), + 'OtherTestCookie' => array( + 'key' => 'other value' + ) + ); + $this->assertEquals('value', $this->Cookie->read('key')); + + $this->Cookie->name = 'OtherTestCookie'; + $this->assertEquals('other value', $this->Cookie->read('key')); + } + /** * test a simple write() *