00001 <?php
00044 require_once 'Object/Freezer.php';
00045 require_once 'Object/Freezer/Cache.php';
00046 require_once 'Object/Freezer/LazyProxy.php';
00047
00059 abstract class Object_Freezer_Storage
00060 {
00064 protected $cache;
00065
00069 protected $freezer;
00070
00074 protected $lazyLoad = FALSE;
00075
00083 public function __construct(Object_Freezer $freezer = NULL, Object_Freezer_Cache $cache = NULL, $useLazyLoad = FALSE)
00084 {
00085 if ($freezer === NULL) {
00086 $freezer = new Object_Freezer;
00087 }
00088
00089 if ($cache === NULL) {
00090 $cache = new Object_Freezer_Cache;
00091 }
00092
00093 $this->freezer = $freezer;
00094 $this->cache = $cache;
00095
00096 $this->setUseLazyLoad($useLazyLoad);
00097 }
00098
00105 public function setUseLazyLoad($flag)
00106 {
00107
00108 if (!is_bool($flag)) {
00109 throw Object_Freezer_Util::getInvalidArgumentException(1, 'boolean');
00110 }
00111
00112 $this->lazyLoad = $flag;
00113 }
00114
00121 public function store($object)
00122 {
00123
00124 if (!is_object($object)) {
00125 throw Object_Freezer_Util::getInvalidArgumentException(1, 'object');
00126 }
00127
00128 $this->doStore($this->freezer->freeze($object));
00129
00130 return $object->__php_object_freezer_uuid;
00131 }
00132
00139 public function fetch($id)
00140 {
00141
00142 if (!is_string($id)) {
00143 throw Object_Freezer_Util::getInvalidArgumentException(1, 'string');
00144 }
00145
00146
00147 $object = $this->cache->get($id);
00148
00149 if (!$object) {
00150
00151 $frozenObject = $this->doFetch($id);
00152 $this->fetchArray($frozenObject['objects'][$id]['state']);
00153 $object = $this->freezer->thaw($frozenObject);
00154
00155
00156 $this->cache->put($id, $object);
00157 }
00158
00159 return $object;
00160 }
00161
00168 protected function fetchArray(array &$array, array &$objects = array())
00169 {
00170 foreach ($array as &$value) {
00171 if (is_array($value)) {
00172 $this->fetchArray($value, $objects);
00173 }
00174
00175 else if (is_string($value) && strpos($value, '__php_object_freezer_') === 0) {
00176 $uuid = str_replace('__php_object_freezer_', '', $value);
00177
00178 if (!$this->lazyLoad) {
00179 $this->doFetch($uuid, $objects);
00180 } else {
00181 $value = new Object_Freezer_LazyProxy($this, $uuid);
00182 }
00183 }
00184 }
00185 }
00186
00192 abstract protected function doStore(array $frozenObject);
00193
00200 abstract protected function doFetch($id);
00201 }