All pastes #963099 Raw Edit

Anonymous

public text v1 · immutable
#963099 ·published 2008-03-30 06:12 UTC
rendered paste body
<?php


 //This file was written by Kendall Hopkins
 //API:
 //		
 //		This class allows you to store any type of variable as a name in the database which is then cached
 //		into a file for quick access. Name must be unique.
 //
 //		->getValue( $theName )					//to return value by name, returns NULL if doesn't exist
 //		->setValue( $theName, $theValue )	    //sets a value by name
 //		->unsetValue( $theName )				//delete a value by name, can fail if doesn't exist


class settings 
{
    public $settingsArray;
        
    private $base;
	private $cacheLocation;
    private $reCacheNeeded = FALSE;

    public function settings( &$base )
    {
        $this->base = &$base;
        $this->cacheLocation = getcwd()."/cache/settings.cache";
        $this->load();
    }

	public function getValue( $theName )
	{
		return $this->settingsArray[ $theName ];
	}
	
    public function setValue( $theName, $theValue )
    {
    	$theSerializeValue = serialize( $theValue );
    	
    	//try update first, if fail insert.
        if ( ! $this->base->query( "UPDATE ".$this->base->prefix."settings_test SET value = '".$theSerializeValue."' WHERE name = '".$theName."'" ) )
        {
        	$this->base->query( "INSERT INTO ".$this->base->prefix."settings_test (name, value) VALUES ( '".$theName."', '".$theSerializeValue."' )" );
        }
        
        $this->settingsArray[$theName] = $theValue;
        $this->reCacheNeeded = TRUE;
    }

    public function unsetValue( $theName )
    {
        $this->base->query( "DELETE FROM ".$this->base->prefix."settings_test WHERE name='".$theName."'" ) ;
        unset( $this->settingsArray[$theName] );
        $this->reCacheNeeded = TRUE;
    }

    private function load()
    {
        if ( $this->isCached() )
        {
            $this->loadCache();
        }
        else
        {
            $this->createCache();
        }
    }

    private function isCached()
    {
        return file_exists( $this->cacheLocation );
    }

    //loads itself and cache file w/ settings
    private function createCache()
    {
        $theFileHandle = fopen( $this->cacheLocation , "w" );
        if ( $theFileHandle && flock( $theFileHandle, LOCK_EX ) ) //if i can get a handle and a lock on file
        {
            $get_settings = $this->base->query( "SELECT name,value FROM ".$this->base->prefix."settings_test" );
            $this->settingsArray = array();
            while ( $theRow = $get_settings->fetch() )
            {
                $this->settingsArray[ $theRow['name'] ] = unserialize( $theRow['value'] );
            }
            $theData = serialize( $this->settingsArray );
            fwrite( $theFileHandle, $theData );
            flock ($theFileHandle, LOCK_UN);
            fclose( $theFileHandle );
        }
    }

    private function clearCache()
    {
        unlink ( $this->cacheLocation );
    }

    private function loadCache()
    {
        $theFileHandle = fopen( $this->cacheLocation , "r" );
        if ( $theFileHandle && filesize( $this->cacheLocation ) > 0 ) 
        {
        	$theData = fread( $theFileHandle, filesize( $this->cacheLocation ) );
        	$this->settingsArray = unserialize ( $theData );
        	fclose( $theFileHandle );
        }
        else
        {
        	$this->settingsArray = array(); // we need to set it to a empty array if we fail to load the cache
        }
    }
    
   	function __destruct() 
   	{
  		if ( $this->reCacheNeeded )
  		{
  			$this->clearCache();
  		}
   	}
}

?>