/** * Returns an associative array of all attributes of an object, * including those declared as protected or private. * * @param object $object The object for which all attributes are returned. * @return array * @throws InvalidArgumentException */ public static function readAttributes($object) { // Bail out if a non-object was passed. if (!is_object($object)) { throw Object_Freezer_Util::getInvalidArgumentException(1, 'object'); } $reflector = new ReflectionObject($object); $result = array(); // Iterate over the attributes of the object. foreach ($reflector->getProperties() as $attribute) { $attribute->setAccessible(TRUE); $result[$attribute->getName()] = $attribute->getValue($object); } return $result; }
/** * Checks whether an object is dirty, ie. if its SHA1 hash is still valid. * * Returns TRUE when the object's __php_object_freezer_hash attribute is no * longer valid or does not exist. * Returns FALSE when the object's __php_object_freezer_hash attribute is * still valid. * * @param object $object The object that is to be checked. * @param bool $rehash Whether or not to rehash dirty objects. * * @return bool * * @throws InvalidArgumentException */ public function isDirty($object, $rehash = false) { // Bail out if a non-object was passed. if (!is_object($object)) { throw Object_Freezer_Util::getInvalidArgumentException(1, 'object'); } // Bail out if a non-boolean was passed. if (!is_bool($rehash)) { throw Object_Freezer_Util::getInvalidArgumentException(2, 'boolean'); } $isDirty = true; $hash = $this->hashGenerator->getHash($object); if (isset($object->__php_object_freezer_hash) && $object->__php_object_freezer_hash == $hash) { $isDirty = false; } if ($isDirty && $rehash) { $object->__php_object_freezer_hash = $hash; } return $isDirty; }
/** * Constructor. * * @param string $database * Name of the database to be used * @param Object_Freezer $freezer * Object_Freezer instance to be used * @param Object_Freezer_Cache $cache * Object_Freezer_Cache instance to be used * @param boolean $useLazyLoad * Flag that controls whether objects are * fetched using lazy load or not * @param string $host * Hostname of the CouchDB instance to be used * @param int $port * Port of the CouchDB instance to be used * @throws InvalidArgumentException */ public function __construct($database, Object_Freezer $freezer = NULL, Object_Freezer_Cache $cache = NULL, $useLazyLoad = FALSE, $host = 'localhost', $port = 5984) { parent::__construct($freezer, $cache, $useLazyLoad); // Bail out if a non-string was passed. if (!is_string($database)) { throw Object_Freezer_Util::getInvalidArgumentException(1, 'string'); } // Bail out if a non-string was passed. if (!is_string($host)) { throw Object_Freezer_Util::getInvalidArgumentException(4, 'string'); } // Bail out if a non-integer was passed. if (!is_int($port)) { throw Object_Freezer_Util::getInvalidArgumentException(5, 'integer'); } $this->database = $database; $this->host = $host; $this->port = $port; }
/** * Implementation of Object_Freezer_HashGenerator that uses the SHA1 * hashing function on the attribute values of an object without recursing * into aggregated arrays or objects. * * @param object $object The object that is to be hashed. * * @return string * * @throws InvalidArgumentException */ public function getHash($object) { // Bail out if a non-object was passed. if (!is_object($object)) { throw Object_Freezer_Util::getInvalidArgumentException(1, 'object'); } $attributes = Object_Freezer_Util::readAttributes($object); ksort($attributes); if (isset($attributes['__php_object_freezer_hash'])) { unset($attributes['__php_object_freezer_hash']); } foreach ($attributes as $key => $value) { if (is_array($value)) { $attributes[$key] = '<array>'; } elseif (is_object($value)) { if (!isset($value->__php_object_freezer_uuid)) { $value->__php_object_freezer_uuid = $this->idGenerator->getId(); } $attributes[$key] = $value->__php_object_freezer_uuid; } elseif (is_resource($value)) { $attributes[$key] = null; } } return sha1(get_class($object) . implode(':', $attributes)); }
/** * Sets the flag that controls whether or not debug messages are printed. * * @param bool $flag * * @throws InvalidArgumentException */ public function setDebug($flag) { // Bail out if a non-boolean was passed. if (!is_bool($flag)) { throw Object_Freezer_Util::getInvalidArgumentException(1, 'boolean'); } $this->debug = $flag; }
/** * Fetches a frozen object from the object storage and thaws it. * * @param string $id The ID of the object that is to be fetched. * * @return object */ public function fetch($id) { // Bail out if a non-string was passed. if (!is_string($id)) { throw Object_Freezer_Util::getInvalidArgumentException(1, 'string'); } // Try to retrieve object from the object cache. $object = $this->cache->get($id); if (!$object) { // Retrieve object from the object storage. $frozenObject = $this->doFetch($id); $this->fetchArray($frozenObject['objects'][$id]['state']); $object = $this->freezer->thaw($frozenObject); // Put object into the object cache. $this->cache->put($id, $object); } return $object; }
/** * @covers Object_Freezer_Util::readAttributes * @covers Object_Freezer_Util::getInvalidArgumentException * @expectedException InvalidArgumentException */ public function testExceptionIsThrownIfNotAnObjectIsPassedToReadAttributes() { Object_Freezer_Util::readAttributes(NULL); }