Skip to content

Commit

Permalink
Refactoring \storage\Session::check() to receive functions from ada…
Browse files Browse the repository at this point in the history
…pters. Updating adapters accordingly.
  • Loading branch information
nateabele committed Mar 17, 2010
1 parent c1ffc95 commit c730391
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
36 changes: 23 additions & 13 deletions libraries/lithium/storage/Session.php
Expand Up @@ -106,7 +106,7 @@ public static function read($key, array $options = array()) {
}
}
$filters = $settings['filters'];
$result = static::_filter(__METHOD__, compact('key', 'options'), $method, $filters);
$result = static::_filter(__FUNCTION__, compact('key', 'options'), $method, $filters);
return static::applyStrategies(__FUNCTION__, $name, $result, 'LIFO');
}

Expand Down Expand Up @@ -144,7 +144,7 @@ public static function write($key, $value = null, array $options = array()) {
foreach ($methods as $name => $method) {
$params = compact('key', 'value', 'options');
$filters = $settings['filters'];
$result = $result || static::_filter(__METHOD__, $params, $method, $filters);
$result = $result || static::_filter(__FUNCTION__, $params, $method, $filters);
}
return $result;
}
Expand All @@ -167,19 +167,19 @@ public static function delete($key, array $options = array()) {
if ($name = $options['name']) {
$methods = array($name => static::adapter($name)->delete($key, $options));
} else {
foreach (array_keys(static::$_configurations) as $name) {
foreach (static::$_configurations as $name => $config) {
if ($method = static::adapter($name)->delete($key, $options)) {
$methods[$name] = $method;
}
}
}
$result = false;
$settings = static::_config($name);
$params = compact('key', 'options');

foreach ($methods as $name => $method) {
$params = compact('key', 'options');
$settings = static::_config($name);
$filters = $settings['filters'];
$result = $result || static::_filter(__METHOD__, $params, $method, $filters);
$result = $result || static::_filter(__FUNCTION__, $params, $method, $filters);
}
return $result;
}
Expand All @@ -196,16 +196,26 @@ public static function delete($key, array $options = array()) {
public static function check($key, array $options = array()) {
$defaults = array('name' => null);
$options += $defaults;
$methods = array();

if ($options['name']) {
return static::adapter($options['name'])->check($key, $options);
}
foreach (array_keys(static::$_configurations) as $name) {
if (static::adapter($name)->check($key, $options)) {
return true;
if ($name = $options['name']) {
$methods = array($name => static::adapter($name)->check($key, $options));
} else {
foreach (static::$_configurations as $name => $config) {
if ($method = static::adapter($name)->check($key, $options)) {
$methods[$name] = $method;
}
}
}
return false;
$params = compact('key', 'options');
$result = false;

foreach ($methods as $name => $method) {
$settings = static::_config($name);
$filters = $settings['filters'];
$result = $result || static::_filter(__FUNCTION__, $params, $method, $filters);
}
return $result;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion libraries/lithium/storage/session/adapter/Memory.php
Expand Up @@ -29,7 +29,10 @@ public function isStarted() {
}

public function check($key, array $options = array()) {
return isset($this->_session[$key]);
$session =& $this->_session;
return function($self, $params, $chain) use (&$session) {
return isset($session[$params['key']]);
};
}

public function read($key = null, array $options = array()) {
Expand Down
33 changes: 10 additions & 23 deletions libraries/lithium/tests/cases/storage/SessionTest.php
Expand Up @@ -115,39 +115,26 @@ public function testSessionKeyCheckAndDelete() {
Session::write('key1', 'value', array('name' => 'persistent'));
Session::write('key2', 'value', array('name' => 'temp'));

$result = Session::check('key1');
$this->assertTrue($result);

$result = Session::check('key2');
$this->assertTrue($result);
$this->assertTrue(Session::check('key1'));
$this->assertTrue(Session::check('key2'));

$result = Session::check('key1', array('name' => 'persistent'));
$this->assertTrue($result);
$this->assertTrue(Session::check('key1', array('name' => 'persistent')));
$this->assertFalse(Session::check('key1', array('name' => 'temp')));

$result = Session::check('key1', array('name' => 'temp'));
$this->assertFalse($result);

$result = Session::check('key2', array('name' => 'persistent'));
$this->assertFalse($result);

$result = Session::check('key2', array('name' => 'temp'));
$this->assertTrue($result);
$this->assertFalse(Session::check('key2', array('name' => 'persistent')));
$this->assertTrue(Session::check('key2', array('name' => 'temp')));

Session::delete('key1');
$result = Session::check('key1');
$this->assertFalse($result);
$this->assertFalse(Session::check('key1'));

Session::write('key1', 'value', array('name' => 'persistent'));
$result = Session::check('key1');
$this->assertTrue($result);
$this->assertTrue(Session::check('key1'));

Session::delete('key1', array('name' => 'temp'));
$result = Session::check('key1');
$this->assertTrue($result);
$this->assertTrue(Session::check('key1'));

Session::delete('key1', array('name' => 'persistent'));
$result = Session::check('key1');
$this->assertFalse($result);
$this->assertFalse(Session::check('key1'));
}

/**
Expand Down

0 comments on commit c730391

Please sign in to comment.