eZ Publish  [trunk]
ezexpiryhandler.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZExpiryHandler class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /**
00012  * Keeps track of expiry keys and their timestamps
00013  * @class eZExpiryHandler ezexpiryhandler.php
00014  */
00015 class eZExpiryHandler
00016 {
00017     /**
00018      * Constructor
00019      */
00020     function eZExpiryHandler()
00021     {
00022         $this->Timestamps = array();
00023         $this->IsModified = false;
00024 
00025         $cacheDirectory = eZSys::cacheDirectory();
00026         $this->CacheFile = eZClusterFileHandler::instance( $cacheDirectory . '/' . 'expiry.php' );
00027         $this->restore();
00028     }
00029 
00030     /**
00031      * Load the expiry timestamps from cache
00032      *
00033      * @return void
00034      */
00035     function restore()
00036     {
00037         $Timestamps = $this->CacheFile->processFile( array( $this, 'fetchData' ) );
00038         $this->Timestamps = $Timestamps;
00039         $this->IsModified = false;
00040     }
00041 
00042     /**
00043      * Includes the expiry file and extracts the $Timestamps variable from it.
00044      * @param string $path
00045      */
00046     static function fetchData( $path )
00047     {
00048         include( $path );
00049         return $Timestamps;
00050     }
00051 
00052     /**
00053      * Stores the current timestamps values to cache
00054      */
00055     function store()
00056     {
00057         if ( $this->IsModified )
00058         {
00059             $this->CacheFile->storeContents( "<?php\n\$Timestamps = " . var_export( $this->Timestamps, true ) . ";\n?>", 'expirycache', false, true );
00060             $this->IsModified = false;
00061         }
00062     }
00063 
00064     /**
00065      * Sets the expiry timestamp for a key
00066      *
00067      * @param string $name Expiry key
00068      * @param int    $value Expiry timestamp value
00069      */
00070     function setTimestamp( $name, $value )
00071     {
00072         $this->Timestamps[$name] = $value;
00073         $this->IsModified = true;
00074     }
00075 
00076     /**
00077      * Checks if an expiry timestamp exist
00078      *
00079      * @param string $name Expiry key name
00080      *
00081      * @return bool true if the timestamp exists, false otherwise
00082      */
00083     function hasTimestamp( $name )
00084     {
00085         return isset( $this->Timestamps[$name] );
00086     }
00087 
00088     /**
00089      * Returns the expiry timestamp for a key
00090      *
00091      * @param string $name Expiry key
00092      *
00093      * @return int|false The timestamp if it exists, false otherwise
00094      */
00095     function timestamp( $name )
00096     {
00097         if ( !isset( $this->Timestamps[$name] ) )
00098         {
00099             eZDebug::writeError( "Unknown expiry timestamp called '$name'", __METHOD__ );
00100             return false;
00101         }
00102         return $this->Timestamps[$name];
00103     }
00104 
00105     /**
00106      * Returns the expiry timestamp for a key, or a default value if it isn't set
00107      *
00108      * @param string $name Expiry key name
00109      * @param int $default Default value that will be returned if the key isn't set
00110      *
00111      * @return mixed The expiry timestamp, or $default
00112      */
00113     static function getTimestamp( $name, $default = false )
00114     {
00115         $handler = eZExpiryHandler::instance();
00116         if ( !isset( $handler->Timestamps[$name] ) )
00117         {
00118             return $default;
00119         }
00120         return $handler->Timestamps[$name];
00121     }
00122 
00123     /**
00124      * Returns a shared instance of the eZExpiryHandler class
00125      *
00126      * @return eZExpiryHandler
00127      */
00128     static function instance()
00129     {
00130         if ( !isset( $GLOBALS['eZExpiryHandlerInstance'] ) ||
00131              !( $GLOBALS['eZExpiryHandlerInstance'] instanceof eZExpiryHandler ) )
00132         {
00133             $GLOBALS['eZExpiryHandlerInstance'] = new eZExpiryHandler();
00134         }
00135 
00136         return $GLOBALS['eZExpiryHandlerInstance'];
00137     }
00138 
00139     /**
00140      * Checks if a shared instance of eZExpiryHandler exists
00141      *
00142      * @return bool true if an instance exists, false otherwise
00143      */
00144     static function hasInstance()
00145     {
00146         return isset( $GLOBALS['eZExpiryHandlerInstance'] ) && $GLOBALS['eZExpiryHandlerInstance'] instanceof eZExpiryHandler;
00147     }
00148 
00149     /**
00150      * Called at the end of execution and will store the data if it is modified.
00151      */
00152     static function shutdown()
00153     {
00154         if ( eZExpiryHandler::hasInstance() )
00155         {
00156             eZExpiryHandler::instance()->store();
00157         }
00158     }
00159 
00160     /**
00161      * Registers the shutdown function.
00162      * @see eZExpiryHandler::shutdown()
00163      */
00164     public static function registerShutdownFunction(){
00165         if ( !eZExpiryHandler::$isShutdownFunctionRegistered ) {
00166             register_shutdown_function( array('eZExpiryHandler', 'shutdown') );
00167             eZExpiryHandler::$isShutdownFunctionRegistered = true;
00168         }
00169     }
00170 
00171     /**
00172      * Returns the data modification status
00173      *
00174      * @return bool true if data was modified, false if it wasn't
00175      * @deprecated 4.2 will be removed in 4.3
00176      */
00177     public function isModified()
00178     {
00179         return $this->IsModified;
00180     }
00181 
00182     /**
00183      * Indicates if thre shutdown function has been registered
00184      * @var bool
00185      */
00186     private static $isShutdownFunctionRegistered = false;
00187 
00188     /**
00189      * Holds the expiry timestamps array
00190      * @var array
00191      */
00192     public $Timestamps;
00193 
00194     /**
00195      * Wether data has been modified or not
00196      * @var bool
00197      */
00198     public $IsModified;
00199 }
00200 
00201 ?>