<?php
abstract class woo_base_Registry{
abstract protected function get($key);
abstract protected function set($key,$val);
}
class woo_base_RequestRegistry extends woo_base_Registry{
private $values=array();
private static $instance;
private function __construct(){}
static function instance(){
if(!isset(self::$instance)){
self::$instance=new self();
}
return self::$instance;
}
protected function set($key,$val){
$this->values[$key]=$val;
}
protected function get($key){
if(isset($this->values[$key])){
return $this->values[$key];
}
return null;
}
static function getRequest(){
return self::instance()->get('request');
}
static function setRequest(woo_controller_Request $request){
return self::instance()->set('request',$request);
}
}
class woo_base_SessionRegistry extends woo_base_Registry{
private static $instance;
private function __construct(){
session_start();
}
static function instance(){
if(!isset(self::$instance)){
self::$instance=new self();
}
return self::$instance;
}
protected function get($key){
if(isset($_SESSION[__CLASS__][$key])){
return $_SESSION[__CLASS__][$key];
}
return null;
}
protected function set($key,$val){
$_SESSION[__CLASS__][$key]=$val;
}
function setComplex(Complex $complex){
self::instance()->set('complex',$complex);
}
function getComplex(){
return self::instance()->get('complex');
}
}
class woo_base_ApplicationRegistry extends woo_base_Registry{
private static $instance;
private $freezedir="data";
private $values=array();
private $mtimes=array();
private function __construct(){}
static function instance(){
if(!isset(self::$instance)){
self::$instance=new self();
}
return self::$instance;
}
protected function get($key){
$path=$this->freezedir.DIRECTORY_SEPARATOR.$key;
if(file_exists($path)){
clearstatcache();
$mtime=filemtime($path);
if(!isset($this->mtimes[$key])){
$this->mtimes[$key]=0;
}
if($mtime > $this->mtimes[$key]){
$data=file_get_contents($path);
$this->mtimes[$key]=$mtime;
return ($this->values[$key]=unserialize($data));
}
}
if(isset($this->values[$key])){
return $this->values[$key];
}
return null;
}
protected function set($key,$val){
$this->values[$key]=$val;
$path=$this->freezedir.DIRECTORY_SEPARATOR.$key;
file_put_contents($path,serialize($val));
$this->mtimes[$key]=time();
}
static function getDsn(){
return self::instance()->get('dsn');
}
static function setDsn($dsn){
return self::instance()->set('dsn',$dsn);
}
}
class woo_controller_Request{}
class Complex{}
$request=new woo_controller_Request();
$complex=new Complex();
$re=woo_base_RequestRegistry::instance();
woo_base_RequestRegistry::setRequest($request);
print_r(woo_base_RequestRegistry::getRequest('request'));
echo "<br>";
$session=woo_base_SessionRegistry::instance();
$session->setComplex( $complex );
print_r($session->getComplex());
$result=woo_base_ApplicationRegistry::instance();
woo_base_ApplicationRegistry::setDsn('bb14524');
echo woo_base_ApplicationRegistry::getDsn();
?>