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
This commit is contained in:
Josh Sherman 2008-08-15 04:49:02 +00:00
parent 5198febae5
commit 66338a66bb
5 changed files with 56 additions and 24 deletions

View file

@ -7,8 +7,15 @@ class Config extends Singleton {
private function __construct() { } private function __construct() { }
public static function getInstance() { public static function getInstance() {
if (!self::$instance instanceof Config) { $session = Session::getInstance();
self::$instance = new Config();
$class = __CLASS__;
if (isset($session->$class)) {
self::$instance = Singleton::thaw($class);
}
else if (!self::$instance instanceof $class) {
self::$instance = new $class();
} }
return self::$instance; return self::$instance;
@ -18,24 +25,30 @@ class Config extends Singleton {
// @todo no hardcoded paths! // @todo no hardcoded paths!
$file = '/var/www/josh/pickles/config/' . $site . '.xml'; $file = '/var/www/josh/pickles/config/' . $site . '.xml';
if (file_exists($file)) { if (!isset($this->file) || $this->file != $file) {
$config_array = ArrayUtils::object2array(simplexml_load_file($file)); if (file_exists($file)) {
$this->file = $file;
if (is_array($config_array)) { $config_array = ArrayUtils::object2array(simplexml_load_file($file));
foreach ($config_array as $variable => $value) {
if ($value == 'true' || $value == array()) { if (is_array($config_array)) {
$value = (bool) $value; 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; $this->freeze();
}
else { return true;
Error::addError('Unable to load the configuration file'); }
return false; else {
Error::addError('Unable to load the configuration file');
return false;
}
} }
} }

View file

@ -31,7 +31,7 @@ class Controller extends Object {
} }
// Grab the passed in model or use the default // 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') { if ($name == 'logout') {
Security::logout(); Security::logout();
@ -60,7 +60,7 @@ class Controller extends Object {
else { else {
$this->model = new Model(); $this->model = new Model();
} }
if ($this->model->get('auth') == false) { if ($this->model->get('auth') == false) {
$this->model->set('auth', $this->config->get('behavior', 'auth')); $this->model->set('auth', $this->config->get('behavior', 'auth'));
} }

View file

@ -15,8 +15,15 @@ class DB extends Singleton {
private function __construct() { } private function __construct() { }
public static function getInstance() { public static function getInstance() {
if (!self::$instance instanceof DB) { $session = Session::getInstance();
self::$instance = new DB();
$class = __CLASS__;
if (isset($session->$class)) {
self::$instance = Singleton::thaw($class);
}
else if (!self::$instance instanceof $class) {
self::$instance = new $class();
} }
return self::$instance; return self::$instance;

View file

@ -4,14 +4,13 @@ class Session extends Singleton {
private static $instance; private static $instance;
public $id = null; public static $id = null;
private function __construct() { private function __construct() {
if (ini_get('session.auto_start') == 0) { if (ini_get('session.auto_start') == 0) {
session_start(); session_start();
$this->id = session_id();
} }
$this->id = session_id();
} }
public static function getInstance() { public static function getInstance() {
@ -43,7 +42,7 @@ class Session extends Singleton {
return $_SESSION[$var]; return $_SESSION[$var];
} }
function __set($var,$val) { function __set($var, $val) {
return ($_SESSION[$var] = $val); return ($_SESSION[$var] = $val);
} }

View file

@ -25,6 +25,19 @@ class Singleton {
$this->$$variable = $value; $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);
}
} }
?> ?>