added option to modify element cache name, #2327, see tests. Adding auth key for flash message, #2996

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5497 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-08-07 07:44:12 +00:00
parent b91b501314
commit 889e62881b
3 changed files with 63 additions and 9 deletions

View file

@ -296,7 +296,7 @@ class AuthComponent extends Object {
}
return true;
} else {
$this->Session->setFlash($this->loginError, 'default', array(), 'Auth.login');
$this->Session->setFlash($this->loginError, 'default', array(), 'auth');
unset($controller->data[$this->userModel][$this->fields['password']]);
}
return false;
@ -347,7 +347,7 @@ class AuthComponent extends Object {
return true;
}
}
$this->Session->setFlash($this->authError);
$this->Session->setFlash($this->authError, 'default', array(), 'auth');
$controller->redirect($controller->referer(), null, true);
return false;
} else {
@ -467,6 +467,7 @@ class AuthComponent extends Object {
$object = $auth[$type];
} else {
$type = $auth;
return compact('type');
}
return compact('type', 'object');
}

View file

@ -420,24 +420,29 @@ class View extends Object {
* @access public
*/
function element($name, $params = array()) {
if (in_array('cache', array_keys($params))) {
if (isset($params['cache'])) {
$expires = '+1 day';
if ($params['cache'] !== true) {
$key = null;
if (is_array($params['cache'])) {
$expires = $params['cache']['time'];
$key = convertSlash($params['cache']['key']);
} elseif ($params['cache'] !== true) {
$expires = $params['cache'];
$key = implode('_', array_keys($params));
}
if ($expires) {
$plugin = null;
if (isset($params['plugin'])) {
$plugin = $params['plugin'];
$plugin = $params['plugin'].'_';
}
$cacheFile = 'element_' . $plugin .'_' . convertSlash($name);
$cacheFile = 'element_' . $key .'_'. $plugin . convertSlash($name);
$cache = cache('views' . DS . $cacheFile, null, $expires);
if ($cache) {
if (is_string($cache)) {
return $cache;
} else {
$element = $this->renderElement($name, $params);
cache('views' . DS . $cacheFile, $element, $expires);
return $element;
cache('views' . DS . $cacheFile, $element, $expires);
return $element;
}
}
}

View file

@ -38,6 +38,13 @@ class PostsController extends Controller {
}
}
class TestView extends View {
function renderElement($name, $params = array()) {
return $name;
}
}
/**
* Short description for class.
*
@ -75,6 +82,47 @@ class ViewTest extends UnitTestCase {
$this->assertEqual($this->view->__scripts, array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);'));
}
function testElementCache() {
$View = new TestView($this->PostsController);
$element = 'element_name';
$result = $View->element($element);
$this->assertEqual($result, $element);
$cached = false;
$result = $View->element($element, array('cache'=>'+1 second'));
if(file_exists(CACHE . 'views' . DS . 'element_cache_'.$element)) {
$cached = true;
unlink(CACHE . 'views' . DS . 'element_cache_'.$element);
}
$this->assertTrue($cached);
$cached = false;
$result = $View->element($element, array('cache'=>'+1 second', 'other_param'=> true, 'anotherParam'=> true));
if(file_exists(CACHE . 'views' . DS . 'element_cache_other_param_anotherParam_'.$element)) {
$cached = true;
unlink(CACHE . 'views' . DS . 'element_cache_other_param_anotherParam_'.$element);
}
$this->assertTrue($cached);
$cached = false;
$result = $View->element($element, array('cache'=>array('time'=>'+1 second', 'key'=>'/whatever/here')));
if(file_exists(CACHE . 'views' . DS . 'element_'.convertSlash('/whatever/here').'_'.$element)) {
$cached = true;
unlink(CACHE . 'views' . DS . 'element_'.convertSlash('/whatever/here').'_'.$element);
}
$this->assertTrue($cached);
$cached = false;
$result = $View->element($element, array('cache'=>array('time'=>'+1 second', 'key'=>'whatever_here')));
if(file_exists(CACHE . 'views' . DS . 'element_whatever_here_'.$element)) {
$cached = true;
unlink(CACHE . 'views' . DS . 'element_whatever_here_'.$element);
}
$this->assertTrue($cached);
$this->assertEqual($result, $element);
}
function tearDown() {
unset($this->view);
unset($this->PostsController);