eZ Publish  [4.0]
ezexpiryhandler.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZExpiryHandler class
00004 //
00005 // Created on: <28-Feb-2003 16:52:53 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezexpiryhandler.php
00032 */
00033 
00034 /*!
00035   \class eZExpiryHandler ezexpiryhandler.php
00036   \brief Keeps track of expiry keys and their timestamps
00037 
00038 */
00039 
00040 ////include_once( 'lib/ezutils/classes/ezphpcreator.php' );
00041 
00042 class eZExpiryHandler
00043 {
00044     /*!
00045      Constructor
00046     */
00047     function eZExpiryHandler()
00048     {
00049         $this->Timestamps = array();
00050         $this->IsModified = false;
00051 
00052         $cacheDirectory = eZSys::cacheDirectory();
00053         require_once( 'kernel/classes/ezclusterfilehandler.php' );
00054         $this->CacheFile = eZClusterFileHandler::instance( $cacheDirectory . '/' . 'expiry.php' );
00055         $this->restore();
00056     }
00057 
00058     /*!
00059      Will load timestamp values from disk.
00060     */
00061     function restore()
00062     {
00063         $Timestamps = $this->CacheFile->processFile( array( $this, 'fetchData' ) );
00064         $this->Timestamps = $Timestamps;
00065         $this->IsModified = false;
00066     }
00067 
00068     /*!
00069      \static
00070      Includes the expiry file and extracts the $Timestamps variable from it.
00071      */
00072     static function fetchData( $path )
00073     {
00074         include( $path );
00075         return $Timestamps;
00076     }
00077 
00078     /*!
00079      Will store the current timestamp values to disk.
00080     */
00081     function store()
00082     {
00083         $cacheDirectory = eZSys::cacheDirectory();
00084 
00085         $storeString = "<?php\n\$Timestamps = array( ";
00086         $i = 0;
00087         foreach ( $this->Timestamps as $key => $value )
00088         {
00089             if ( $i > 0 )
00090                 $storeString .= ",\n" . str_repeat( ' ', 21 );
00091             $storeString .= "'$key' => $value";
00092             ++$i;
00093         }
00094         $storeString .= " );\n?>";
00095 
00096         $this->CacheFile->storeContents( $storeString, 'expirycache', false, true );
00097         $this->IsModified = false;
00098     }
00099 
00100     /*!
00101      Sets the timestamp value \a $value for expiry key \a $name.
00102     */
00103     function setTimestamp( $name, $value )
00104     {
00105         $this->Timestamps[$name] = $value;
00106         $this->IsModified = true;
00107     }
00108 
00109     /*!
00110      \return true if the expiry key \a $name exists.
00111     */
00112     function hasTimestamp( $name )
00113     {
00114         return isset( $this->Timestamps[$name] );
00115     }
00116 
00117     /*!
00118      \return the timestamp value for the expiry key \a $name if it exists or \c false if not,
00119     */
00120     function timestamp( $name )
00121     {
00122         if ( !isset( $this->Timestamps[$name] ) )
00123         {
00124             eZDebug::writeError( "Unknown expiry timestamp called '$name'", 'eZExpiryHandler::timestamp' );
00125             return false;
00126         }
00127         return $this->Timestamps[$name];
00128     }
00129 
00130     /*!
00131      \static
00132      \return the timestamp value for the expiry key \a $name if it exists or \c false if not,
00133     */
00134     static function getTimestamp( $name, $default = false )
00135     {
00136         $handler = eZExpiryHandler::instance();
00137         if ( !isset( $handler->Timestamps[$name] ) )
00138         {
00139             return $default;
00140         }
00141         return $handler->Timestamps[$name];
00142     }
00143 
00144     /*!
00145      \static
00146      \return the unique instance of the expiry handler.
00147     */
00148     static function instance()
00149     {
00150         if ( !isset( $GLOBALS['eZExpiryHandlerInstance'] ) ||
00151              !( $GLOBALS['eZExpiryHandlerInstance'] instanceof eZExpiryHandler ) )
00152         {
00153             $GLOBALS['eZExpiryHandlerInstance'] = new eZExpiryHandler();
00154         }
00155 
00156         return $GLOBALS['eZExpiryHandlerInstance'];
00157     }
00158 
00159     /*!
00160      \static
00161      \return true if there's a unique instance of the expiry handler, false otherwise.
00162     */
00163     static function hasInstance()
00164     {
00165         return isset( $GLOBALS['eZExpiryHandlerInstance'] ) && $GLOBALS['eZExpiryHandlerInstance'] instanceof eZExpiryHandler;
00166     }
00167 
00168     /*!
00169      \return true if the expiry handler has modified data.
00170     */
00171     function isModified()
00172     {
00173         return $this->IsModified;
00174     }
00175 
00176     /*!
00177      Called at the end of execution and will store the data if it is modified.
00178     */
00179     static function shutdown()
00180     {
00181         if ( eZExpiryHandler::hasInstance() )
00182         {
00183             $instance = eZExpiryHandler::instance();
00184             if ( $instance->isModified() )
00185             {
00186                 $instance->store();
00187             }
00188         }
00189     }
00190 
00191     /*!
00192      Registers shutdown function to be called at the end of script execution
00193     */
00194     public static function registerShutdownFunction(){
00195         if ( !eZExpiryHandler::$isShutdownFunctionRegistered ) {
00196             register_shutdown_function( 'eZExpiryHandler::shutdown' );
00197             eZExpiryHandler::$isShutdownFunctionRegistered = true;
00198         }
00199     }
00200 
00201     /// \privatesection
00202     private static $isShutdownFunctionRegistered = false;
00203 
00204     public $Timestamps;
00205     public $IsModified;
00206 }
00207 
00208 ?>