3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Memory\Container
;
13 use Zend\Memory\Exception
;
16 * Memory value container
18 * Movable (may be swapped with specified backend and unloaded).
20 class Movable
extends AbstractContainer
30 * Memory manager reference
32 * @var \Zend\Memory\MemoryManager
39 * @var \Zend\Memory\Value
49 * Value state (LOADED/SWAPPED/LOCKED)
58 * @param \Zend\Memory\MemoryManager $memoryManager
60 * @param string $value
62 public function __construct(Memory\MemoryManager
$memoryManager, $id, $value)
64 $this->memManager
= $memoryManager;
66 $this->state
= self
::LOADED
;
67 $this->value
= new Memory\
Value($value, $this);
71 * Lock object in memory.
73 public function lock()
75 if (!($this->state
& self
::LOADED
)) {
76 $this->memManager
->load($this, $this->id
);
77 $this->state |
= self
::LOADED
;
80 $this->state |
= self
::LOCKED
;
84 * It's possible to set "value" container attribute to avoid modification tracing, while it's locked
85 * Check, if it's more effective
92 public function unlock()
94 // Clear LOCKED state bit
95 $this->state
&= ~self
::LOCKED
;
99 * Return true if object is locked
103 public function isLocked()
105 return $this->state
& self
::LOCKED
;
111 * Loads object if necessary and moves it to the top of loaded objects list.
112 * Swaps objects from the bottom of loaded objects list, if necessary.
114 * @param string $property
116 * @throws Exception\InvalidArgumentException
118 public function __get($property)
120 if ($property != 'value') {
121 throw new Exception\
InvalidArgumentException('Unknown property: \Zend\Memory\Container\Movable::$' . $property);
124 if (!($this->state
& self
::LOADED
)) {
125 $this->memManager
->load($this, $this->id
);
126 $this->state |
= self
::LOADED
;
135 * @param string $property
136 * @param string $value
137 * @throws Exception\InvalidArgumentException
139 public function __set($property, $value)
141 if ($property != 'value') {
142 throw new Exception\
InvalidArgumentException('Unknown property: \Zend\Memory\Container\Movable::$' . $property);
145 $this->state
= self
::LOADED
;
146 $this->value
= new Memory\
Value($value, $this);
148 $this->memManager
->processUpdate($this, $this->id
);
153 * Get string value reference
155 * _Must_ be used for value access before PHP v 5.2
156 * or _may_ be used for performance considerations
160 public function &getRef()
162 if (!($this->state
& self
::LOADED
)) {
163 $this->memManager
->load($this, $this->id
);
164 $this->state |
= self
::LOADED
;
167 return $this->value
->getRef();
171 * Signal, that value is updated by external code.
173 * Should be used together with getRef()
175 public function touch()
177 $this->memManager
->processUpdate($this, $this->id
);
181 * Process container value update.
182 * Must be called only by value object
186 public function processUpdate()
188 // Clear SWAPPED state bit
189 $this->state
&= ~self
::SWAPPED
;
191 $this->memManager
->processUpdate($this, $this->id
);
195 * Start modifications trace
199 public function startTrace()
201 if (!($this->state
& self
::LOADED
)) {
202 $this->memManager
->load($this, $this->id
);
203 $this->state |
= self
::LOADED
;
206 $this->value
->startTrace();
210 * Set value (used by memory manager when value is loaded)
214 public function setValue($value)
216 $this->value
= new Memory\
Value($value, $this);
220 * Clear value (used by memory manager when value is swapped)
224 public function unloadValue()
226 // Clear LOADED state bit
227 $this->state
&= ~self
::LOADED
;
233 * Mark, that object is swapped
237 public function markAsSwapped()
239 // Clear LOADED state bit
240 $this->state |
= self
::LOADED
;
244 * Check if object is marked as swapped
249 public function isSwapped()
251 return $this->state
& self
::SWAPPED
;
260 public function getId()
265 * Destroy memory container and remove it from memory manager list
269 public function destroy()
272 * We don't clean up swap because of performance considerations
273 * Cleaning is performed by Memory Manager destructor
276 $this->memManager
->unlink($this, $this->id
);