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.31 2007/03/27 01:58:41 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.     * - Whether to treat numbered arrays as duplicates of their parent directive
  42.     *   or as individual directives
  43.     *   Ex: $options['duplicateDirectives'] = false;
  44.     *
  45.     * @var  array 
  46.     */
  47.     var $options = array('name' => 'conf',
  48.                          'useAttr' => true,
  49.                          'duplicateDirectives' => true);
  50.  
  51.     /**
  52.     * Constructor
  53.     *
  54.     * @access public
  55.     * @param    string  $options    Options to be used by renderer
  56.     */
  57.     function Config_Container_PHPArray($options = array())
  58.     {
  59.         foreach ($options as $key => $value{
  60.             $this->options[$key$value;
  61.         }
  62.     // end constructor
  63.  
  64.     /**
  65.     * Parses the data of the given configuration file
  66.     *
  67.     * @access public
  68.     * @param string $datasrc    path to the configuration file
  69.     * @param object $obj        reference to a config object
  70.     * @return mixed    returns a PEAR_ERROR, if error occurs or true if ok
  71.     */
  72.     function &parseDatasrc($datasrc&$obj)
  73.     {
  74.         $return = true;
  75.         if (empty($datasrc)) {
  76.             return PEAR::raiseError("Datasource file path is empty."nullPEAR_ERROR_RETURN);
  77.         }
  78.         if (is_array($datasrc)) {
  79.             $this->_parseArray($datasrc$obj->container);
  80.         else {
  81.             if (!file_exists($datasrc)) {
  82.                 return PEAR::raiseError("Datasource file does not exist."nullPEAR_ERROR_RETURN);        
  83.             else {
  84.                 include($datasrc);
  85.                 if (!isset(${$this->options['name']}|| !is_array(${$this->options['name']})) {
  86.                     return PEAR::raiseError("File '$datasrc' does not contain a required '".$this->options['name']."' array."nullPEAR_ERROR_RETURN);
  87.                 }
  88.             }
  89.             $this->_parseArray(${$this->options['name']}$obj->container);
  90.         }
  91.         return $return;
  92.     // end func parseDatasrc
  93.  
  94.     /**
  95.     * Parses the PHP array recursively
  96.     * @param array  $array      array values from the config file
  97.     * @param object $container  reference to the container object
  98.     * @access private
  99.     * @return void 
  100.     */
  101.     function _parseArray($array&$container)
  102.     {
  103.         foreach ($array as $key => $value{
  104.             switch ((string)$key{
  105.                 case '@':
  106.                     $container->setAttributes($value);
  107.                     break;
  108.                 case '#':
  109.                     $container->setType('directive');
  110.                     $container->setContent($value);
  111.                     break;
  112.                 default:
  113.                     if (is_array($value)) {
  114.                         if ($this->options['duplicateDirectives'== true && is_integer(key($value))) {
  115.                             foreach ($value as $nestedValue{
  116.                                 if (is_array($nestedValue)) {
  117.                                     $section =$container->createSection($key);
  118.                                     $this->_parseArray($nestedValue$section);
  119.                                 else {
  120.                                     $container->createDirective($key$nestedValue);
  121.                                 }
  122.                             }
  123.                         else {
  124.                             $section =$container->createSection($key);
  125.                             $this->_parseArray($value$section);
  126.                         }
  127.                     else {
  128.                         $container->createDirective($key$value);
  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.             $string is_int($obj->name"[".$obj->name."]" "['".$obj->name."']";
  210.             $string $this->_getParentString($obj->parent).$string;
  211.             $count $obj->parent->countChildren(null$obj->name);
  212.             if ($count > 1{
  213.                 $string .= '['.$obj->getItemPosition(false).']';
  214.             }
  215.         }
  216.         else {
  217.             if (empty($this->options['name'])) {
  218.                 $string .= '$'.$obj->name;
  219.             else {
  220.                 $string .= '$'.$this->options['name'];
  221.             }
  222.         }
  223.         return $string;
  224.     // end func _getParentString
  225.  
  226.     /**
  227.     * Writes the configuration to a file
  228.     *
  229.     * @param  mixed  datasrc        info on datasource such as path to the configuraton file
  230.     * @param  string configType     (optional)type of configuration
  231.     * @access public
  232.     * @return string 
  233.     */
  234.     function writeDatasrc($datasrc&$obj)
  235.     {
  236.         $fp @fopen($datasrc'w');
  237.         if ($fp{
  238.             $string "<?php\n"$this->toString($obj."?>"// <? : Fix my syntax coloring
  239.             $len strlen($string);
  240.             @flock($fpLOCK_EX);
  241.             @fwrite($fp$string$len);
  242.             @flock($fpLOCK_UN);
  243.             @fclose($fp);
  244.             return true;
  245.         else {
  246.             return PEAR::raiseError('Cannot open datasource for writing.'1PEAR_ERROR_RETURN);
  247.         }
  248.     // end func writeDatasrc
  249. // end class Config_Container_PHPArray
  250. ?>

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