Location: PHPKode > scripts > Storage > storage.php
<?php
/**
 *  @package storage
 *  @author Domenico Pontari <hide@address.com>
 *  @copyright Copyright (c) 2009, Domenico Pontari
 *  @license http://opensource.org/licenses/bsd-license.php New and Simplified BSD licenses
 *  @version 1.2
 *
 *  This class can be used to save variables in a directory of your filesystem you can
 *  extend this class defining 'prepareToBeStored' and 'retrieveFromStorage' methods
 *  to have further elaborations
 */

/**
 *  @package storage
 *  If a 'prepareToBeStored' or a 'retrieveFromStorage' method exists, it will be
 *  called
 */
class storage {
    /**
     *  @var string directory without trailing slashes where you want to store your objects
     */
    protected $path;

    protected $objs = array();

    /**
     *  Fake function: it can be used extending this class for futher elaborations
     */
    function prepareToBeStored ($data) {return $data;}

    /**
     *  Fake function: it can be used extending this class for futher elaborations
     */
    function retrieveFromStorage ($data) {return $data;}

    function __construct($path = '') {
        if (!empty($path))  $this->setPath($path);
    }

    function setPath ($path) {
        $this->path = $path;
        $this->objs = self::readObjNamesFromPath($this->path);
    }

    /**
     *  @param string
     *  @param bool if true data will be always retrived from the filesystem
     *  @return object
     */
    function getObj ($name, $forceFromFS = false) {
        if (!isset($this->path)) trigger_error ('storagePathNotSetted', E_USER_ERROR);
        if (!array_key_exists($name, $this->objs)) trigger_error ('objectNameNotFound', E_USER_ERROR);
        if ($forceFromFS || ($this->objs[$name] === NULL)) {
            $filename = self::name2FS ($name);
            $content =@ file_get_contents("$this->path/$filename");
            if ($content === false) trigger_error ('unableToReadOjbectFile', E_USER_ERROR);
            if (method_exists($this, 'retrieveFromStorage')) $content = $this->retrieveFromStorage($content);
            $this->objs[$name] = $content;
        }
        return $this->objs[$name];
    }
    
    /**
     *  @param string
     *  @param mixed
     *  @param bool if false data won't be saved on the HD just memorized as simple variable
     */
    function setObj ($name, $obj, $saveInFS = true) {
        if (!isset($this->path)) trigger_error ('storagePathNotSetted', E_USER_ERROR);
        $this->objs[$name] = $obj;
        if ($saveInFS) {
            $filename = self::name2FS($name);
            if (method_exists($this, 'prepareToBeStored')) $data = $this->prepareToBeStored($this->objs[$name]);
                else $data = $this->objs[$name];
            if (!is_scalar($data)) trigger_error ('dataMustBeScalarToBeStored', E_USER_ERROR);
            $result = file_put_contents ("$this->path/$filename", $data);
            if ($result === false) trigger_error ('unableToWriteOjbectFile', E_USER_ERROR);
        }
    }

    function deleteObj ($name) {
        if (!isset($this->path)) trigger_error ('storagePathNotSetted', E_USER_ERROR);
        if (!array_key_exists($name, $this->objs)) trigger_error ('objectNameNotFound', E_USER_ERROR);
        $filename = self::name2FS($name);
        $result =@ unlink ("$this->path/$filename");
        if (!$result) trigger_error ('unableToDeleteOjbectFile', E_USER_ERROR);
        unset ($this->objs[$name]);
    }

    protected static function readObjNamesFromPath ($path) {
        $result = array();
        $avoidFilenames = array ('.', '..');
        if (is_dir($path)) {
            if ($dh = opendir($path)) {
                while (($filename = readdir($dh)) !== false) {
                    if (in_array($filename, $avoidFilenames)) continue;
                    $name = self::FS2name ($filename);
                    $result[$name] = NULL;
                }
                closedir($dh);
            } else trigger_error ('unableToOpenListPath', E_USER_ERROR);
        } else trigger_error ('connectionListPathNotValid', E_USER_ERROR);
        return $result;
    }

    protected static function name2FS ($name) {
        $filename = str_replace(" ", "-", $name);
        return $filename;
    }
    
    protected static function FS2name ($filename) {
        $name = str_replace("-", " ", $filename);
        return $name;
    }
}

?>
Return current item: Storage