Movable.php 6.7 KB

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