Skip to content

Commit

Permalink
Add configKey() method.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed May 29, 2014
1 parent d25a0e8 commit c7371a0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 19 deletions.
47 changes: 30 additions & 17 deletions 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)
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -141,6 +133,27 @@ public function __construct(ComponentRegistry $collection, array $config = array
}
}

/**
* 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
*
Expand Down
49 changes: 47 additions & 2 deletions 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)
*
Expand Down Expand Up @@ -71,6 +69,53 @@ public function tearDown() {
$this->Cookie->destroy();
}

/**
* 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.
*
Expand Down

0 comments on commit c7371a0

Please sign in to comment.