Permalink
Browse files

Refactoring Adaptable. Changing conditions parameter to options in Ca…

…che.

Fixing/cleaning up Session.
  • Loading branch information...
1 parent f6cf75e commit 4dc16e523c95b4397991c811f04239f2e7359acc @jperras jperras committed with nateabele Mar 30, 2010
View
4 libraries/lithium/core/Adaptable.php
@@ -162,7 +162,9 @@ public static function strategies($name) {
* @return mixed Result of application of strategies to data. If no strategies
* have been configured, this method will simply return the original data.
*/
- public static function applyStrategies($method, $name, $data, $options = array('mode' => null)){
+ public static function applyStrategies($method, $name, $data, array $options = array()){
+ $options += array('mode' => null);
+
if (!$strategies = static::strategies($name)) {
return $data;
}
View
41 libraries/lithium/storage/Cache.php
@@ -89,17 +89,19 @@ public static function key($key, $data = array()) {
* @param mixed $key Key to uniquely identify the cache entry
* @param mixed $data Data to be cached
* @param string $expiry A strtotime() compatible cache time
- * @param mixed $conditions Conditions for the write operation to proceed
+ * @param mixed $options Options for the method, filters and strategies.
* @return boolean True on successful cache write, false otherwise
* @filter This method may be filtered.
*/
- public static function write($name, $key, $data, $expiry = null, $conditions = null) {
+ public static function write($name, $key, $data, $expiry = null, array $options = array()) {
+ $options += array('conditions' => null, 'strategies' => true);
$settings = static::config();
if (!isset($settings[$name])) {
return false;
}
+ $conditions = $options['conditions'];
if (is_callable($conditions) && !$conditions()) {
return false;
}
@@ -110,7 +112,7 @@ public static function write($name, $key, $data, $expiry = null, $conditions = n
$data = null;
}
- if (!isset($options['strategies']) || $strategies) {
+ if ($options['strategies']) {
$options = array('key' => $key, 'class' => __CLASS__);
$data = static::applyStrategies(__FUNCTION__, $name, $data, $options);
}
@@ -126,30 +128,30 @@ public static function write($name, $key, $data, $expiry = null, $conditions = n
*
* @param string $name Configuration to be used for reading
* @param mixed $key Key to be retrieved
- * @param mixed $conditions Conditions for the read operation to proceed
+ * @param mixed $options Options for the method and strategies.
* @return mixed Read results on successful cache read, null otherwise
* @filter This method may be filtered.
*/
- public static function read($name, $key, $conditions = null) {
+ public static function read($name, $key, array $options = array()) {
+ $options += array('conditions' => null, 'strategies' => true);
$settings = static::config();
if (!isset($settings[$name])) {
return false;
}
+ $conditions = $options['conditions'];
if (is_callable($conditions) && !$conditions()) {
return false;
}
$key = static::key($key);
$method = static::adapter($name)->read($key);
$params = compact('key');
$filters = $settings[$name]['filters'];
-
$result = static::_filter(__FUNCTION__, $params, $method, $filters);
- $options = array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
- $strategies = (isset($options['strategies']) && $options['strategies'] === true);
- if (!isset($options['strategies']) || $strategies) {
+ if ($options['strategies']) {
+ $options = array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
return static::applyStrategies(__FUNCTION__, $name, $result, $options);
}
return $result;
@@ -160,27 +162,28 @@ public static function read($name, $key, $conditions = null) {
*
* @param string $name The cache configuration to delete from
* @param mixed $key Key to be deleted
- * @param mixed $conditions Conditions for the delete operation to proceed
+ * @param mixed $options Options for the method and strategies.
* @return boolean True on successful deletion, false otherwise
* @filter This method may be filtered.
*/
- public static function delete($name, $key, $conditions = null) {
+ public static function delete($name, $key, array $options = array()) {
+ $options += array('conditions' => null, 'strategies' => true);
$settings = static::config();
if (!isset($settings[$name])) {
return false;
}
+ $conditions = $options['conditions'];
if (is_callable($conditions) && !$conditions()) {
return false;
}
$key = static::key($key);
$method = static::adapter($name)->delete($key);
$filters = $settings[$name]['filters'];
- $options = array('key' => $key, 'class' => __CLASS__);
- if (!isset($options['strategies']) || $strategies) {
+ if ($options['strategies']) {
$options += array('key' => $key, 'class' => __CLASS__);
$key = static::applyStrategies(__FUNCTION__, $name, $key, $options);
}
@@ -195,16 +198,18 @@ public static function delete($name, $key, $conditions = null) {
* @param string $name
* @param string $key Key of numeric cache item to increment
* @param integer $offset Offset to increment - defaults to 1.
- * @param mixed $conditions
+ * @param mixed $options Options for this method.
* @return integer|boolean Item's new value on successful increment, false otherwise
* @filter This method may be filtered.
*/
- public static function increment($name, $key, $offset = 1, $conditions = null) {
+ public static function increment($name, $key, $offset = 1, array $options = array()) {
+ $options += array('conditions' => null);
$settings = static::config();
if (!isset($settings[$name])) {
return false;
}
+ $conditions = $options['conditions'];
if (is_callable($conditions) && !$conditions()) {
return false;
@@ -225,16 +230,18 @@ public static function increment($name, $key, $offset = 1, $conditions = null) {
* @param string $name
* @param string $key Key of numeric cache item to dercrement
* @param integer $offset Offset to decrement - defaults to 1.
- * @param mixed $conditions
+ * @param mixed $options Options for this method.
* @return integer|boolean Item's new value on successful decrement, false otherwise
* @filter This method may be filtered.
*/
- public static function decrement($name, $key, $offset = 1, $conditions = null) {
+ public static function decrement($name, $key, $offset = 1, array $options = array()) {
+ $options += array('conditions' => null);
$settings = static::config();
if (!isset($settings[$name])) {
return false;
}
+ $conditions = $options['conditions'];
if (is_callable($conditions) && !$conditions()) {
return false;
View
24 libraries/lithium/storage/Session.php
@@ -81,6 +81,7 @@ public static function isStarted($name = null) {
* comparing the session start time to the expiration time set in the configuration, and any
* security settings.
*
+ * @todo Implement
* @param string $name Named session configuration.
* @return boolean Returns true if the current session is active and valid.
*/
@@ -97,7 +98,7 @@ public static function isValid($name = null) {
* @filter This method may be filtered.
*/
public static function read($key = null, array $options = array()) {
- $defaults = array('name' => null);
+ $defaults = array('name' => null, 'strategies' => true);
$options += $defaults;
$method = ($name = $options['name']) ? static::adapter($name)->read($key, $options) : null;
$settings = static::_config($name);
@@ -114,10 +115,9 @@ public static function read($key = null, array $options = array()) {
}
$filters = $settings['filters'];
$result = static::_filter(__FUNCTION__, compact('key', 'options'), $method, $filters);
- $options += array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
- $strategies = (isset($options['strategies']) && $options['strategies'] === true);
- if (!isset($options['strategies']) || $strategies) {
+ if ($options['strategies']) {
+ $options += array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
return static::applyStrategies(__FUNCTION__, $name, $result, $options);
}
return $result;
@@ -133,7 +133,7 @@ public static function read($key = null, array $options = array()) {
* @filter This method may be filtered.
*/
public static function write($key, $value = null, array $options = array()) {
- $defaults = array('name' => null);
+ $defaults = array('name' => null, 'strategies' => true);
$options += $defaults;
if (is_resource($value) || !static::$_configurations) {
@@ -152,9 +152,8 @@ public static function write($key, $value = null, array $options = array()) {
}
$result = false;
$settings = static::_config($name);
- $strategies = (isset($options['strategies']) && $options['strategies'] === true);
- if (!isset($options['strategies']) || $strategies) {
+ if ($options['strategies']) {
$options += array('key' => $key, 'class' => __CLASS__);
$value = static::applyStrategies(__FUNCTION__, $name, $value, $options);
}
@@ -177,7 +176,7 @@ public static function write($key, $value = null, array $options = array()) {
* @filter This method may be filtered.
*/
public static function delete($key, array $options = array()) {
- $defaults = array('name' => null);
+ $defaults = array('name' => null, 'strategies' => true);
$options += $defaults;
$methods = array();
@@ -192,10 +191,9 @@ public static function delete($key, array $options = array()) {
}
}
$result = false;
- $strategies = (isset($options['strategies']) && $options['strategies'] === true);
$options += array('key' => $key, 'class' => __CLASS__);
- if (!isset($options['strategies']) || $strategies) {
+ if ($options['strategies']) {
$options += array('key' => $key, 'class' => __CLASS__);
$key = static::applyStrategies(__FUNCTION__, $name, $key, $options);
}
@@ -218,7 +216,7 @@ public static function delete($key, array $options = array()) {
* @filter This method may be filtered.
*/
public static function check($key, array $options = array()) {
- $defaults = array('name' => null);
+ $defaults = array('name' => null, 'strategies' => true);
$options += $defaults;
$methods = array();
@@ -239,6 +237,10 @@ public static function check($key, array $options = array()) {
$filters = $settings['filters'];
$result = $result || static::_filter(__FUNCTION__, $params, $method, $filters);
}
+ if ($options['strategies']) {
+ $options += array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
+ return static::applyStrategies(__FUNCTION__, $name, $result, $options);
+ }
return $result;
}
View
43 libraries/lithium/tests/cases/storage/CacheTest.php
@@ -200,18 +200,26 @@ public function testCacheWriteWithConditions() {
$expected = $config;
$this->assertEqual($expected, $result);
- $result = Cache::write('default', 'some_key', 'some_data', '+1 minute', function() {
+ $conditions = function() {
return false;
- });
+ };
+ $result = Cache::write(
+ 'default', 'some_key', 'some_data', '+1 minute', compact('conditions')
+ );
$this->assertFalse($result);
- $anonymous = function() use (&$config) {
+ $conditions = function() use (&$config) {
return (isset($config['default']));
};
- $result = Cache::write('default', 'some_key', 'some_data', '+1 minute', $anonymous);
+
+ $result = Cache::write(
+ 'default', 'some_key', 'some_data', '+1 minute', compact('conditions')
+ );
$this->assertTrue($result);
- $result = Cache::write('non_existing', 'key_value', 'data', '+1 minute', $anonymous);
+ $result = Cache::write(
+ 'non_existing', 'key_value', 'data', '+1 minute', compact('conditions')
+ );
$this->assertFalse($result);
}
@@ -261,21 +269,22 @@ public function testCacheReadAndWriteWithConditions() {
$expected = $config;
$this->assertEqual($expected, $result);
- $anonymous = function() use (&$config) {
+ $conditions = function() use (&$config) {
return (isset($config['default']));
};
- $result = Cache::read('non_existing', 'key_value', $anonymous);
+ $result = Cache::read('non_existing', 'key_value', compact('conditions'));
$this->assertFalse($result);
- $result = Cache::read('default', 'key_value', $anonymous);
+ $result = Cache::read('default', 'key_value', compact('conditions'));
$this->assertFalse($result);
- $result = Cache::write('default', 'keyed', 'some data', '+1 minute', $anonymous);
+ $result = Cache::write('default', 'keyed', 'some data', '+1 minute', compact('conditions'));
$this->assertTrue($result);
- $result = Cache::write('default', 'keyed', 'some data', '+1 minute', function() {
+ $conditions = function() {
return false;
- });
+ };
+ $result = Cache::write('default', 'keyed', 'some data', '+1 minute', compact('conditions'));
$this->assertFalse($result);
}
@@ -308,19 +317,23 @@ public function testCacheWriteAndDeleteWithConditions() {
$expected = $config;
$this->assertEqual($expected, $result);
- $anonymous = function() use (&$config) {
+ $conditions = function() use (&$config) {
return (isset($config['default']));
};
- $result = Cache::delete('non_existing', 'key_value', $anonymous);
+ $result = Cache::delete('non_existing', 'key_value', compact('conditions'));
$this->assertFalse($result);
$result = Cache::write('default', 'to delete', 'dead data', '+1 minute');
$this->assertTrue($result);
- $result = Cache::delete('default', 'to delete', function() { return false; });
+ $result = Cache::delete('default', 'to delete', array(
+ 'conditions' => function() {
+ return false;
+ }
+ ));
$this->assertFalse($result);
- $result = Cache::delete('default', 'to delete', $anonymous);
+ $result = Cache::delete('default', 'to delete', compact('conditions'));
$this->assertTrue($result);
}

0 comments on commit 4dc16e5

Please sign in to comment.