1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: /**
15: * Simple object cache so we don't have to create them or write caching over again.
16: *
17: * Example:
18: * <code>
19: * $key = 'User_Friends/' . $user->username();
20: * return Jyxo_Spl_ObjectCache::get($key) ?: Jyxo_Spl_ObjectCache::set($key, new User_Friends($this->context, $user));
21: * </code>
22: *
23: * @category Jyxo
24: * @package Jyxo_Spl
25: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
26: * @license https://github.com/jyxo/php/blob/master/license.txt
27: * @author Jakub Tománek
28: */
29: class Jyxo_Spl_ObjectCache implements IteratorAggregate
30: {
31: /**
32: * Object storage.
33: *
34: * @var array
35: */
36: private $storage = array();
37:
38: /**
39: * Returns default storage for static access.
40: *
41: * @return Jyxo_Spl_ObjectCache
42: */
43: public static function getInstance()
44: {
45: static $instance = null;
46: if (null === $instance) {
47: $instance = new self();
48: }
49: return $instance;
50: }
51:
52: /**
53: * Returns an object from the default storage.
54: *
55: * @param string $key Object key
56: * @return object
57: */
58: public static function get($key)
59: {
60: return self::getInstance()->$key;
61: }
62:
63: /**
64: * Clear the whole storage.
65: *
66: * @return Jyxo_Spl_ObjectCache
67: */
68: public function clear()
69: {
70: $this->storage = array();
71: return $this;
72: }
73:
74: /**
75: * Saves an object into the default storage.
76: *
77: * @param string $key Object key
78: * @param object $value Object
79: * @return object saved object
80: */
81: public static function set($key, $value)
82: {
83: self::getInstance()->$key = $value;
84: return $value;
85: }
86:
87: /**
88: * Returns an object from an own storage.
89: *
90: * @param string $key Object key
91: * @return object
92: */
93: public function __get($key)
94: {
95: return isset($this->storage[$key]) ? $this->storage[$key] : null;
96: }
97:
98: /**
99: * Saves an object into an own storage.
100: *
101: * @param string $key Object key
102: * @param object $value Object
103: */
104: public function __set($key, $value)
105: {
106: $this->storage[$key] = $value;
107: }
108:
109: /**
110: * Returns if there's an object with key $key in the storage.
111: *
112: * @param string $key Object key
113: * @return boolean
114: */
115: public function __isset($key)
116: {
117: return isset($this->storage[$key]);
118: }
119:
120: /**
121: * Deletes an object with key $key from the storage.
122: *
123: * @param mixed $key Object key
124: */
125: public function __unset($key)
126: {
127: unset ($this->storage[$key]);
128: }
129:
130: /**
131: * Returns an iterator.
132: *
133: * @return ArrayIterator
134: */
135: public function getIterator()
136: {
137: return new ArrayIterator($this->storage);
138: }
139: }
140: