From 1fa21aa551eff57a2d0e0eb1d020f649acc21b05 Mon Sep 17 00:00:00 2001 From: phpnut Date: Sat, 25 Nov 2006 08:43:17 +0000 Subject: [PATCH] Refactoring session classes. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3958 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/controller/components/session.php | 108 ++++++++++++------ cake/libs/controller/controller.php | 6 +- cake/libs/controller/scaffold.php | 8 +- cake/libs/view/helpers/session.php | 2 +- .../libs/view/templates/layouts/default.thtml | 2 +- 5 files changed, 80 insertions(+), 46 deletions(-) diff --git a/cake/libs/controller/components/session.php b/cake/libs/controller/components/session.php index ae9ff3fa2..950de024a 100644 --- a/cake/libs/controller/components/session.php +++ b/cake/libs/controller/components/session.php @@ -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 = '
' . $flashMessage . '
'; - } 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 = '
' . $flashMessage . '
'; + } 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(); + } } } diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 029e5f58b..bdbe7a72a 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -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'); diff --git a/cake/libs/controller/scaffold.php b/cake/libs/controller/scaffold.php index 3a26c0bde..85277ce76 100644 --- a/cake/libs/controller/scaffold.php +++ b/cake/libs/controller/scaffold.php @@ -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); diff --git a/cake/libs/view/helpers/session.php b/cake/libs/view/helpers/session.php index 6a6894150..ef8e1a821 100644 --- a/cake/libs/view/helpers/session.php +++ b/cake/libs/view/helpers/session.php @@ -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; diff --git a/cake/libs/view/templates/layouts/default.thtml b/cake/libs/view/templates/layouts/default.thtml index 150f56e9d..aa231406a 100644 --- a/cake/libs/view/templates/layouts/default.thtml +++ b/cake/libs/view/templates/layouts/default.thtml @@ -28,7 +28,7 @@ -CakePHP : <?php echo $title_for_layout;?> +CakePHP : <?php echo $title_for_layout;?> css('cake.generic');?>