Location: PHPKode > projects > ZZ/OSS Installer > zic-1.1.0dev1/installer/lib/ZZOSS_Config/Config.php
<?php
    /*
    Copyright (C) 2001-2004 ZZOSS GbR, http://www.zzoss.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    */
    
    /**
    @version $Id: Config.php,v 1.6 2004/03/23 18:21:27 ordnas Exp $
    @copyright Copyright &copy; 2001-2004 ZZ/OSS GbR, http://www.zzoss.com
    @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
    */

/**   
 * This class parses the XML config file and provides the objects of the  
 * XML document.   
 *   
 * Extends PEAR XML_Parser class.   
 *   
 * @version  1   
 * @author   Sandro Zic <hide@address.com>; |    
 * @author   Christian Zonsius <hide@address.com>; |    
 */   
   
class ZZOSS_Config {   
    
    /**
    * The config file.
    *
    */
    var $file;
    /**
    * The configuration parameters.
    */
    var $config;
    var $options;
    var $cache_file;
    var $cache_dir;
    
    function ZZOSS_Config($options = array())
    {
       $this->setOptions($options);
    }
    
    function setOptions($options)
    {
         $this->options = $options;
    }
    
    function setCacheDir($dir)
    {
        $this->cache_dir = $dir;
    }
    
    function setFile($file)
    {
        $this->file = $file;
    }
    
    function writeCacheUnique()
    {
        // compose the name of the cache file
        $tmpPath = $this->cache_dir;
        $parts = pathinfo($this->file);
        if(strlen($tmpPath)<1) {
            $tmpPath = $parts['dirname'].'/';
        } else {
            $cache_file_prefix = md5($parts['dirname']).'_';
        }
        $cache_file = $tmpPath.$cache_file_prefix.substr($parts['basename'],0,strrpos($parts['basename'],'.')).'.reg';
        return $this->writeCache($cache_file);
    }
    
    function query($xPath)
    {
        if(ZZOSS_CONFIG_DEBUG){
            ZZOSS_Debug::log('ZZOSS_Config', ' Config file $this->file = '.$this->file, __FILE__, __LINE__, __FUNCTION__, __CLASS__);
        }
        
        if(isset($this->options['cache']) && $this->options['cache'] && $this->_isCachied()) {
            // read serialized array
            $content = file_get_contents($this->cache_file);
            $this->config = unserialize($content);
        } else {
            $this->config = $this->_configToArray();
            // should we create a cache file ?
            if(isset($this->options['cache'])){
                // write serialized array
                $this->writeCache();
            }
        }
        if(ZZOSS_CONFIG_DEBUG){
            ZZOSS_Debug::log('ZZOSS_Config', ' Assign new config array.', __FILE__, __LINE__, __FUNCTION__, __CLASS__);
        }
        
        if(ZZOSS_CONFIG_DEBUG){
            ZZOSS_Debug::log('ZZOSS_Config', ' Config array $this->config = '.var_export($this->config, true), __FILE__, __LINE__, __FUNCTION__, __CLASS__);
        }
        
        $patterns[0] = '/\]/';
        $replacements[0] = "";
        $patterns[1] = '/\[/';
        $replacements[1] = "/";
        $patterns[2] = "/@/";
        $replacements[2] = '@/';
        $patterns[3] = "/\//";
        $replacements[3] = "']['";
        $param = "\$param =& \$this->config['root']['".preg_replace($patterns,$replacements,trim($xPath,'/')).'\']'.";"; 
        //echo '<br/>'.$param.'<br>';//die();
        eval($param);
        /*
        echo '<pre>';
        print_r($param);
        echo '</pre>';
        */
        /*
        if($this->options['numeric'] && !is_string($param)){
            // now we check, if in fact the last XPath node is an attribute,
            // only then we should not create the child sequence
            if(!eregi('\[@.+\]$', $xPath)){
                return array(0 => $param);  
            }
        }*/
        return $param;
    }
    
    function _isCachied()
    {
        if(!file_exists($this->cache_file)) {
            return false;
        } else {
            // serialized file still up-to-date ?
            if(filectime($this->file)<filectime($this->cache_file)) {
                $readCache = true;
            }
        }
    }
    
    function _configToArray()
    {
        // initialize PEAR::Config
        include_once 'Config.php';
        $config = new Config();
        $root =& $config->parseConfig($this->file, 'xml');
        if(is_object($root)) {
            if(method_exists($root, 'toArray')) {
                $array = $root->toArray();
            } else {
                die('file: '.$this->file.'<br/>'.$root->error_message_prefix.' '.$root->message);
            }
        }
        unset($config);
        unset($root);
        
        if(isset($this->options['numeric']) && is_array($this->options['numeric'])) {
            $array = $this->_numeric($array);
        }
        /*
        echo '<pre>';
        print_r($array);
        echo '</pre>';
        */
        return $array;
    }
    
    /**
    * This method takes care that a sequence of XML elements with the same
    * name is preserved. Which means that they will be returned as a list
    * in a numeric array instead of the way that PEAR::Config does it.
    */
    function _numeric($array) {
        return ZZOSS_Config::prepareNumeric($array, $this->options['numeric']);
    }
    /*
    function _numeric($array) {
        if(is_array($array)) {
            foreach($array as $key => $val) {
                if(in_array($key, $this->options['numeric'])) {
                    if(is_array($val)) {
                        if(!isset($val[0])) {
                            unset($array[$key]);
                            $array[$key][0] = $this->_numeric($val);
                        }
                    }
                } elseif(is_array($val)) {
                    $array[$key] = $this->_numeric($val);
                }
            }
        }
        return $array;
    }
    */
    function prepareNumeric($array, $keys)
    {
        if(!is_array($array)){
            return false;
        }
        
        foreach($array as $key => $val) {
            if(in_array($key, $keys)) {
                if(is_array($val)) {
                    if(!isset($val[0])) {
                        unset($array[$key]);
                        $array[$key][0] = ZZOSS_Config::prepareNumeric($val, $keys);
                    } elseif(isset($val[1]) && is_array($val[1])) {
                        $array[$key][1] = ZZOSS_Config::prepareNumeric($val[1], $keys);
                    }
                }
            } elseif(is_array($val)) {
                $array[$key] = ZZOSS_Config::prepareNumeric($val, $keys);
            }
        }
        return $array;
    }
    
    function writeCache($cache_file = NULL)
    {
        if(!is_null($cache_file)){
            $this->cache_file = $cache_file;
        }
        if(!strlen($this->cache_file)){
            // Path to cache file has not been provided, so we write to
            // a file in the temp directory.
            include_once 'System.php';
            $dir = System::tmpdir().'/zzoss_installer/';
            System::mkdir(array('-p',$dir));
            $this->cache_file = $dir.md5(rand(2,5)).'.reg';
        }
        if($fp = @fopen($this->cache_file,'w')) {
            fputs($fp, serialize($this->config));
            fclose($fp);
            return $this->cache_file;
        } else {
            PEAR::raiseError('Cannot create cache file '.$this->cache_file);
        }
        
        return false;
    }
    
    function parseConfig($file, $cache_file = NULL) 
    {
        if(!file_exists($file)){
            PEAR::raiseError($file.' does not exist.');
        }
        $this->file = $file;
        if(!is_null($cache_file)){
            $this->cache_file = $cache_file;
        }
        return true;
    } 
    
    function setCacheFile($file)
    {
        $this->cache_file = $file;
    }
    
    function getCacheFile()
    {
        return $this->cache_file;
    }
} 
?>
Return current item: ZZ/OSS Installer