Browse Source

Add configKey() method.

This method allows configuration to be set per top level cookie. This
will replace many of the parameters used in read()/write() and simplify
the overall API that CookieComponent uses.
mark_story 11 years ago
parent
commit
c7371a06c8

+ 30 - 17
src/Controller/Component/CookieComponent.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * Cookie Component
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -71,30 +69,24 @@ class CookieComponent extends Component {
 	];
 
 /**
- * Values stored in the cookie.
- *
- * Accessed in the controller using $this->Cookie->read('Name.key');
+ * Config specific to a given top level key name.
  *
- * @see CookieComponent::read();
- * @var string
- */
-	protected $_values = array();
-
-/**
- * Used to reset cookie time if $expire is passed to CookieComponent::write()
+ * The values in this array are merged with the general config
+ * to generate the configuration for a given top level cookie name.
  *
- * @var string
+ * @var array
  */
-	protected $_reset = null;
+	protected $_keyConfig = [];
 
 /**
- * Expire time of the cookie
+ * Values stored in the cookie.
  *
- * This is controlled by CookieComponent::time;
+ * Accessed in the controller using $this->Cookie->read('Name.key');
  *
+ * @see CookieComponent::read();
  * @var string
  */
-	protected $_expires = 0;
+	protected $_values = array();
 
 /**
  * A reference to the Controller's Cake\Network\Response object
@@ -142,6 +134,27 @@ class CookieComponent extends Component {
 	}
 
 /**
+ * Set the configuration for a specific top level key.
+ *
+ * @param string $keyname The top level keyname to configure.
+ * @param null|string|array $option Either the option name to set, or an array of options to set,
+ *   or null to read config options for a given key.
+ * @param string|null $value Either the value to set, or empty when $option is an array.
+ * @return void
+ */
+	public function configKey($keyname, $option = null, $value = null) {
+		if ($option === null) {
+			$default = $this->_config;
+			$local = isset($this->_keyConfig[$keyname]) ? $this->_keyConfig[$keyname] : [];
+			return $local + $default;
+		}
+		if (!is_array($option)) {
+			$option = [$option => $value];
+		}
+		$this->_keyConfig[$keyname] = $option;
+	}
+
+/**
  * Start CookieComponent for use in the controller
  *
  * @param Event $event An Event instance

+ 47 - 2
tests/TestCase/Controller/Component/CookieComponentTest.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * CookieComponentTest file
- *
  * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -72,6 +70,53 @@ class CookieComponentTest extends TestCase {
 	}
 
 /**
+ * Test setting config per key.
+ *
+ * @return void
+ */
+	public function testConfigKey() {
+		$this->Cookie->configKey('User', 'expires', '+3 days');
+		$result = $this->Cookie->configKey('User');
+		$expected = [
+			'expires' => '+3 days',
+			'path' => '/',
+			'domain' => '',
+			'key' => 'somerandomhaskeysomerandomhaskey',
+			'secure' => false,
+			'httpOnly' => false,
+			'encryption' => 'aes',
+			'name' => 'CakeTestCookie',
+			'time' => 10,
+		];
+		$this->assertEquals($expected, $result);
+	}
+
+/**
+ * Test setting config per key.
+ *
+ * @return void
+ */
+	public function testConfigKeyArray() {
+		$this->Cookie->configKey('User', [
+			'expires' => '+3 days',
+			'path' => '/shop'
+		]);
+		$result = $this->Cookie->configKey('User');
+		$expected = [
+			'expires' => '+3 days',
+			'path' => '/shop',
+			'domain' => '',
+			'key' => 'somerandomhaskeysomerandomhaskey',
+			'secure' => false,
+			'httpOnly' => false,
+			'encryption' => 'aes',
+			'name' => 'CakeTestCookie',
+			'time' => 10,
+		];
+		$this->assertEquals($expected, $result);
+	}
+
+/**
  * sets up some default cookie data.
  *
  * @return void