Movable.php 6.8 KB

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