home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Config / Container / PHPArray.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  9.5 KB  |  252 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Bertrand Mansion <bmansion@mamasam.com>                     |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: PHPArray.php,v 1.32 2007/06/12 05:24:19 aashley Exp $
  19.  
  20. /**
  21. * Config parser for common PHP configuration array
  22. * such as found in the horde project.
  23. *
  24. * Options expected is:
  25. * 'name' => 'conf'
  26. * Name of the configuration array.
  27. * Default is $conf[].
  28. * 'useAttr' => true
  29. * Whether we render attributes
  30. *
  31. * @author      Bertrand Mansion <bmansion@mamasam.com>
  32. * @package     Config
  33. */
  34. class Config_Container_PHPArray {
  35.  
  36.     /**
  37.     * This class options:
  38.     * - name of the config array to parse/output
  39.     *   Ex: $options['name'] = 'myconf';
  40.     * - Whether to add attributes to the array
  41.     *   Ex: $options['useAttr'] = false;
  42.     * - Whether to treat numbered arrays as duplicates of their parent directive
  43.     *   or as individual directives
  44.     *   Ex: $options['duplicateDirectives'] = false;
  45.     *
  46.     * @var  array
  47.     */
  48.     var $options = array('name' => 'conf',
  49.                          'useAttr' => true,
  50.                          'duplicateDirectives' => true);
  51.  
  52.     /**
  53.     * Constructor
  54.     *
  55.     * @access public
  56.     * @param    string  $options    Options to be used by renderer
  57.     */
  58.     function Config_Container_PHPArray($options = array())
  59.     {
  60.         foreach ($options as $key => $value) {
  61.             $this->options[$key] = $value;
  62.         }
  63.     } // end constructor
  64.  
  65.     /**
  66.     * Parses the data of the given configuration file
  67.     *
  68.     * @access public
  69.     * @param string $datasrc    path to the configuration file
  70.     * @param object $obj        reference to a config object
  71.     * @return mixed    returns a PEAR_ERROR, if error occurs or true if ok
  72.     */
  73.     function &parseDatasrc($datasrc, &$obj)
  74.     {
  75.         $return = true;
  76.         if (empty($datasrc)) {
  77.             return PEAR::raiseError("Datasource file path is empty.", null, PEAR_ERROR_RETURN);
  78.         }
  79.         if (is_array($datasrc)) {
  80.             $this->_parseArray($datasrc, $obj->container);
  81.         } else {
  82.             if (!file_exists($datasrc)) {
  83.                 return PEAR::raiseError("Datasource file does not exist.", null, PEAR_ERROR_RETURN);
  84.             } else {
  85.                 include($datasrc);
  86.                 if (!isset(${$this->options['name']}) || !is_array(${$this->options['name']})) {
  87.                     return PEAR::raiseError("File '$datasrc' does not contain a required '".$this->options['name']."' array.", null, PEAR_ERROR_RETURN);
  88.                 }
  89.             }
  90.             $this->_parseArray(${$this->options['name']}, $obj->container);
  91.         }
  92.         return $return;
  93.     } // end func parseDatasrc
  94.  
  95.     /**
  96.     * Parses the PHP array recursively
  97.     * @param array  $array      array values from the config file
  98.     * @param object $container  reference to the container object
  99.     * @access private
  100.     * @return void
  101.     */
  102.     function _parseArray($array, &$container)
  103.     {
  104.         foreach ($array as $key => $value) {
  105.             switch ((string)$key) {
  106.                 case '@':
  107.                     $container->setAttributes($value);
  108.                     break;
  109.                 case '#':
  110.                     $container->setType('directive');
  111.                     $container->setContent($value);
  112.                     break;
  113.                 default:
  114.                     if (is_array($value)) {
  115.                         if ($this->options['duplicateDirectives'] == true && is_integer(key($value))) {
  116.                             foreach ($value as $nestedValue) {
  117.                                 if (is_array($nestedValue)) {
  118.                                     $section =& $container->createSection($key);
  119.                                     $this->_parseArray($nestedValue, $section);
  120.                                 } else {
  121.                                     $container->createDirective($key, $nestedValue);
  122.                                 }
  123.                             }
  124.                         } else {
  125.                             $section =& $container->createSection($key);
  126.                             $this->_parseArray($value, $section);
  127.                         }
  128.                     } else {
  129.                         $container->createDirective($key, $value);
  130.                     }
  131.             }
  132.         }
  133.     } // end func _parseArray
  134.  
  135.     /**
  136.     * Returns a formatted string of the object
  137.     * @param    object  $obj    Container object to be output as string
  138.     * @access   public
  139.     * @return   string
  140.     */
  141.     function toString(&$obj)
  142.     {
  143.         if (!isset($string)) {
  144.             $string = '';
  145.         }
  146.         switch ($obj->type) {
  147.             case 'blank':
  148.                 $string .= "\n";
  149.                 break;
  150.             case 'comment':
  151.                 $string .= '// '.$obj->content."\n";
  152.                 break;
  153.             case 'directive':
  154.                 $attrString = '';
  155.                 $parentString = $this->_getParentString($obj);
  156.                 $attributes = $obj->getAttributes();
  157.                 if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) {
  158.                     // Directive with attributes '@' and value '#'
  159.                     $string .= $parentString."['#']";
  160.                     foreach ($attributes as $attr => $val) {
  161.                         $attrString .= $parentString."['@']"
  162.                                     ."['".$attr."'] = '".addcslashes($val, "\\'")."';\n";
  163.                     }
  164.                 } else {
  165.                     $string .= $parentString;
  166.                 }
  167.                 $string .= ' = ';
  168.                 if (is_string($obj->content)) {
  169.                     $string .= "'".addcslashes($obj->content, "\\'")."'";
  170.                 } elseif (is_int($obj->content) || is_float($obj->content)) {
  171.                     $string .= $obj->content;
  172.                 } elseif (is_bool($obj->content)) {
  173.                     $string .= ($obj->content) ? 'true' : 'false';
  174.                 }
  175.                 $string .= ";\n";
  176.                 $string .= $attrString;
  177.                 break;
  178.             case 'section':
  179.                 $attrString = '';
  180.                 $attributes = $obj->getAttributes();
  181.                 if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) {
  182.                     $parentString = $this->_getParentString($obj);
  183.                     foreach ($attributes as $attr => $val) {
  184.                         $attrString .= $parentString."['@']"
  185.                                     ."['".$attr."'] = '".addcslashes($val, "\\'")."';\n";
  186.                     }
  187.                 }
  188.                 $string .= $attrString;
  189.                 if ($count = count($obj->children)) {
  190.                     for ($i = 0; $i < $count; $i++) {
  191.                         $string .= $this->toString($obj->getChild($i));
  192.                     }
  193.                 }
  194.                 break;
  195.             default:
  196.                 $string = '';
  197.         }
  198.         return $string;
  199.     } // end func toString
  200.  
  201.     /**
  202.     * Returns a formatted string of the object parents
  203.     * @access private
  204.     * @return string
  205.     */
  206.     function _getParentString(&$obj)
  207.     {
  208.         $string = '';
  209.         if (!$obj->isRoot()) {
  210.             $string = is_int($obj->name) ? "[".$obj->name."]" : "['".$obj->name."']";
  211.             $string = $this->_getParentString($obj->parent).$string;
  212.             $count = $obj->parent->countChildren(null, $obj->name);
  213.             if ($count > 1) {
  214.                 $string .= '['.$obj->getItemPosition(false).']';
  215.             }
  216.         }
  217.         else {
  218.             if (empty($this->options['name'])) {
  219.                 $string .= '$'.$obj->name;
  220.             } else {
  221.                 $string .= '$'.$this->options['name'];
  222.             }
  223.         }
  224.         return $string;
  225.     } // end func _getParentString
  226.  
  227.     /**
  228.     * Writes the configuration to a file
  229.     *
  230.     * @param  mixed  datasrc        info on datasource such as path to the configuraton file
  231.     * @param  string configType     (optional)type of configuration
  232.     * @access public
  233.     * @return string
  234.     */
  235.     function writeDatasrc($datasrc, &$obj)
  236.     {
  237.         $fp = @fopen($datasrc, 'w');
  238.         if ($fp) {
  239.             $string = "<?php\n". $this->toString($obj) ."?>"; // <? : Fix my syntax coloring
  240.             $len = strlen($string);
  241.             @flock($fp, LOCK_EX);
  242.             @fwrite($fp, $string, $len);
  243.             @flock($fp, LOCK_UN);
  244.             @fclose($fp);
  245.             return true;
  246.         } else {
  247.             return PEAR::raiseError('Cannot open datasource for writing.', 1, PEAR_ERROR_RETURN);
  248.         }
  249.     } // end func writeDatasrc
  250. } // end class Config_Container_PHPArray
  251. ?>
  252.