Advertisement
fruffl

Untitled

Sep 16th, 2011
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.51 KB | None | 0 0
  1. abstract class  System
  2. {
  3.         const           GC_COLLECT_CYCLES           = 'gc_collect_cycles';
  4.         public          function finalize()         { if(isset($__func_exists[self::GC_COLLECT_CYCLES])) gc_collect_cycles(); return $this; }
  5.         public          function isReference(Object $object)    { /* how to check this? */ }
  6.     final   public          function getHashCode()          { return $this->internalHash()->getHashCode(); }
  7.         public          function __construct()          { $this->setHashCode()->function_register(self::GC_COLLECT_CYCLES);}
  8.         public          function __destruct()           { $this->finalize(); }
  9.         private         function setHashCode()          { $this->__hash = new Hash(); return $this; }
  10.         private         function function_register($name)   { if(!isset(self::$__func_checked[$name])){ self::$__func_checked[$name] = (bool) function_exists($name); if(TRUE === self::$__func_checked[$name]) self::$__func_exists[$name] = self::$__func_checked[$name];} }
  11.         private         $__hash                 = NULL;
  12.         private     static  $__func_checked             = array();
  13.         private     static  $__func_exists              = array();
  14.         protected       function internalHash()         { return $this->__hash; }
  15. }
  16.  
  17. final class     Hash
  18. {
  19.         public          function getHashCode()          { return new Integer($this->__hashCode); }
  20.         public          function __construct()          { $this->__hashCode = mt_rand(); }
  21.         private         $__hashCode             = NULL;
  22. }
  23.  
  24.  
  25. interface   Type_Interface{}
  26.  
  27. final class Type
  28. extends     System
  29. implements  Type_Interface
  30. {
  31.         public          function isArray()          { return $this->__isArray; }
  32.         public          function isIterator()           { return $this->__isIterator; }
  33.         public          function isCountable()          { return $this->__isCountable; }
  34.         public          function isPrimitive()          { return $this->__isPrimitive; }
  35.         public          function getType()          { return $this->__type; }
  36.         public          function isLocked()         { return $this->__isLocked; }
  37.         public          function lock()             { $this->__isLocked = TRUE; return $this; }
  38.         public          function lockWithTrace()        { $this->lock()->__locktrace = debug_backtrace(); return $this; }
  39.         public          function isModifiable()         { if(TRUE === $this->isLocked()) throw new UnauthorizedAccessException(UnauthorizedAccessException::ERROR_LOCKED_FOR_MOD); return TRUE; }
  40.         public          function getLockTrace()         { return $this->__locktrace; }
  41.         public          function __construct(SystemObject $value)
  42.                     {
  43.                         parent::__construct();
  44.                         $this->__type           = get_class($value);
  45.                         $this->__isPrimitive        = isset(self::$__primitives[strtolower($this->__type)]);
  46.                         $this->__isArray        = ($value instanceOf ObjectArray);
  47.                         $this->__isCountable        = ($value instanceOf COUNTABLE);
  48.                         $this->__isIterator     = ($value instanceOf ITERATOR);
  49.                     }
  50.         private     static  $__primitives               = array('string' => TRUE, 'integer' => TRUE, 'boolean' => TRUE, 'float' => TRUE);
  51.         private         $__type                 = '';
  52.         private         $__isArray              = NULL;
  53.         private         $__isPrimitive              = NULL;
  54.         private         $__isCountable              = NULL;
  55.         private         $__isIterator               = NULL;
  56.         private         $__isLocked                 = FALSE;
  57.         private         $__locktrace                = NULL;
  58. }
  59.  
  60. interface Object_Interface
  61. {
  62.         public          function restore();
  63.         public          function apply();
  64. }
  65.  
  66. abstract class  SystemObject
  67. extends     System
  68. {
  69.         const           Blank                   = NULL;
  70.     final   public          function isArray()          { return new Boolean($this->internalTypeIsArray()); }
  71.     final   public          function isIterator()           { return new Boolean($this->internalTypeIsIterator()); }
  72.     final   public          function isCountable()          { return new Boolean($this->internalTypeIsCountable()); }
  73.     final   public          function isPrimitive()          { return new Boolean($this->internalTypeIsPrimitive()); }
  74.     final   public          function isModifiable()         { return new Boolean($this->internalTypeIsModifiable()); }
  75.     final   public          function getType()          { return new String($this->internalTypeGetType()); }
  76.     final   public          function lock()             { $this->internalType()->lock(); return $this; }
  77.     final   public          function lockWithTrace()        { $this->internalType()->lockWithTrace(); return $this; }
  78.     final   public          function toString()         { return var_export($this)."\n"; }
  79.         public          function __construct()          { parent::__construct(); $this->__type = new Type($this); }
  80.     final   private         function internalType()         { return $this->__type; }
  81.         private         $__type                 = NULL;
  82.     final   protected       function internalTypeIsArray()      { return $this->internalType()->isArray(); }
  83.     final   protected       function internalTypeIsIterator()   { return $this->internalType()->isIterator(); }
  84.     final   protected       function internalTypeIsCountable()  { return $this->internalType()->isCountable(); }
  85.     final   protected       function internalTypeIsPrimitive()  { return $this->internalType()->isPrimitive(); }
  86.     final   protected       function internalTypeIsModifiable() { return $this->internalType()->isModifiable(); }
  87.     final   protected       function internalTypeGetType()      { return $this->internalType()->getType(); }
  88.     final   protected       function _equals(SystemObject $object)  { return new Boolean($this === $object); }
  89. }
  90.  
  91. abstract class Object extends SystemObject implements Object_Interface
  92. {
  93.         const           Blank                   = NULL;
  94.     final   public          function copy()             { $copy = $this->getTemp(); $copy->set($this->__buffer); return $copy; }
  95.     final   public          function duplicate()            { $copy = $this->getTemp(); $copy->set($this->__buffer)->setCopy($this->__copy); return $copy; }
  96.     final   public          function get()              { return $this->__copy; }
  97.     final   public          function restore()          { $this->setCopy($this->__buffer); return $this; }
  98.     final   public          function registerMember(Hash $hash) { $this->__usedBy[] = $hash->getHashCode()->get(); }
  99.         public          function set($value)            { try{ if(FALSE === $this->internalTypeIsModifiable()) return $this; $this->__buffer = $value; return $this->setCopy($this->__buffer); } catch(UnauthorizedAccessException $e){throw new ObjectHandlerWriteException(ObjectHandlerWriteException::ERROR_CAN_NOT_WRITE, $e);} }
  100.     final   public          function apply()            { try{ if($this->internalTypeIsModifiable()) return $this->set($this->__copy); } catch(UnauthorizedAccessException $e){throw new ObjectHandlerWriteException(ObjectHandlerWriteException::ERROR_CAN_NOT_WRITE, $e);} }
  101.         public          function __construct($value)        { parent::__construct(); $this->set($value); }
  102.         private         $__copy                 = self::Blank;
  103.         private         $__buffer               = self::Blank;
  104.         private         $__usedBy               = array();
  105.     final   protected       function getTemp()          { $c = get_class($this); return new $c($this->get()); }
  106.     final   protected       function setCopy($value)        { $this->__copy = $value; return $this; }
  107.     final   protected       function _compareTo(Object $object) { return new Boolean($this->get() === $object->get()); }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement