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() { }
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;
}
}
}

View file

@ -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();

View file

@ -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;

View file

@ -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);
}

View file

@ -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);
}
}
?>