* @package generis */ abstract class common_cache_PartitionedCachable implements common_Serializable { // --- ASSOCIATIONS --- // --- ATTRIBUTES --- /** * Short description of attribute serial * * @access protected * @var string */ protected $serial = ''; /** * Short description of attribute serializedProperties * * @access protected * @var array */ protected $serializedProperties = []; // --- OPERATIONS --- /** * Obtain a serial for the instance of the class that implements the * * @access public * @author Jerome Bogaerts, * @return string */ public function getSerial() { $returnValue = (string) ''; if (empty($this->serial)) { $this->serial = $this->buildSerial(); } $returnValue = $this->serial; return (string) $returnValue; } /** * Short description of method __construct * * @access public * @author Jerome Bogaerts, * @return mixed */ public function __construct() { if (!is_null($this->getCache())) { $this->getCache()->put($this); } } /** * Gives the list of attributes to serialize by reflection. * * @access public * @author Jerome Bogaerts, * @return array */ public function __sleep() { $returnValue = []; $this->serializedProperties = []; $reflection = new ReflectionClass($this); foreach ($reflection->getProperties() as $property) { //assuming that private properties don't contain serializables if (!$property->isStatic() && !$property->isPrivate()) { $propertyName = $property->getName(); $containsSerializable = false; $value = $this->$propertyName; if (is_array($value)) { $containsNonSerializable = false; $serials = []; foreach ($value as $key => $subvalue) { if (is_object($subvalue) && $subvalue instanceof self) { $containsSerializable = true; $serials[$key] = $subvalue->getSerial(); } else { $containsNonSerializable = true; } } if ($containsNonSerializable && $containsSerializable) { throw new common_exception_Error('Serializable ' . $this->getSerial() . ' mixed serializable and non serializable values in property ' . $propertyName); } } else { if (is_object($value) && $value instanceof self) { $containsSerializable = true; $serials = $value->getSerial(); } } if ($containsSerializable) { $this->serializedProperties[$property->getName()] = $serials; } else { $returnValue[] = $property->getName(); } } } return (array) $returnValue; } /** * Short description of method __wakeup * * @access public * @author Jerome Bogaerts, * @return mixed */ public function __wakeup() { foreach ($this->serializedProperties as $key => $value) { if (is_array($value)) { $restored = []; foreach ($value as $arrayKey => $arrayValue) { $restored[$arrayKey] = $this->getCache()->get($arrayValue); } } else { $restored = $this->getCache()->get($value); } $this->$key = $restored; } $this->serializedProperties = []; } /** * Short description of method _remove * * @access public * @author Jerome Bogaerts, * @return mixed */ public function _remove() { //usefull only when persistance is enabled if (!is_null($this->getCache())) { //clean session $this->getCache()->remove($this->getSerial()); } } /** * Short description of method getSuccessors * * @access public * @author Jerome Bogaerts, * @return array */ public function getSuccessors() { $returnValue = []; $reflection = new ReflectionClass($this); foreach ($reflection->getProperties() as $property) { if (!$property->isStatic() && !$property->isPrivate()) { $propertyName = $property->getName(); $value = $this->$propertyName; if (is_array($value)) { foreach ($value as $key => $subvalue) { if (is_object($subvalue) && $subvalue instanceof self) { $returnValue[] = $subvalue; } } } elseif (is_object($value) && $value instanceof self) { $returnValue[] = $value; } } } return (array) $returnValue; } /** * Short description of method getPredecessors * * @access public * @author Jerome Bogaerts, * @param string classFilter * @return array */ public function getPredecessors($classFilter = null) { $returnValue = []; foreach ($this->getCache()->getAll() as $serial => $instance) { if ( ($classFilter == null || $instance instanceof $classFilter) && in_array($this, $instance->getSuccessors()) ) { $returnValue[] = $instance; break; } } return (array) $returnValue; } /** * Short description of method buildSerial * * @abstract * @access protected * @author Jerome Bogaerts, * @return string */ abstract protected function buildSerial(); /** * Short description of method getCache * * @abstract * @access public * @author Jerome Bogaerts, * @return common_cache_Cache */ abstract public function getCache(); } /* end of abstract class common_cache_PartitionedCachable */