EllisLab text mark
Advanced Search
     
Variable helper. Helps to save runtime configuration variables to file system.
Posted: 01 July 2007 02:53 PM   [ Ignore ]
Joined: 2007-03-18
8 posts

This is a alfa version. Not tested.

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Variable Helper
 *
 * @author        Inozemcev Andrew
 * @copyright    Copyright (c) 2007, Inozemcev Andrew.
 * @since        Version 1.0
 */

// ------------------------------------------------------------------------

/**
 * @subpackage    Helpers
 * @category    Helpers
 * @author        Inozemcev Andrew
 */

// ------------------------------------------------------------------------

/**
 * Variable Read
 *
 * Read array of variables from file system.
 *
 * @access    public
 * @return    array
 */

function &variable_read()
{
    
static $registered FALSE;
    static 
$variables;

    if(!
$registered)
    
{
        register_shutdown_function
('variable_write');
        
$registered TRUE;
    
}

    
if(is_array($variables))
    
{
        
return $variables;
    
}

    $variables 
= array();

    
$filepath APPPATH 'config/variables.cfg';

    if(!
file_exists($filepath))
    
{
        
return $variables;
    
}

    $size 
filesize($filepath);

    if(
$size === 0)
    
{
        
return $variables;
    
}

    
if($fp = @fopen($filepath'r'))
    
{
        flock
($fpLOCK_SH);
        
$variables unserialize(fread($fp$size));
        
flock($fpLOCK_UN);
        
fclose($fp);
    
}

    
return $variables;
}

// ------------------------------------------------------------------------

/**
 * Variable Write
 *
 * Write array of variables to file system.
 *
 * @access    public
 */

function variable_write()
{
    $variables 
=& variable_read();

    if(!isset(
$variables['access']))
    
{
        
return;
    
}

    $filepath 
APPPATH 'config/variables.cfg';

    
$mode file_exists($filepath) ? 'r+' 'w+';

    if(
$fp = @fopen($filepath$mode))
    
{
        flock
($fpLOCK_EX);

        
$size filesize($filepath);

        if(
$size 0)
        
{
            $original 
unserialize(fread($fp$size));
        
}

        
else
        
{
            $original 
= array();
        
}

        $current 
=& $variables['current'];

        foreach(
$variables['access'as $key => $val)
        
{
            
if($val === 'c')
            
{
                $original[$key] 
$current[$key];
            
}

            
elseif($val === 'd')
            
{
                
unset($original[$key]);
            
}
        }

        ftruncate
($fp0);
        
fseek($fp0);
        
fwrite($fpserialize($original));
        
flock($fpLOCK_UN);
        
fclose($fp);
    
}

    
@chmod($filename0666);
}

// ------------------------------------------------------------------------

/**
 * Variable Get
 *
 * Get variable.
 *
 * @access    public
 * @param    string
 * @param    mixed
 * @return    mixed
 */

function variable_get($name$default NULL)
{
    $variables 
=& variable_read();

    if(isset(
$variables['access'][$name]) AND $variables['access'][$name] === 'd')
    
{
        
return $default;
    
}

    
if(!isset($variables['current'][$name]))
    
{
        
return $default;
    
}

    
return $variables['current'][$name];
}

// ------------------------------------------------------------------------

/**
 * Variable Set
 *
 * Set variable.
 *
 * @access    public
 * @param    string
 * @param    mixed
 */

function variable_set($name$value)
{
    $variables 
=& variable_read();

    
$variables['current'][$name] $value;
    
$variables['access'][$name] 'c';
}

// ------------------------------------------------------------------------

/**
 * Variable Delete
 *
 * Delete variable.
 *
 * @access    public
 * @param    string
 */

function variable_delete($name)
{
    $variables 
=& variable_read();

    
$variables['access'][$name] 'd';
}

?>