File indexing completed on 2024-12-22 05:36:52

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Memory
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /** Zend_Memory_Container */
0023 // require_once 'Zend/Memory/Container.php';
0024 
0025 /** Zend_Memory_Value */
0026 // require_once 'Zend/Memory/Value.php';
0027 
0028 /**
0029  * Memory value container
0030  *
0031  * Movable (may be swapped with specified backend and unloaded).
0032  *
0033  * @category   Zend
0034  * @package    Zend_Memory
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Memory_Container_Movable extends Zend_Memory_Container {
0039     /**
0040      * Internal object Id
0041      *
0042      * @var integer
0043      */
0044     protected $_id;
0045 
0046     /**
0047      * Memory manager reference
0048      *
0049      * @var Zend_Memory_Manager
0050      */
0051     private $_memManager;
0052 
0053     /**
0054      * Value object
0055      *
0056      * @var Zend_Memory_Value
0057      */
0058     private $_value;
0059 
0060     /** Value states */
0061     const LOADED   = 1;
0062     const SWAPPED  = 2;
0063     const LOCKED   = 4;
0064 
0065     /**
0066      * Value state (LOADED/SWAPPED/LOCKED)
0067      *
0068      * @var integer
0069      */
0070     private $_state;
0071 
0072     /**
0073      * Object constructor
0074      *
0075      * @param Zend_Memory_Manager $memoryManager
0076      * @param integer $id
0077      * @param string $value
0078      */
0079     public function __construct(Zend_Memory_Manager $memoryManager, $id, $value)
0080     {
0081         $this->_memManager = $memoryManager;
0082         $this->_id    = $id;
0083         $this->_state = self::LOADED;
0084         $this->_value = new Zend_Memory_Value($value, $this);
0085     }
0086 
0087     /**
0088      * Lock object in memory.
0089      */
0090     public function lock()
0091     {
0092         if ( !($this->_state & self::LOADED) ) {
0093             $this->_memManager->load($this, $this->_id);
0094             $this->_state |= self::LOADED;
0095         }
0096 
0097         $this->_state |= self::LOCKED;
0098 
0099         /**
0100          * @todo
0101          * It's possible to set "value" container attribute to avoid modification tracing, while it's locked
0102          * Check, if it's  more effective
0103          */
0104     }
0105 
0106     /**
0107      * Unlock object
0108      */
0109     public function unlock()
0110     {
0111         // Clear LOCKED state bit
0112         $this->_state &= ~self::LOCKED;
0113     }
0114 
0115     /**
0116      * Return true if object is locked
0117      *
0118      * @return boolean
0119      */
0120     public function isLocked()
0121     {
0122         return $this->_state & self::LOCKED;
0123     }
0124 
0125     /**
0126      * Get handler
0127      *
0128      * Loads object if necessary and moves it to the top of loaded objects list.
0129      * Swaps objects from the bottom of loaded objects list, if necessary.
0130      *
0131      * @param string $property
0132      * @return string
0133      * @throws Zend_Memory_Exception
0134      */
0135     public function __get($property)
0136     {
0137         if ($property != 'value') {
0138             // require_once 'Zend/Memory/Exception.php';
0139             throw new Zend_Memory_Exception('Unknown property: Zend_Memory_container::$' . $property);
0140         }
0141 
0142         if ( !($this->_state & self::LOADED) ) {
0143             $this->_memManager->load($this, $this->_id);
0144             $this->_state |= self::LOADED;
0145         }
0146 
0147         return $this->_value;
0148     }
0149 
0150     /**
0151      * Set handler
0152      *
0153      * @param string $property
0154      * @param  string $value
0155      * @throws Zend_Exception
0156      */
0157     public function __set($property, $value)
0158     {
0159         if ($property != 'value') {
0160             // require_once 'Zend/Memory/Exception.php';
0161             throw new Zend_Memory_Exception('Unknown property: Zend_Memory_container::$' . $property);
0162         }
0163 
0164         $this->_state = self::LOADED;
0165         $this->_value = new Zend_Memory_Value($value, $this);
0166 
0167         $this->_memManager->processUpdate($this, $this->_id);
0168     }
0169 
0170 
0171     /**
0172      * Get string value reference
0173      *
0174      * _Must_ be used for value access before PHP v 5.2
0175      * or _may_ be used for performance considerations
0176      *
0177      * @return &string
0178      */
0179     public function &getRef()
0180     {
0181         if ( !($this->_state & self::LOADED) ) {
0182             $this->_memManager->load($this, $this->_id);
0183             $this->_state |= self::LOADED;
0184         }
0185 
0186         return $this->_value->getRef();
0187     }
0188 
0189     /**
0190      * Signal, that value is updated by external code.
0191      *
0192      * Should be used together with getRef()
0193      */
0194     public function touch()
0195     {
0196         $this->_memManager->processUpdate($this, $this->_id);
0197     }
0198 
0199     /**
0200      * Process container value update.
0201      * Must be called only by value object
0202      *
0203      * @internal
0204      */
0205     public function processUpdate()
0206     {
0207         // Clear SWAPPED state bit
0208         $this->_state &= ~self::SWAPPED;
0209 
0210         $this->_memManager->processUpdate($this, $this->_id);
0211     }
0212 
0213     /**
0214      * Start modifications trace
0215      *
0216      * @internal
0217      */
0218     public function startTrace()
0219     {
0220         if ( !($this->_state & self::LOADED) ) {
0221             $this->_memManager->load($this, $this->_id);
0222             $this->_state |= self::LOADED;
0223         }
0224 
0225         $this->_value->startTrace();
0226     }
0227 
0228     /**
0229      * Set value (used by memory manager when value is loaded)
0230      *
0231      * @internal
0232      */
0233     public function setValue($value)
0234     {
0235         $this->_value = new Zend_Memory_Value($value, $this);
0236     }
0237 
0238     /**
0239      * Clear value (used by memory manager when value is swapped)
0240      *
0241      * @internal
0242      */
0243     public function unloadValue()
0244     {
0245         // Clear LOADED state bit
0246         $this->_state &= ~self::LOADED;
0247 
0248         $this->_value = null;
0249     }
0250 
0251     /**
0252      * Mark, that object is swapped
0253      *
0254      * @internal
0255      */
0256     public function markAsSwapped()
0257     {
0258         // Clear LOADED state bit
0259         $this->_state |= self::LOADED;
0260     }
0261 
0262     /**
0263      * Check if object is marked as swapped
0264      *
0265      * @internal
0266      * @return boolean
0267      */
0268     public function isSwapped()
0269     {
0270         return $this->_state & self::SWAPPED;
0271     }
0272 
0273     /**
0274      * Get object id
0275      *
0276      * @internal
0277      * @return integer
0278      */
0279     public function getId()
0280     {
0281         return $this->_id;
0282     }
0283     /**
0284      * Destroy memory container and remove it from memory manager list
0285      *
0286      * @internal
0287      */
0288     public function destroy()
0289     {
0290         /**
0291          * We don't clean up swap because of performance considerations
0292          * Cleaning is performed by Memory Manager destructor
0293          */
0294 
0295         $this->_memManager->unlink($this, $this->_id);
0296     }
0297 }