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 { class SessionComponent extends CakeSession {
/**
* Used to determine if methods implementation is used, or bypassed
*
* @var boolean
*/
var $__active = true;
/** /**
* Class constructor * Class constructor
* *
* @param string $base * @param string $base
*/ */
function __construct($base = null) { 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. * 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. * @param string $value The value you want to store in a session.
*/ */
function write($name, $value) { 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. * 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 * @return values from the session vars
*/ */
function read($name = null) { function read($name = null) {
return $this->readSessionVar($name); if ($this->__active === true) {
return $this->readSessionVar($name);
}
return false;
} }
/** /**
* Used to delete a session variable. * 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. * @return boolean, true is session variable is set and can be deleted, false is variable was not set.
*/ */
function del($name) { function del($name) {
return $this->delSessionVar($name); if ($this->__active === true) {
return $this->delSessionVar($name);
}
return false;
} }
/** /**
* Wrapper for SessionComponent::del(); * 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. * @return boolean, true is session variable is set and can be deleted, false is variable was not set.
*/ */
function delete($name) { 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 * 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 * @return boolean true is session variable is set, false if not
*/ */
function check($name) { 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. * Used to determine the last error in a session.
@ -124,7 +148,10 @@ class SessionComponent extends CakeSession {
* @return string Last session error * @return string Last session error
*/ */
function 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. * 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' * @param string $key Message key, default is 'flash'
*/ */
function setFlash($flashMessage, $layout = 'default', $params = array(), $key = 'flash') { function setFlash($flashMessage, $layout = 'default', $params = array(), $key = 'flash') {
if ($layout == 'default') { if ($this->__active === true) {
$out = '<div id="' . $key . 'Message" class="message">' . $flashMessage . '</div>'; if ($layout == 'default') {
} else if($layout == '' || $layout == null) { $out = '<div id="' . $key . 'Message" class="message">' . $flashMessage . '</div>';
$out = $flashMessage; } elseif ($layout == '' || $layout == null) {
} else { $out = $flashMessage;
$ctrl = null; } else {
$view = new View($ctrl); $ctrl = null;
$view->base = $this->base; $view = new View($ctrl);
$view->webroot = $this->webroot; $view->base = $this->base;
$view->here = $this->here; $view->webroot = $this->webroot;
$view->params = $this->params; $view->here = $this->here;
$view->action = $this->action; $view->params = $this->params;
$view->data = $this->data; $view->action = $this->action;
$view->plugin = $this->plugin; $view->data = $this->data;
$view->helpers = array('Html'); $view->plugin = $this->plugin;
$view->layout = $layout; $view->helpers = array('Html');
$view->pageTitle = ''; $view->layout = $layout;
$view->_viewVars = $params; $view->pageTitle = '';
$out = $view->renderLayout($flashMessage); $view->_viewVars = $params;
$out = $view->renderLayout($flashMessage);
}
$this->write('Message.' . $key, $out);
} }
$this->write('Message.' . $key, $out);
} }
/** /**
* This method is deprecated. * This method is deprecated.
@ -170,12 +199,14 @@ class SessionComponent extends CakeSession {
* @deprecated * @deprecated
*/ */
function flash($key = 'flash') { function flash($key = 'flash') {
if ($this->check('Message.' . $key)) { if ($this->__active === true) {
e($this->read('Message.' . $key)); if ($this->check('Message.' . $key)) {
$this->del('Message.' . $key); e($this->read('Message.' . $key));
} else { $this->del('Message.' . $key);
return false; return;
}
} }
return false;
} }
/** /**
* Used to renew a session id * Used to renew a session id
@ -183,7 +214,9 @@ class SessionComponent extends CakeSession {
* In your controller: $this->Session->renew(); * In your controller: $this->Session->renew();
*/ */
function renew() { function renew() {
parent::renew(); if ($this->__active === true) {
parent::renew();
}
} }
/** /**
* Used to check for a valid session. * 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 * @return boolean true is session is valid, false is session is invalid
*/ */
function valid() { function valid() {
return $this->isValid(); if ($this->__active === true) {
return $this->isValid();
}
return false;
} }
/** /**
* Used to destroy sessions * Used to destroy sessions
@ -201,7 +237,9 @@ class SessionComponent extends CakeSession {
* In your controller:. $this->Session->destroy(); * In your controller:. $this->Session->destroy();
*/ */
function 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 unknown_type
*/ */
var $components = array(); var $components = array('Session');
/** /**
* Enter description here... * Enter description here...
* *
@ -258,10 +258,6 @@ class Controller extends Object {
$this->modelClass = ucwords(Inflector::singularize($this->name)); $this->modelClass = ucwords(Inflector::singularize($this->name));
$this->modelKey = Inflector::underscore($this->modelClass); $this->modelKey = Inflector::underscore($this->modelClass);
if (!defined('AUTO_SESSION') || AUTO_SESSION == true) {
$this->components[] = 'Session';
}
if (is_subclass_of($this, 'AppController')) { if (is_subclass_of($this, 'AppController')) {
$appVars = get_class_vars('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->{$this->modelKey}->save($this->controllerClass->params['data'])) {
if ($this->controllerClass->_afterScaffoldSave($type)) { 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( $this->controllerClass->Session->setFlash(
'The ' . Inflector::humanize($this->modelKey) . ' has been ' . $success . '.'); 'The ' . Inflector::humanize($this->modelKey) . ' has been ' . $success . '.');
$this->controllerClass->redirect( $this->controllerClass->redirect(
@ -258,7 +258,7 @@ class Scaffold extends Object{
return $this->controllerClass->_afterScaffoldSaveError($type); return $this->controllerClass->_afterScaffoldSaveError($type);
} }
} else { } 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.'); $this->controllerClass->Session->setFlash('Please correct errors below.');
} }
@ -299,7 +299,7 @@ class Scaffold extends Object{
$id=$params['pass'][0]; $id=$params['pass'][0];
if ($this->controllerClass->{$this->modelKey}->del($id)) { 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( $this->controllerClass->Session->setFlash(
'The ' . Inflector::humanize($this->modelKey) . ' with id: ' . $id 'The ' . Inflector::humanize($this->modelKey) . ' with id: ' . $id
. ' has been deleted.'); . ' has been deleted.');
@ -311,7 +311,7 @@ class Scaffold extends Object{
'/' . Inflector::underscore($this->controllerClass->viewPath)); '/' . Inflector::underscore($this->controllerClass->viewPath));
} }
} else { } else {
if (is_object($this->controllerClass->Session)) { if (is_object($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
$this->controllerClass->Session->setFlash( $this->controllerClass->Session->setFlash(
'There was an error deleting the ' . Inflector::humanize($this->modelKey) 'There was an error deleting the ' . Inflector::humanize($this->modelKey)
. ' with the id ' . $id); . ' with the id ' . $id);

View file

@ -48,7 +48,7 @@ class SessionHelper extends CakeSession {
* @param string $base * @param string $base
*/ */
function __construct($base = null) { function __construct($base = null) {
if (!defined('AUTO_SESSION') || AUTO_SESSION == true) { if (!defined('AUTO_SESSION') || AUTO_SESSION === true) {
parent::__construct($base, false); parent::__construct($base, false);
} else { } else {
$this->__active = false; $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"> <!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"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <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="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" /> <link rel="shortcut icon" href="<?php echo $this->webroot . 'favicon.ico';?>" type="image/x-icon" />
<?php echo $html->css('cake.generic');?> <?php echo $html->css('cake.generic');?>