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

Documentation generated on Mon, 11 Mar 2019 15:42:04 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.