Source for file PHPArray.php
Documentation is available at PHPArray.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Bertrand Mansion <bmansion@mamasam.com> |
// +----------------------------------------------------------------------+
// $Id: PHPArray.php 306488 2010-12-20 08:45:09Z cweiske $
* Config parser for common PHP configuration array
* such as found in the horde project.
* Name of the configuration array.
* Whether we render attributes
* @author Bertrand Mansion <bmansion@mamasam.com>
* - name of the config array to parse/output
* Ex: $options['name'] = 'myconf';
* - Whether to add attributes to the array
* Ex: $options['useAttr'] = false;
* - Whether to treat numbered arrays as duplicates of their parent directive
* or as individual directives
* Ex: $options['duplicateDirectives'] = false;
'duplicateDirectives' => true );
* @param string $options Options to be used by renderer
foreach ($options as $key => $value) {
* Parses the data of the given configuration file
* @param string $datasrc path to the configuration file
* @param object $obj reference to a config object
* @return mixed returns a PEAR_ERROR, if error occurs or true if ok
return PEAR ::raiseError ("Datasource file path is empty.", null , PEAR_ERROR_RETURN );
$this->_parseArray ($datasrc, $obj->container );
return PEAR ::raiseError ("Datasource file does not exist.", null , PEAR_ERROR_RETURN );
return PEAR ::raiseError (" File '$datasrc' does not contain a required '". $this->options['name']. "' array.", null , PEAR_ERROR_RETURN );
$this->_parseArray ($ {$this->options['name']}, $obj->container );
} // end func parseDatasrc
* Parses the PHP array recursively
* @param array $array array values from the config file
* @param object $container reference to the container object
function _parseArray ($array, &$container)
foreach ($array as $key => $value) {
$container->setAttributes ($value);
$container->setType ('directive');
$container->setContent ($value);
if ($this->options['duplicateDirectives'] == true
//speed (first/one key is numeric)
//accuracy (all keys are numeric)
foreach ($value as $nestedValue) {
$section = & $container->createSection ($key);
$this->_parseArray ($nestedValue, $section);
$container->createDirective ($key, $nestedValue);
$section = & $container->createSection ($key);
$this->_parseArray ($value, $section);
$container->createDirective ($key, $value);
} // end func _parseArray
* Returns a formatted string of the object
* @param object $obj Container object to be output as string
$string .= '// '. $obj->content. "\n";
$parentString = $this->_getParentString ($obj);
$attributes = $obj->getAttributes ();
// Directive with attributes '@' and value '#'
$string .= $parentString. "['#']";
foreach ($attributes as $attr => $val) {
$attrString .= $parentString. "['@']"
$string .= $parentString;
$string .= $obj->content;
} elseif (is_bool($obj->content )) {
$string .= ($obj->content ) ? 'true' : 'false';
} elseif ($obj->content === null ) {
$attributes = $obj->getAttributes ();
$parentString = $this->_getParentString ($obj);
foreach ($attributes as $attr => $val) {
$attrString .= $parentString. "['@']"
if ($count = count($obj->children )) {
for ($i = 0; $i < $count; $i++ ) {
$string .= $this->toString($obj->getChild ($i));
* Returns a formatted string of the object parents
function _getParentString (&$obj)
$string = is_int($obj->name ) ? "[". $obj->name. "]" : "['". $obj->name. "']";
$string = $this->_getParentString ($obj->parent ). $string;
$count = $obj->parent ->countChildren (null , $obj->name );
$string .= '['. $obj->getItemPosition (false ). ']';
if (empty ($this->options['name'])) {
$string .= '$'. $obj->name;
$string .= '$'. $this->options['name'];
} // end func _getParentString
* Writes the configuration to a file
* @param mixed datasrc info on datasource such as path to the configuraton file
* @param string configType (optional)type of configuration
$fp = @fopen($datasrc, 'w');
$string = "<?php\n". $this->toString($obj) . "?>"; // <? : Fix my syntax coloring
return PEAR ::raiseError ('Cannot open datasource for writing.', 1 , PEAR_ERROR_RETURN );
} // end func writeDatasrc
} // end class Config_Container_PHPArray
Documentation generated on Mon, 11 Mar 2019 15:42:04 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|