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.21 2003/11/29 11:05:34 mansion 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.         }
  116.     // end func _parseArray
  117.  
  118.     /**
  119.     * Returns a formatted string of the object
  120.     * @param    object  $obj    Container object to be output as string
  121.     * @access   public
  122.     * @return   string 
  123.     */
  124.     function toString(&$obj)
  125.     {
  126.         if (!isset($string)) {
  127.             $string '';
  128.         }
  129.         switch ($obj->type{
  130.             case 'blank':
  131.                 $string .= "\n";
  132.                 break;
  133.             case 'comment':
  134.                 $string .= '// '.$obj->content."\n";
  135.                 break;
  136.             case 'directive':
  137.                 $attrString '';
  138.                 $parentString $this->_getParentString($obj);
  139.                 $attributes $obj->getAttributes();
  140.                 if ($this->options['useAttr'&& is_array($attributes&& count($attributes> 0{
  141.                     // Directive with attributes '@' and value '#'
  142.                     $string .= $parentString."['#']";
  143.                     foreach ($attributes as $attr => $val{
  144.                         $attrString .= $parentString."['@']"
  145.                                     ."['".$attr."'] = '".$val."';\n";
  146.                     }
  147.                 else {
  148.                     $string .= $parentString;
  149.                 }
  150.                 $string .= ' = ';
  151.                 if (is_string($obj->content)) {
  152.                     $string .= "'".$obj->content."'";
  153.                 elseif (is_int($obj->content|| is_float($obj->content)) {
  154.                     $string .= $obj->content;
  155.                 elseif (is_bool($obj->content)) {
  156.                     $string .= ($obj->content'true' 'false';
  157.                 }
  158.                 $string .= ";\n";
  159.                 $string .= $attrString;
  160.                 break;
  161.             case 'section':
  162.                 $attrString '';
  163.                 $attributes $obj->getAttributes();
  164.                 if ($this->options['useAttr'&& is_array($attributes&& count($attributes> 0{
  165.                     $parentString $this->_getParentString($obj);
  166.                     foreach ($attributes as $attr => $val{
  167.                         $attrString .= $parentString."['@']"
  168.                                     ."['".$attr."'] = '".$val."';\n";
  169.                     }
  170.                 }
  171.                 $string .= $attrString;
  172.                 if ($count count($obj->children)) {
  173.                     for ($i = 0; $i $count$i++{
  174.                         $string .= $this->toString($obj->getChild($i));
  175.                     }
  176.                 }
  177.                 break;
  178.             default:
  179.                 $string '';
  180.         }
  181.         return $string;
  182.     // end func toString
  183.  
  184.     /**
  185.     * Returns a formatted string of the object parents
  186.     * @access private
  187.     * @return string 
  188.     */
  189.     function _getParentString(&$obj)
  190.     {
  191.         $string '';
  192.         if (!$obj->isRoot()) {
  193.             if (!$obj->parent->isRoot()) {
  194.                 $string is_int($obj->name"[".$obj->name."]" "['".$obj->name."']";
  195.             else {
  196.                 if (empty($this->options['name'])) {
  197.                     $string .= '$'.$obj->name;
  198.                 else {
  199.                     $string .= '$'.$this->options['name']."['".$obj->name."']";
  200.                 }
  201.             }
  202.             $string $this->_getParentString($obj->parent).$string;
  203.             $count $obj->parent->countChildren(null$obj->name);
  204.             if ($count > 1{
  205.                 $string .= '['.$obj->getItemPosition().']';
  206.             }
  207.         }
  208.         return $string;
  209.     // end func _getParentString
  210.  
  211.     /**
  212.     * Writes the configuration to a file
  213.     *
  214.     * @param  mixed  datasrc        info on datasource such as path to the configuraton file
  215.     * @param  string configType     (optional)type of configuration
  216.     * @access public
  217.     * @return string 
  218.     */
  219.     function writeDatasrc($datasrc&$obj)
  220.     {
  221.         $fp @fopen($datasrc'w');
  222.         if ($fp{
  223.             $string "<?php\n"$this->toString($obj."?>"// <? : Fix my syntax coloring
  224.             $len strlen($string);
  225.             @flock($fpLOCK_EX);
  226.             @fwrite($fp$string$len);
  227.             @flock($fpLOCK_UN);
  228.             @fclose($fp);
  229.             return true;
  230.         else {
  231.             return PEAR::raiseError('Cannot open datasource for writing.'1PEAR_ERROR_RETURN);
  232.         }
  233.     // end func writeDatasrc
  234. // end class Config_Container_PHPArray
  235. ?>

Documentation generated on Mon, 11 Mar 2019 13:51:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.