Refactoring session classes.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3958 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2006-11-25 08:43:17 +00:00
parent d4af73c2a6
commit 1fa21aa551
5 changed files with 80 additions and 46 deletions

View file

@ -37,13 +37,23 @@
*
*/
class SessionComponent extends CakeSession {
/**
* Used to determine if methods implementation is used, or bypassed
*
* @var boolean
*/
var $__active = true;
/**
* Class constructor
*
* @param string $base
*/
function __construct($base = null) {
parent::__construct($base);
if (!defined('AUTO_SESSION') || AUTO_SESSION === true) {
parent::__construct($base);
} else {
$this->__active = false;
}
}
/**
* Startup method. Copies controller data locally for rendering flash messages.
@ -68,7 +78,9 @@ class SessionComponent extends CakeSession {
* @param string $value The value you want to store in a session.
*/
function write($name, $value) {
$this->writeSessionVar($name, $value);
if ($this->__active === true) {
$this->writeSessionVar($name, $value);
}
}
/**
* Used to read a session values for a key or return values for all keys.
@ -81,7 +93,10 @@ class SessionComponent extends CakeSession {
* @return values from the session vars
*/
function read($name = null) {
return $this->readSessionVar($name);
if ($this->__active === true) {
return $this->readSessionVar($name);
}
return false;
}
/**
* Used to delete a session variable.
@ -92,7 +107,10 @@ class SessionComponent extends CakeSession {
* @return boolean, true is session variable is set and can be deleted, false is variable was not set.
*/
function del($name) {
return $this->delSessionVar($name);
if ($this->__active === true) {
return $this->delSessionVar($name);
}
return false;
}
/**
* Wrapper for SessionComponent::del();
@ -103,7 +121,10 @@ class SessionComponent extends CakeSession {
* @return boolean, true is session variable is set and can be deleted, false is variable was not set.
*/
function delete($name) {
return $this->del($name);
if ($this->__active === true) {
return $this->del($name);
}
return false;
}
/**
* Used to check if a session variable is set
@ -114,7 +135,10 @@ class SessionComponent extends CakeSession {
* @return boolean true is session variable is set, false if not
*/
function check($name) {
return $this->checkSessionVar($name);
if ($this->__active === true) {
return $this->checkSessionVar($name);
}
return false;
}
/**
* Used to determine the last error in a session.
@ -124,7 +148,10 @@ class SessionComponent extends CakeSession {
* @return string Last session error
*/
function error() {
return $this->getLastError();
if ($this->__active === true) {
return $this->getLastError();
}
return false;
}
/**
* Used to set a session variable that can be used to output messages in the view.
@ -139,27 +166,29 @@ class SessionComponent extends CakeSession {
* @param string $key Message key, default is 'flash'
*/
function setFlash($flashMessage, $layout = 'default', $params = array(), $key = 'flash') {
if ($layout == 'default') {
$out = '<div id="' . $key . 'Message" class="message">' . $flashMessage . '</div>';
} else if($layout == '' || $layout == null) {
$out = $flashMessage;
} else {
$ctrl = null;
$view = new View($ctrl);
$view->base = $this->base;
$view->webroot = $this->webroot;
$view->here = $this->here;
$view->params = $this->params;
$view->action = $this->action;
$view->data = $this->data;
$view->plugin = $this->plugin;
$view->helpers = array('Html');
$view->layout = $layout;
$view->pageTitle = '';
$view->_viewVars = $params;
$out = $view->renderLayout($flashMessage);
if ($this->__active === true) {
if ($layout == 'default') {
$out = '<div id="' . $key . 'Message" class="message">' . $flashMessage . '</div>';
} elseif ($layout == '' || $layout == null) {
$out = $flashMessage;
} else {
$ctrl = null;
$view = new View($ctrl);
$view->base = $this->base;
$view->webroot = $this->webroot;
$view->here = $this->here;
$view->params = $this->params;
$view->action = $this->action;
$view->data = $this->data;
$view->plugin = $this->plugin;
$view->helpers = array('Html');
$view->layout = $layout;
$view->pageTitle = '';
$view->_viewVars = $params;
$out = $view->renderLayout($flashMessage);
}
$this->write('Message.' . $key, $out);
}
$this->write('Message.' . $key, $out);
}
/**
* This method is deprecated.
@ -170,12 +199,14 @@ class SessionComponent extends CakeSession {
* @deprecated
*/
function flash($key = 'flash') {
if ($this->check('Message.' . $key)) {
e($this->read('Message.' . $key));
$this->del('Message.' . $key);
} else {
return false;
if ($this->__active === true) {
if ($this->check('Message.' . $key)) {
e($this->read('Message.' . $key));
$this->del('Message.' . $key);
return;
}
}
return false;
}
/**
* Used to renew a session id
@ -183,7 +214,9 @@ class SessionComponent extends CakeSession {
* In your controller: $this->Session->renew();
*/
function renew() {
parent::renew();
if ($this->__active === true) {
parent::renew();
}
}
/**
* Used to check for a valid session.
@ -193,7 +226,10 @@ class SessionComponent extends CakeSession {
* @return boolean true is session is valid, false is session is invalid
*/
function valid() {
return $this->isValid();
if ($this->__active === true) {
return $this->isValid();
}
return false;
}
/**
* Used to destroy sessions
@ -201,7 +237,9 @@ class SessionComponent extends CakeSession {
* In your controller:. $this->Session->destroy();
*/
function destroy() {
$this->destroyInvalid();
if ($this->__active === true) {
$this->destroyInvalid();
}
}
}

View file

@ -175,7 +175,7 @@ class Controller extends Object {
*
* @var unknown_type
*/
var $components = array();
var $components = array('Session');
/**
* Enter description here...
*
@ -258,10 +258,6 @@ class Controller extends Object {
$this->modelClass = ucwords(Inflector::singularize($this->name));
$this->modelKey = Inflector::underscore($this->modelClass);
if (!defined('AUTO_SESSION') || AUTO_SESSION == true) {
$this->components[] = 'Session';
}
if (is_subclass_of($this, 'AppController')) {
$appVars = get_class_vars('AppController');

View file

@ -243,7 +243,7 @@ class Scaffold extends Object{
if ($this->controllerClass->{$this->modelKey}->save($this->controllerClass->params['data'])) {
if ($this->controllerClass->_afterScaffoldSave($type)) {
if (is_object($this->controllerClass->Session)) {
if (is_object($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
$this->controllerClass->Session->setFlash(
'The ' . Inflector::humanize($this->modelKey) . ' has been ' . $success . '.');
$this->controllerClass->redirect(
@ -258,7 +258,7 @@ class Scaffold extends Object{
return $this->controllerClass->_afterScaffoldSaveError($type);
}
} else {
if (is_object($this->controllerClass->Session)) {
if (is_object($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
$this->controllerClass->Session->setFlash('Please correct errors below.');
}
@ -299,7 +299,7 @@ class Scaffold extends Object{
$id=$params['pass'][0];
if ($this->controllerClass->{$this->modelKey}->del($id)) {
if (is_object($this->controllerClass->Session)) {
if (is_object($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
$this->controllerClass->Session->setFlash(
'The ' . Inflector::humanize($this->modelKey) . ' with id: ' . $id
. ' has been deleted.');
@ -311,7 +311,7 @@ class Scaffold extends Object{
'/' . Inflector::underscore($this->controllerClass->viewPath));
}
} else {
if (is_object($this->controllerClass->Session)) {
if (is_object($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
$this->controllerClass->Session->setFlash(
'There was an error deleting the ' . Inflector::humanize($this->modelKey)
. ' with the id ' . $id);

View file

@ -48,7 +48,7 @@ class SessionHelper extends CakeSession {
* @param string $base
*/
function __construct($base = null) {
if (!defined('AUTO_SESSION') || AUTO_SESSION == true) {
if (!defined('AUTO_SESSION') || AUTO_SESSION === true) {
parent::__construct($base, false);
} else {
$this->__active = false;

View file

@ -28,7 +28,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CakePHP : <?php echo $title_for_layout;?></title>
<title>CakePHP : <?php echo $title_for_layout;?></title>
<link rel="icon" href="<?php echo $this->webroot . 'favicon.ico';?>" type="image/x-icon" />
<link rel="shortcut icon" href="<?php echo $this->webroot . 'favicon.ico';?>" type="image/x-icon" />
<?php echo $html->css('cake.generic');?>