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.23 2005/02/10 06:02:40 ryansking 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.         if (empty($datasrc)) {
  71.             return PEAR::raiseError("Datasource file path is empty."nullPEAR_ERROR_RETURN);
  72.         }
  73.         if (is_array($datasrc)) {
  74.             $this->_parseArray($datasrc$obj->container);
  75.         else {
  76.             if (!file_exists($datasrc)) {
  77.                 return PEAR::raiseError("Datasource file does not exist."nullPEAR_ERROR_RETURN);        
  78.             else {
  79.                 include($datasrc);
  80.                 if (!isset(${$this->options['name']}|| !is_array(${$this->options['name']})) {
  81.                     return PEAR::raiseError("File '$datasrc' does not contain a required '".$this->options['name']."' array."nullPEAR_ERROR_RETURN);
  82.                 }
  83.             }
  84.             $this->_parseArray(${$this->options['name']}$obj->container);
  85.         }
  86.         return true;
  87.     // end func parseDatasrc
  88.  
  89.     /**
  90.     * Parses the PHP array recursively
  91.     * @param array  $array      array values from the config file
  92.     * @param object $container  reference to the container object
  93.     * @access private
  94.     * @return void 
  95.     */
  96.     function _parseArray($array&$container)
  97.     {
  98.         foreach ($array as $key => $value{
  99.             switch ((string)$key{
  100.                 case '@':
  101.                     $container->setAttributes($value);
  102.                     break;
  103.                 case '#':
  104.                     $container->setType('directive');
  105.                     $container->setContent($value);
  106.                     break;
  107.                 default:
  108. /*                    if (is_array($value)) {
  109.                         $section =& $container->createSection($key);
  110.                         $this->_parseArray($value, $section);
  111.                     } else {
  112.                         $container->createDirective($key, $value);
  113.                     }*/
  114.  
  115.                     if (is_array($value)) {
  116.                         if (is_integer(key($value))) {
  117.                             foreach ($value as $nestedValue{
  118.                                 $section =$container->createSection($key);
  119.                                 $this->_parseArray($nestedValue$section);
  120.                             }
  121.                         else {
  122.  
  123.                             $section =$container->createSection($key);
  124.                             $this->_parseArray($value$section);
  125.                         }
  126.                     else {
  127.                         $container->createDirective($key$value);
  128.                     }
  129.                                                                                                                                                                 
  130.             }
  131.         }
  132.     // end func _parseArray
  133.  
  134.     /**
  135.     * Returns a formatted string of the object
  136.     * @param    object  $obj    Container object to be output as string
  137.     * @access   public
  138.     * @return   string 
  139.     */
  140.     function toString(&$obj)
  141.     {
  142.         if (!isset($string)) {
  143.             $string '';
  144.         }
  145.         switch ($obj->type{
  146.             case 'blank':
  147.                 $string .= "\n";
  148.                 break;
  149.             case 'comment':
  150.                 $string .= '// '.$obj->content."\n";
  151.                 break;
  152.             case 'directive':
  153.                 $attrString '';
  154.                 $parentString $this->_getParentString($obj);
  155.                 $attributes $obj->getAttributes();
  156.                 if ($this->options['useAttr'&& is_array($attributes&& count($attributes> 0{
  157.                     // Directive with attributes '@' and value '#'
  158.                     $string .= $parentString."['#']";
  159.                     foreach ($attributes as $attr => $val{
  160.                         $attrString .= $parentString."['@']"
  161.                                     ."['".$attr."'] = '".addslashes($val)."';\n";
  162.                     }
  163.                 else {
  164.                     $string .= $parentString;
  165.                 }
  166.                 $string .= ' = ';
  167.                 if (is_string($obj->content)) {
  168.                     $string .= "'".addslashes($obj->content)."'";
  169.                 elseif (is_int($obj->content|| is_float($obj->content)) {
  170.                     $string .= $obj->content;
  171.                 elseif (is_bool($obj->content)) {
  172.                     $string .= ($obj->content'true' 'false';
  173.                 }
  174.                 $string .= ";\n";
  175.                 $string .= $attrString;
  176.                 break;
  177.             case 'section':
  178.                 $attrString '';
  179.                 $attributes $obj->getAttributes();
  180.                 if ($this->options['useAttr'&& is_array($attributes&& count($attributes> 0{
  181.                     $parentString $this->_getParentString($obj);
  182.                     foreach ($attributes as $attr => $val{
  183.                         $attrString .= $parentString."['@']"
  184.                                     ."['".$attr."'] = '".addslashes($val)."';\n";
  185.                     }
  186.                 }
  187.                 $string .= $attrString;
  188.                 if ($count count($obj->children)) {
  189.                     for ($i = 0; $i $count$i++{
  190.                         $string .= $this->toString($obj->getChild($i));
  191.                     }
  192.                 }
  193.                 break;
  194.             default:
  195.                 $string '';
  196.         }
  197.         return $string;
  198.     // end func toString
  199.  
  200.     /**
  201.     * Returns a formatted string of the object parents
  202.     * @access private
  203.     * @return string 
  204.     */
  205.     function _getParentString(&$obj)
  206.     {
  207.         $string '';
  208.         if (!$obj->isRoot()) {
  209.             if (!$obj->parent->isRoot()) {
  210.                 $string is_int($obj->name"[".$obj->name."]" "['".$obj->name."']";
  211.             else {
  212.                 if (empty($this->options['name'])) {
  213.                     $string .= '$'.$obj->name;
  214.                 else {
  215.                     $string .= '$'.$this->options['name']."['".$obj->name."']";
  216.                 }
  217.             }
  218.             $string $this->_getParentString($obj->parent).$string;
  219.             $count $obj->parent->countChildren(null$obj->name);
  220.             if ($count > 1{
  221.                 $string .= '['.$obj->getItemPosition().']';
  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($fpLOCK_EX);
  242.             @fwrite($fp$string$len);
  243.             @flock($fpLOCK_UN);
  244.             @fclose($fp);
  245.             return true;
  246.         else {
  247.             return PEAR::raiseError('Cannot open datasource for writing.'1PEAR_ERROR_RETURN);
  248.         }
  249.     // end func writeDatasrc
  250. // end class Config_Container_PHPArray
  251. ?>

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