From 66338a66bb4bb64d73e51406958414b63e52bf00 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Fri, 15 Aug 2008 04:49:02 +0000 Subject: [PATCH] Fixed the autoload issue when unserializing objects stored in the session. Added in the freeze / thaw methods for storing singleton objects in the session for later retrieval. git-svn-id: http://svn.cleancode.org/svn/pickles@36 4d10bc64-7434-11dc-a737-d2d0f8310089 --- classes/Config.php | 45 +++++++++++++++++++++++++++--------------- classes/Controller.php | 4 ++-- classes/DB.php | 11 +++++++++-- classes/Session.php | 7 +++---- classes/Singleton.php | 13 ++++++++++++ 5 files changed, 56 insertions(+), 24 deletions(-) diff --git a/classes/Config.php b/classes/Config.php index b7a5720..ddc447a 100755 --- a/classes/Config.php +++ b/classes/Config.php @@ -7,8 +7,15 @@ class Config extends Singleton { private function __construct() { } public static function getInstance() { - if (!self::$instance instanceof Config) { - self::$instance = new Config(); + $session = Session::getInstance(); + + $class = __CLASS__; + + if (isset($session->$class)) { + self::$instance = Singleton::thaw($class); + } + else if (!self::$instance instanceof $class) { + self::$instance = new $class(); } return self::$instance; @@ -18,24 +25,30 @@ class Config extends Singleton { // @todo no hardcoded paths! $file = '/var/www/josh/pickles/config/' . $site . '.xml'; - if (file_exists($file)) { - $config_array = ArrayUtils::object2array(simplexml_load_file($file)); + if (!isset($this->file) || $this->file != $file) { + if (file_exists($file)) { + $this->file = $file; - if (is_array($config_array)) { - foreach ($config_array as $variable => $value) { - if ($value == 'true' || $value == array()) { - $value = (bool) $value; + $config_array = ArrayUtils::object2array(simplexml_load_file($file)); + + if (is_array($config_array)) { + foreach ($config_array as $variable => $value) { + if ($value == 'true' || $value == array()) { + $value = (bool) $value; + } + + $this->$variable = $value == array() ? (bool) $value : $value; } - - $this->$variable = $value == array() ? (bool) $value : $value; } - } - return true; - } - else { - Error::addError('Unable to load the configuration file'); - return false; + $this->freeze(); + + return true; + } + else { + Error::addError('Unable to load the configuration file'); + return false; + } } } diff --git a/classes/Controller.php b/classes/Controller.php index a49e9a2..bcff937 100755 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -31,7 +31,7 @@ class Controller extends Object { } // Grab the passed in model or use the default - $name = isset($_REQUEST['model']) ? $_REQUEST['model'] : $this->config->get('navigation', 'default'); + $name = isset($_REQUEST['model']) ? str_replace('-', '_', $_REQUEST['model']) : $this->config->get('navigation', 'default'); if ($name == 'logout') { Security::logout(); @@ -60,7 +60,7 @@ class Controller extends Object { else { $this->model = new Model(); } - + if ($this->model->get('auth') == false) { $this->model->set('auth', $this->config->get('behavior', 'auth')); } diff --git a/classes/DB.php b/classes/DB.php index f9f348e..41c59a0 100755 --- a/classes/DB.php +++ b/classes/DB.php @@ -15,8 +15,15 @@ class DB extends Singleton { private function __construct() { } public static function getInstance() { - if (!self::$instance instanceof DB) { - self::$instance = new DB(); + $session = Session::getInstance(); + + $class = __CLASS__; + + if (isset($session->$class)) { + self::$instance = Singleton::thaw($class); + } + else if (!self::$instance instanceof $class) { + self::$instance = new $class(); } return self::$instance; diff --git a/classes/Session.php b/classes/Session.php index ff646c6..e5e71df 100755 --- a/classes/Session.php +++ b/classes/Session.php @@ -4,14 +4,13 @@ class Session extends Singleton { private static $instance; - public $id = null; + public static $id = null; private function __construct() { if (ini_get('session.auto_start') == 0) { session_start(); + $this->id = session_id(); } - - $this->id = session_id(); } public static function getInstance() { @@ -43,7 +42,7 @@ class Session extends Singleton { return $_SESSION[$var]; } - function __set($var,$val) { + function __set($var, $val) { return ($_SESSION[$var] = $val); } diff --git a/classes/Singleton.php b/classes/Singleton.php index 7c507eb..8fd6292 100644 --- a/classes/Singleton.php +++ b/classes/Singleton.php @@ -25,6 +25,19 @@ class Singleton { $this->$$variable = $value; } + public function freeze() { + $session = Session::getInstance(); + $class = get_class($this); + $session->$class = serialize($this); + } + + public static function thaw($class) { + __autoload($class); + + $session = Session::getInstance(); + return unserialize($session->$class); + } + } ?>