Config
[ class tree: Config ] [ index: Config ] [ all elements ]

Source for file PHPArray.php

Documentation is available at PHPArray.php

  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.29 2006/10/20 03:13:35 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.  
  35.     /**
  36.     * This class options:
  37.     * - name of the config array to parse/output
  38.     *   Ex: $options['name'] = 'myconf';
  39.     * - Whether to add attributes to the array
  40.     *   Ex: $options['useAttr'] = false;
  41.     *
  42.     * @var  array 
  43.     */
  44.     var $options = array('name' => 'conf',
  45.                          'useAttr' => true);
  46.  
  47.     /**
  48.     * Constructor
  49.     *
  50.     * @access public
  51.     * @param    string  $options    Options to be used by renderer
  52.     */
  53.     function Config_Container_PHPArray($options = array())
  54.     {
  55.         foreach ($options as $key => $value{
  56.             $this->options[$key$value;
  57.         }
  58.     // end constructor
  59.  
  60.     /**
  61.     * Parses the data of the given configuration file
  62.     *
  63.     * @access public
  64.     * @param string $datasrc    path to the configuration file
  65.     * @param object $obj        reference to a config object
  66.     * @return mixed    returns a PEAR_ERROR, if error occurs or true if ok
  67.     */
  68.     function &parseDatasrc($datasrc&$obj)
  69.     {
  70.         $return = true;
  71.         if (empty($datasrc)) {
  72.             return PEAR::raiseError("Datasource file path is empty."nullPEAR_ERROR_RETURN);
  73.         }
  74.         if (is_array($datasrc)) {
  75.             $this->_parseArray($datasrc$obj->container);
  76.         else {
  77.             if (!file_exists($datasrc)) {
  78.                 return PEAR::raiseError("Datasource file does not exist."nullPEAR_ERROR_RETURN);        
  79.             else {
  80.                 include($datasrc);
  81.                 if (!isset(${$this->options['name']}|| !is_array(${$this->options['name']})) {
  82.                     return PEAR::raiseError("File '$datasrc' does not contain a required '".$this->options['name']."' array."nullPEAR_ERROR_RETURN);
  83.                 }
  84.             }
  85.             $this->_parseArray(${$this->options['name']}$obj->container);
  86.         }
  87.         return $return;
  88.     // end func parseDatasrc
  89.  
  90.     /**
  91.     * Parses the PHP array recursively
  92.     * @param array  $array      array values from the config file
  93.     * @param object $container  reference to the container object
  94.     * @access private
  95.     * @return void 
  96.     */
  97.     function _parseArray($array&$container)
  98.     {
  99.         foreach ($array as $key => $value{
  100.             switch ((string)$key{
  101.                 case '@':
  102.                     $container->setAttributes($value);
  103.                     break;
  104.                 case '#':
  105.                     $container->setType('directive');
  106.                     $container->setContent($value);
  107.                     break;
  108.                 default:
  109. /*                    if (is_array($value)) {
  110.                         $section =& $container->createSection($key);
  111.                         $this->_parseArray($value, $section);
  112.                     } else {
  113.                         $container->createDirective($key, $value);
  114.                     }*/
  115.  
  116.                     if (is_array($value)) {
  117.                         if (is_integer(key($value))) {
  118.                             foreach ($value as $nestedValue{
  119.                                 if (is_array($nestedValue)) {
  120.                                     $section =$container->createSection($key);
  121.                                     $this->_parseArray($nestedValue$section);
  122.                                 else {
  123.                                     $container->createDirective($key$nestedValue);
  124.                                 }
  125.                             }
  126.                         else {
  127.                             $section =$container->createSection($key);
  128.                             $this->_parseArray($value$section);
  129.                         }
  130.                     else {
  131.                         $container->createDirective($key$value);
  132.                     }
  133.                                                                                                                                                                 
  134.             }
  135.         }
  136.     // end func _parseArray
  137.  
  138.     /**
  139.     * Returns a formatted string of the object
  140.     * @param    object  $obj    Container object to be output as string
  141.     * @access   public
  142.     * @return   string 
  143.     */
  144.     function toString(&$obj)
  145.     {
  146.         if (!isset($string)) {
  147.             $string '';
  148.         }
  149.         switch ($obj->type{
  150.             case 'blank':
  151.                 $string .= "\n";
  152.                 break;
  153.             case 'comment':
  154.                 $string .= '// '.$obj->content."\n";
  155.                 break;
  156.             case 'directive':
  157.                 $attrString '';
  158.                 $parentString $this->_getParentString($obj);
  159.                 $attributes $obj->getAttributes();
  160.                 if ($this->options['useAttr'&& is_array($attributes&& count($attributes> 0{
  161.                     // Directive with attributes '@' and value '#'
  162.                     $string .= $parentString."['#']";
  163.                     foreach ($attributes as $attr => $val{
  164.                         $attrString .= $parentString."['@']"
  165.                                     ."['".$attr."'] = '".addslashes($val)."';\n";
  166.                     }
  167.                 else {
  168.                     $string .= $parentString;
  169.                 }
  170.                 $string .= ' = ';
  171.                 if (is_string($obj->content)) {
  172.                     $string .= "'".addslashes($obj->content)."'";
  173.                 elseif (is_int($obj->content|| is_float($obj->content)) {
  174.                     $string .= $obj->content;
  175.                 elseif (is_bool($obj->content)) {
  176.                     $string .= ($obj->content'true' 'false';
  177.                 }
  178.                 $string .= ";\n";
  179.                 $string .= $attrString;
  180.                 break;
  181.             case 'section':
  182.                 $attrString '';
  183.                 $attributes $obj->getAttributes();
  184.                 if ($this->options['useAttr'&& is_array($attributes&& count($attributes> 0{
  185.                     $parentString $this->_getParentString($obj);
  186.                     foreach ($attributes as $attr => $val{
  187.                         $attrString .= $parentString."['@']"
  188.                                     ."['".$attr."'] = '".addslashes($val)."';\n";
  189.                     }
  190.                 }
  191.                 $string .= $attrString;
  192.                 if ($count count($obj->children)) {
  193.                     for ($i = 0; $i $count$i++{
  194.                         $string .= $this->toString($obj->getChild($i));
  195.                     }
  196.                 }
  197.                 break;
  198.             default:
  199.                 $string '';
  200.         }
  201.         return $string;
  202.     // end func toString
  203.  
  204.     /**
  205.     * Returns a formatted string of the object parents
  206.     * @access private
  207.     * @return string 
  208.     */
  209.     function _getParentString(&$obj)
  210.     {
  211.         $string '';
  212.         if (!$obj->isRoot()) {
  213.             $string is_int($obj->name"[".$obj->name."]" "['".$obj->name."']";
  214.             $string $this->_getParentString($obj->parent).$string;
  215.             $count $obj->parent->countChildren(null$obj->name);
  216.             if ($count > 1{
  217.                 $string .= '['.$obj->getItemPosition(false).']';
  218.             }
  219.         }
  220.         else {
  221.             if (empty($this->options['name'])) {
  222.                 $string .= '$'.$obj->name;
  223.             else {
  224.                 $string .= '$'.$this->options['name'];
  225.             }
  226.         }
  227.         return $string;
  228.     // end func _getParentString
  229.  
  230.     /**
  231.     * Writes the configuration to a file
  232.     *
  233.     * @param  mixed  datasrc        info on datasource such as path to the configuraton file
  234.     * @param  string configType     (optional)type of configuration
  235.     * @access public
  236.     * @return string 
  237.     */
  238.     function writeDatasrc($datasrc&$obj)
  239.     {
  240.         $fp @fopen($datasrc'w');
  241.         if ($fp{
  242.             $string "<?php\n"$this->toString($obj."?>"// <? : Fix my syntax coloring
  243.             $len strlen($string);
  244.             @flock($fpLOCK_EX);
  245.             @fwrite($fp$string$len);
  246.             @flock($fpLOCK_UN);
  247.             @fclose($fp);
  248.             return true;
  249.         else {
  250.             return PEAR::raiseError('Cannot open datasource for writing.'1PEAR_ERROR_RETURN);
  251.         }
  252.     // end func writeDatasrc
  253. // end class Config_Container_PHPArray
  254. ?>

Documentation generated on Mon, 11 Mar 2019 14:50:54 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.