|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Memory |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Movable.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
20 */ |
|
21 |
|
22 /** Zend_Memory_Container */ |
|
23 require_once 'Zend/Memory/Container.php'; |
|
24 |
|
25 /** Zend_Memory_Value */ |
|
26 require_once 'Zend/Memory/Value.php'; |
|
27 |
|
28 /** |
|
29 * Memory value container |
|
30 * |
|
31 * Movable (may be swapped with specified backend and unloaded). |
|
32 * |
|
33 * @category Zend |
|
34 * @package Zend_Memory |
|
35 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
36 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
37 */ |
|
38 class Zend_Memory_Container_Movable extends Zend_Memory_Container { |
|
39 /** |
|
40 * Internal object Id |
|
41 * |
|
42 * @var integer |
|
43 */ |
|
44 protected $_id; |
|
45 |
|
46 /** |
|
47 * Memory manager reference |
|
48 * |
|
49 * @var Zend_Memory_Manager |
|
50 */ |
|
51 private $_memManager; |
|
52 |
|
53 /** |
|
54 * Value object |
|
55 * |
|
56 * @var Zend_Memory_Value |
|
57 */ |
|
58 private $_value; |
|
59 |
|
60 /** Value states */ |
|
61 const LOADED = 1; |
|
62 const SWAPPED = 2; |
|
63 const LOCKED = 4; |
|
64 |
|
65 /** |
|
66 * Value state (LOADED/SWAPPED/LOCKED) |
|
67 * |
|
68 * @var integer |
|
69 */ |
|
70 private $_state; |
|
71 |
|
72 /** |
|
73 * Object constructor |
|
74 * |
|
75 * @param Zend_Memory_Manager $memoryManager |
|
76 * @param integer $id |
|
77 * @param string $value |
|
78 */ |
|
79 public function __construct(Zend_Memory_Manager $memoryManager, $id, $value) |
|
80 { |
|
81 $this->_memManager = $memoryManager; |
|
82 $this->_id = $id; |
|
83 $this->_state = self::LOADED; |
|
84 $this->_value = new Zend_Memory_Value($value, $this); |
|
85 } |
|
86 |
|
87 /** |
|
88 * Lock object in memory. |
|
89 */ |
|
90 public function lock() |
|
91 { |
|
92 if ( !($this->_state & self::LOADED) ) { |
|
93 $this->_memManager->load($this, $this->_id); |
|
94 $this->_state |= self::LOADED; |
|
95 } |
|
96 |
|
97 $this->_state |= self::LOCKED; |
|
98 |
|
99 /** |
|
100 * @todo |
|
101 * It's possible to set "value" container attribute to avoid modification tracing, while it's locked |
|
102 * Check, if it's more effective |
|
103 */ |
|
104 } |
|
105 |
|
106 /** |
|
107 * Unlock object |
|
108 */ |
|
109 public function unlock() |
|
110 { |
|
111 // Clear LOCKED state bit |
|
112 $this->_state &= ~self::LOCKED; |
|
113 } |
|
114 |
|
115 /** |
|
116 * Return true if object is locked |
|
117 * |
|
118 * @return boolean |
|
119 */ |
|
120 public function isLocked() |
|
121 { |
|
122 return $this->_state & self::LOCKED; |
|
123 } |
|
124 |
|
125 /** |
|
126 * Get handler |
|
127 * |
|
128 * Loads object if necessary and moves it to the top of loaded objects list. |
|
129 * Swaps objects from the bottom of loaded objects list, if necessary. |
|
130 * |
|
131 * @param string $property |
|
132 * @return string |
|
133 * @throws Zend_Memory_Exception |
|
134 */ |
|
135 public function __get($property) |
|
136 { |
|
137 if ($property != 'value') { |
|
138 require_once 'Zend/Memory/Exception.php'; |
|
139 throw new Zend_Memory_Exception('Unknown property: Zend_Memory_container::$' . $property); |
|
140 } |
|
141 |
|
142 if ( !($this->_state & self::LOADED) ) { |
|
143 $this->_memManager->load($this, $this->_id); |
|
144 $this->_state |= self::LOADED; |
|
145 } |
|
146 |
|
147 return $this->_value; |
|
148 } |
|
149 |
|
150 /** |
|
151 * Set handler |
|
152 * |
|
153 * @param string $property |
|
154 * @param string $value |
|
155 * @throws Zend_Exception |
|
156 */ |
|
157 public function __set($property, $value) |
|
158 { |
|
159 if ($property != 'value') { |
|
160 require_once 'Zend/Memory/Exception.php'; |
|
161 throw new Zend_Memory_Exception('Unknown property: Zend_Memory_container::$' . $property); |
|
162 } |
|
163 |
|
164 $this->_state = self::LOADED; |
|
165 $this->_value = new Zend_Memory_Value($value, $this); |
|
166 |
|
167 $this->_memManager->processUpdate($this, $this->_id); |
|
168 } |
|
169 |
|
170 |
|
171 /** |
|
172 * Get string value reference |
|
173 * |
|
174 * _Must_ be used for value access before PHP v 5.2 |
|
175 * or _may_ be used for performance considerations |
|
176 * |
|
177 * @return &string |
|
178 */ |
|
179 public function &getRef() |
|
180 { |
|
181 if ( !($this->_state & self::LOADED) ) { |
|
182 $this->_memManager->load($this, $this->_id); |
|
183 $this->_state |= self::LOADED; |
|
184 } |
|
185 |
|
186 return $this->_value->getRef(); |
|
187 } |
|
188 |
|
189 /** |
|
190 * Signal, that value is updated by external code. |
|
191 * |
|
192 * Should be used together with getRef() |
|
193 */ |
|
194 public function touch() |
|
195 { |
|
196 $this->_memManager->processUpdate($this, $this->_id); |
|
197 } |
|
198 |
|
199 /** |
|
200 * Process container value update. |
|
201 * Must be called only by value object |
|
202 * |
|
203 * @internal |
|
204 */ |
|
205 public function processUpdate() |
|
206 { |
|
207 // Clear SWAPPED state bit |
|
208 $this->_state &= ~self::SWAPPED; |
|
209 |
|
210 $this->_memManager->processUpdate($this, $this->_id); |
|
211 } |
|
212 |
|
213 /** |
|
214 * Start modifications trace |
|
215 * |
|
216 * @internal |
|
217 */ |
|
218 public function startTrace() |
|
219 { |
|
220 if ( !($this->_state & self::LOADED) ) { |
|
221 $this->_memManager->load($this, $this->_id); |
|
222 $this->_state |= self::LOADED; |
|
223 } |
|
224 |
|
225 $this->_value->startTrace(); |
|
226 } |
|
227 |
|
228 /** |
|
229 * Set value (used by memory manager when value is loaded) |
|
230 * |
|
231 * @internal |
|
232 */ |
|
233 public function setValue($value) |
|
234 { |
|
235 $this->_value = new Zend_Memory_Value($value, $this); |
|
236 } |
|
237 |
|
238 /** |
|
239 * Clear value (used by memory manager when value is swapped) |
|
240 * |
|
241 * @internal |
|
242 */ |
|
243 public function unloadValue() |
|
244 { |
|
245 // Clear LOADED state bit |
|
246 $this->_state &= ~self::LOADED; |
|
247 |
|
248 $this->_value = null; |
|
249 } |
|
250 |
|
251 /** |
|
252 * Mark, that object is swapped |
|
253 * |
|
254 * @internal |
|
255 */ |
|
256 public function markAsSwapped() |
|
257 { |
|
258 // Clear LOADED state bit |
|
259 $this->_state |= self::LOADED; |
|
260 } |
|
261 |
|
262 /** |
|
263 * Check if object is marked as swapped |
|
264 * |
|
265 * @internal |
|
266 * @return boolean |
|
267 */ |
|
268 public function isSwapped() |
|
269 { |
|
270 return $this->_state & self::SWAPPED; |
|
271 } |
|
272 |
|
273 /** |
|
274 * Get object id |
|
275 * |
|
276 * @internal |
|
277 * @return integer |
|
278 */ |
|
279 public function getId() |
|
280 { |
|
281 return $this->_id; |
|
282 } |
|
283 /** |
|
284 * Destroy memory container and remove it from memory manager list |
|
285 * |
|
286 * @internal |
|
287 */ |
|
288 public function destroy() |
|
289 { |
|
290 /** |
|
291 * We don't clean up swap because of performance considerations |
|
292 * Cleaning is performed by Memory Manager destructor |
|
293 */ |
|
294 |
|
295 $this->_memManager->unlink($this, $this->_id); |
|
296 } |
|
297 } |