00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Varien_Object_Cache
00036 {
00037
00038
00039
00040
00041
00042 protected static $_instance;
00043
00044
00045
00046
00047
00048
00049 protected $_idx = 0;
00050
00051
00052
00053
00054
00055
00056 protected $_objects = array();
00057
00058
00059
00060
00061
00062
00063 protected $_hashes = array();
00064
00065
00066
00067
00068
00069
00070 protected $_objectHashes = array();
00071
00072
00073
00074
00075
00076
00077 protected $_tags = array();
00078
00079
00080
00081
00082
00083
00084 protected $_objectTags = array();
00085
00086
00087
00088
00089
00090
00091 protected $_references = array();
00092
00093
00094
00095
00096
00097
00098 protected $_objectReferences = array();
00099
00100
00101
00102
00103
00104
00105 protected $_debug = array();
00106
00107
00108
00109
00110
00111
00112 public static function singleton()
00113 {
00114 if (!self::$_instance) {
00115 self::$_instance = new self();
00116 }
00117 return self::$_instance;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127 public function load($idx, $default=null)
00128 {
00129 if (isset($this->_references[$idx])) {
00130 $idx = $this->_references[$idx];
00131 }
00132 if (isset($this->_objects[$idx])) {
00133 return $this->_objects[$idx];
00134 }
00135 return $default;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 public function save($object, $idx=null, $tags=null)
00147 {
00148
00149 if (!is_object($object)) {
00150 return false;
00151 }
00152
00153 $hash = spl_object_hash($object);
00154 if (!is_null($idx) && strpos($idx, '{')) {
00155 $idx = str_replace('{hash}', $hash, $idx);
00156 }
00157 if (isset($this->_hashes[$hash])) {
00158
00159 if (!is_null($idx)) {
00160 $this->_references[$idx] = $this->_hashes[$hash];
00161 }
00162 return $this->_hashes[$hash];
00163 }
00164
00165 if (is_null($idx)) {
00166 $idx = '#'.(++$this->_idx);
00167 }
00168
00169 if (isset($this->_objects[$idx])) {
00170 throw new Varien_Exception('Object already exists in registry ('.$idx.'). Old object class: '.get_class($this->_objects[$idx]).', new object class: '.get_class($object));
00171 }
00172
00173 $this->_objects[$idx] = $object;
00174
00175 $this->_hashes[$hash] = $idx;
00176 $this->_objectHashes[$idx] = $hash;
00177
00178 if (is_string($tags)) {
00179 $this->_tags[$tags][$idx] = true;
00180 $this->_objectTags[$idx][$tags] = true;
00181 } elseif (is_array($tags)) {
00182 foreach ($tags as $t) {
00183 $this->_tags[$t][$idx] = true;
00184 $this->_objectTags[$idx][$t] = true;
00185 }
00186 }
00187
00188
00189 return $idx;
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199 public function reference($refName, $idx)
00200 {
00201 if (is_array($refName)) {
00202 foreach ($refName as $ref) {
00203 $this->reference($ref, $idx);
00204 }
00205 return;
00206 }
00207
00208 if (isset($this->_references[$refName])) {
00209 throw new Varien_Exception('The reference already exists: '.$refName.'. New index: '.$idx.', old index: '.$this->_references[$refName]);
00210 }
00211 $this->_references[$refName] = $idx;
00212 $this->_objectReferences[$idx][$refName] = true;
00213
00214 return true;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223 public function delete($idx)
00224 {
00225
00226 if (is_object($idx)) {
00227 $idx = $this->find($idx);
00228 if (false===$idx) {
00229
00230 return false;
00231 }
00232 unset($this->_objects[$idx]);
00233
00234 return false;
00235 } elseif (!isset($this->_objects[$idx])) {
00236
00237 return false;
00238 }
00239
00240 unset($this->_objects[$idx]);
00241
00242 unset($this->_hashes[$this->_objectHashes[$idx]], $this->_objectHashes[$idx]);
00243
00244 if (isset($this->_objectTags[$idx])) {
00245 foreach ($this->_objectTags[$idx] as $t=>$dummy) {
00246 unset($this->_tags[$t][$idx]);
00247 }
00248 unset($this->_objectTags[$idx]);
00249 }
00250
00251 if (isset($this->_objectReferences[$idx])) {
00252 foreach ($references as $r=>$dummy) {
00253 unset($this->_references[$r]);
00254 }
00255 unset($this->_objectReferences[$idx]);
00256 }
00257
00258
00259 return true;
00260 }
00261
00262
00263
00264
00265
00266
00267 public function deleteByClass($class)
00268 {
00269 foreach ($this->_objects as $idx=>$object) {
00270 if ($object instanceof $class) {
00271 $this->delete($idx);
00272 }
00273 }
00274 }
00275
00276
00277
00278
00279
00280
00281 public function deleteByTags($tags)
00282 {
00283 if (is_string($tags)) {
00284 $tags = array($tags);
00285 }
00286 foreach ($tags as $t) {
00287 foreach ($this->_tags[$t] as $idx=>$dummy) {
00288 $this->delete($idx);
00289 }
00290 }
00291 return true;
00292 }
00293
00294
00295
00296
00297
00298
00299
00300 public function has($idx)
00301 {
00302 return isset($this->_objects[$idx]) || isset($this->_references[$idx]);
00303 }
00304
00305
00306
00307
00308
00309
00310
00311 public function find($object)
00312 {
00313 foreach ($this->_objects as $idx=>$obj) {
00314 if ($object===$obj) {
00315 return $idx;
00316 }
00317 }
00318 return false;
00319 }
00320
00321 public function findByIds($ids)
00322 {
00323 $objects = array();
00324 foreach ($this->_objects as $idx=>$obj) {
00325 if (in_array($idx, $ids)) {
00326 $objects[$idx] = $obj;
00327 }
00328 }
00329 return $objects;
00330 }
00331
00332 public function findByHash($hash)
00333 {
00334 return isset($this->_hashes[$hash]) ? $this->_objects[$this->_hashes[$hash]] : null;
00335 }
00336
00337
00338
00339
00340
00341
00342
00343 public function findByTags($tags)
00344 {
00345 if (is_string($tags)) {
00346 $tags = array($tags);
00347 }
00348 $objects = array();
00349 foreach ($tags as $t) {
00350 foreach ($this->_tags[$t] as $idx=>$dummy) {
00351 if (isset($objects[$idx])) {
00352 continue;
00353 }
00354 $objects[$ids] = $this->load($idx);
00355 }
00356 }
00357 return $objects;
00358 }
00359
00360
00361
00362
00363
00364
00365 public function findByClass($class)
00366 {
00367 $objects = array();
00368 foreach ($this->_objects as $idx=>$object) {
00369 if ($object instanceof $class) {
00370 $objects[$idx] = $object;
00371 }
00372 }
00373 return $objects;
00374 }
00375
00376 public function debug($idx, $object=null)
00377 {
00378 $bt = debug_backtrace();
00379 $debug = array();
00380 foreach ($bt as $i=>$step) {
00381 $debug[$i] = array(
00382 'file' => isset($step['file']) ? $step['file'] : null,
00383 'line' => isset($step['line']) ? $step['line'] : null,
00384 'function' => isset($step['function']) ? $step['function'] : null,
00385 );
00386 }
00387 $this->_debug[$idx] = $debug;
00388 }
00389
00390
00391
00392
00393
00394
00395
00396 public function debugByIds($ids)
00397 {
00398 if (is_string($ids)) {
00399 $ids = array($ids);
00400 }
00401 $debug = array();
00402 foreach ($ids as $idx) {
00403 $debug[$idx] = $this->_debug[$idx];
00404 }
00405 return $debug;
00406 }
00407
00408
00409
00410
00411
00412
00413 public function getAllObjects()
00414 {
00415 return $this->_objects;
00416 }
00417
00418
00419
00420
00421
00422
00423 public function getAllTags()
00424 {
00425 return $this->_tags;
00426 }
00427
00428
00429
00430
00431
00432
00433 public function getAllTagsByObject()
00434 {
00435 return $this->_objectTags;
00436 }
00437
00438
00439
00440
00441
00442
00443 public function getAllReferences()
00444 {
00445 return $this->_references;
00446 }
00447
00448
00449
00450
00451
00452
00453 public function getAllReferencesByObject()
00454 {
00455 return $this->_referencesByObject;
00456 }
00457 }