Config
[ class tree: Config ] [ index: Config ] [ all elements ]

Source for file IniCommented.php

Documentation is available at IniCommented.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. // | Author: Bertrand Mansion <bmansion@mamasam.com>                      |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: IniCommented.php 306554 2010-12-21 20:04:20Z cweiske $
  19.  
  20. /**
  21. * Config parser for PHP .ini files with comments
  22. *
  23. @author      Bertrand Mansion <bmansion@mamasam.com>
  24. @package     Config
  25. */
  26.  
  27.     /**
  28.      * Options for this class:
  29.      * - linebreak - Character to use as new line break when serializing
  30.      *
  31.      * @var array 
  32.      */
  33.     var $options = array(
  34.         'linebreak' => "\n"
  35.     );
  36.  
  37.     /**
  38.     * Constructor
  39.     *
  40.     * @access public
  41.     * @param    string  $options    (optional)Options to be used by renderer
  42.     */
  43.     function Config_Container_IniCommented($options = array())
  44.     {
  45.         $this->options = array_merge($this->options$options);
  46.     // end constructor
  47.  
  48.     /**
  49.     * Parses the data of the given configuration file
  50.     *
  51.     * @access public
  52.     * @param string $datasrc    path to the configuration file
  53.     * @param object $obj        reference to a config object
  54.     * @return mixed returns a PEAR_ERROR, if error occurs or true if ok
  55.     */
  56.     function &parseDatasrc($datasrc&$obj)
  57.     {
  58.         $return = true;
  59.         if (!file_exists($datasrc)) {
  60.             return PEAR::raiseError(
  61.                 'Datasource file does not exist.',
  62.                 nullPEAR_ERROR_RETURN
  63.             );
  64.         }
  65.         $lines file($datasrc);
  66.         if ($lines === false{
  67.             return PEAR::raiseError(
  68.                 'File could not be read',
  69.                 nullPEAR_ERROR_RETURN
  70.             );
  71.         }
  72.  
  73.         $n = 0;
  74.         $lastline '';
  75.         $currentSection =$obj->container;
  76.         foreach ($lines as $line{
  77.             $n++;
  78.             if (preg_match('/^\s*;(.*?)\s*$/'$line$match)) {
  79.                 // a comment
  80.                 $currentSection->createComment($match[1]);
  81.             elseif (preg_match('/^\s*$/'$line)) {
  82.                 // a blank line
  83.                 $currentSection->createBlank();
  84.             elseif (preg_match('/^\s*([a-zA-Z0-9_\-\.\s:]*)\s*=\s*(.*)\s*$/'$line$match)) {
  85.                 // a directive
  86.                 
  87.                 $values $this->_quoteAndCommaParser($match[2]);
  88.                 if (PEAR::isError($values)) {
  89.                     return PEAR::raiseError($values);
  90.                 }
  91.                 
  92.                 if (count($values)) {
  93.                     foreach($values as $value{
  94.                         if ($value[0== 'normal'{
  95.                             $currentSection->createDirective(trim($match[1])$value[1]);
  96.                         }
  97.                         if ($value[0== 'comment'{
  98.                             $currentSection->createComment(substr($value[1]1));
  99.                         }
  100.                     }
  101.                 }
  102.             elseif (preg_match('/^\s*\[\s*(.*)\s*\]\s*$/'$line$match)) {
  103.                 // a section
  104.                 $currentSection =$obj->container->createSection($match[1]);
  105.             else {
  106.                 return PEAR::raiseError("Syntax error in '$datasrc' at line $n."nullPEAR_ERROR_RETURN);
  107.             }
  108.         }
  109.         return $return;
  110.     // end func parseDatasrc
  111.  
  112.     /**
  113.      * Quote and Comma Parser for INI files
  114.      *
  115.      * This function allows complex values such as:
  116.      *
  117.      * <samp>
  118.      * mydirective = "Item, number \"1\"", Item 2 ; "This" is really, really tricky
  119.      * </samp>
  120.      * @param  string  $text    value of a directive to parse for quotes/multiple values
  121.      * @return array   The array returned contains multiple values, if any (unquoted literals
  122.      *                  to be used as is), and a comment, if any.  The format of the array is:
  123.      *
  124.      *  <pre>
  125.      *  array(array('normal', 'first value'),
  126.      *        array('normal', 'next value'),...
  127.      *        array('comment', '; comment with leading ;'))
  128.      *  </pre>
  129.      * @author Greg Beaver <cellog@users.sourceforge.net>
  130.      * @access private
  131.      */
  132.     function _quoteAndCommaParser($text)
  133.     {   
  134.         $text trim($text);
  135.         if ($text == ''{
  136.             $emptyNode = array();
  137.             $emptyNode[0][0'normal';
  138.             $emptyNode[0][1'';
  139.             return $emptyNode;
  140.         }
  141.  
  142.         // tokens
  143.         $tokens['normal'= array('"'';'',');
  144.         $tokens['quote'= array('"''\\');
  145.         $tokens['escape'= false; // cycle
  146.         $tokens['after_quote'= array(','';');
  147.         
  148.         // events
  149.         $events['normal'= array('"' => 'quote'';' => 'comment'',' => 'normal');
  150.         $events['quote'= array('"' => 'after_quote''\\' => 'escape');
  151.         $events['after_quote'= array(',' => 'normal'';' => 'comment');
  152.         
  153.         // state stack
  154.         $stack = array();
  155.  
  156.         // return information
  157.         $return = array();
  158.         $returnpos = 0;
  159.         $returntype 'normal';
  160.  
  161.         // initialize
  162.         array_push($stack'normal');
  163.         $pos = 0; // position in $text
  164.  
  165.         do {
  166.             $char $text{$pos};
  167.             $state $this->_getQACEvent($stack);
  168.  
  169.             if ($tokens[$state]{
  170.                 if (in_array($char$tokens[$state])) {
  171.                     switch($events[$state][$char]{
  172.                         case 'quote' :
  173.                             if ($state == 'normal' &&
  174.                                 isset($return[$returnpos]&&
  175.                                 !empty($return[$returnpos][1])) {
  176.                                 return PEAR::raiseError(
  177.                                     'invalid ini syntax, quotes cannot follow'
  178.                                     . " text '$text'",
  179.                                     nullPEAR_ERROR_RETURN
  180.                                 );
  181.                             }
  182.                             if ($returnpos >= 0 && isset($return[$returnpos])) {
  183.                                 // trim any unnecessary whitespace in earlier entries
  184.                                 $return[$returnpos][1trim($return[$returnpos][1]);
  185.                             else {
  186.                                 $returnpos++;
  187.                             }
  188.                             $return[$returnpos= array('normal''');
  189.                             array_push($stack'quote');
  190.                             continue 2;
  191.                         break;
  192.                         case 'comment' :
  193.                             // comments go to the end of the line, so we are done
  194.                             $return[++$returnpos= array('comment'substr($text$pos));
  195.                             return $return;
  196.                         break;
  197.                         case 'after_quote' :
  198.                             array_push($stack'after_quote');
  199.                         break;
  200.                         case 'escape' :
  201.                             // don't save the first slash
  202.                             array_push($stack'escape');
  203.                             continue 2;
  204.                         break;
  205.                         case 'normal' :
  206.                         // start a new segment
  207.                             if ($state == 'normal'{
  208.                                 $returnpos++;
  209.                                 continue 2;
  210.                             else {
  211.                                 while ($state != 'normal'{
  212.                                     array_pop($stack);
  213.                                     $state $this->_getQACEvent($stack);
  214.                                 }
  215.                                 $returnpos++;
  216.                             }
  217.                         break;
  218.                         default :
  219.                             PEAR::raiseError(
  220.                                 "::_quoteAndCommaParser oops, state missing",
  221.                                 nullPEAR_ERROR_DIE
  222.                             );
  223.                         break;
  224.                     }
  225.                 else {
  226.                     if ($state != 'after_quote'{
  227.                         if (!isset($return[$returnpos])) {
  228.                             $return[$returnpos= array('normal''');
  229.                         }
  230.                         // add this character to the current ini segment if
  231.                         // non-empty, or if in a quote
  232.                         if ($state == 'quote'{
  233.                             $return[$returnpos][1.= $char;
  234.                         elseif (!empty($return[$returnpos][1]||
  235.                                  (empty($return[$returnpos][1]&& trim($char!= '')) {
  236.                             if (!isset($return[$returnpos])) {
  237.                                 $return[$returnpos= array('normal''');
  238.                             }
  239.                             $return[$returnpos][1.= $char;
  240.                             if (strcasecmp('true'$return[$returnpos][1]== 0{
  241.                               $return[$returnpos][1'1';
  242.                             elseif (strcasecmp('false'$return[$returnpos][1]== 0{
  243.                               $return[$returnpos][1'';
  244.                             }
  245.                         }
  246.                     else {
  247.                         if (trim($char!= ''{
  248.                             return PEAR::raiseError(
  249.                                 'invalid ini syntax, text after a quote'
  250.                                 . " not allowed '$text'",
  251.                                 nullPEAR_ERROR_RETURN
  252.                             );
  253.                         }
  254.                     }
  255.                 }
  256.             else {
  257.                 // no tokens, so add this one and cycle to previous state
  258.                 $return[$returnpos][1.= $char;
  259.                 array_pop($stack);
  260.             }
  261.         while (++$pos strlen($text));
  262.         return $return;
  263.     // end func _quoteAndCommaParser
  264.     
  265.     /**
  266.      * Retrieve the state off of a state stack for the Quote and Comma Parser
  267.      * @param  array  $stack    The parser state stack
  268.      * @author Greg Beaver <cellog@users.sourceforge.net>
  269.      * @access private
  270.      */
  271.     function _getQACEvent($stack)
  272.     {
  273.         return array_pop($stack);
  274.     // end func _getQACEvent
  275.  
  276.     /**
  277.     * Returns a formatted string of the object
  278.     * @param    object  $obj    Container object to be output as string
  279.     * @access   public
  280.     * @return   string 
  281.     */
  282.     function toString(&$obj)
  283.     {
  284.         static $childrenCount$commaString;
  285.  
  286.         if (!isset($string)) {
  287.             $string '';
  288.         }
  289.         switch ($obj->type{
  290.             case 'blank':
  291.                 $string $this->options['linebreak'];
  292.                 break;
  293.             case 'comment':
  294.                 $string ';'.$obj->content . $this->options['linebreak'];
  295.                 break;
  296.             case 'directive':
  297.                 $count $obj->parent->countChildren('directive'$obj->name);
  298.                 $content $obj->content;
  299.                 if ($content === false{
  300.                     $content '0';
  301.                 elseif ($content === true{
  302.                     $content '1';
  303.                 elseif (strlen(trim($content)) strlen($content||
  304.                           strpos($content','!== false ||
  305.                           strpos($content';'!== false ||
  306.                           strpos($content'='!== false ||
  307.                           strpos($content'"'!== false ||
  308.                           strpos($content'%'!== false ||
  309.                           strpos($content'~'!== false ||
  310.                                                     strpos($content'!'!== false ||
  311.                                                     strpos($content'|'!== false ||
  312.                                                     strpos($content'&'!== false ||
  313.                                                     strpos($content'('!== false ||
  314.                                                     strpos($content')'!== false ||
  315.                                                     $content === 'none'{
  316.                     $content '"'.addslashes($content).'"';          
  317.                 }
  318.                 if ($count > 1{
  319.                     // multiple values for a directive are separated by a comma
  320.                     if (isset($childrenCount[$obj->name])) {
  321.                         $childrenCount[$obj->name]++;
  322.                     else {
  323.                         $childrenCount[$obj->name= 0;
  324.                         $commaString[$obj->name$obj->name.' = ';
  325.                     }
  326.                     if ($childrenCount[$obj->name== $count-1{
  327.                         // Clean the static for future calls to toString
  328.                         $string .= $commaString[$obj->name$content
  329.                             . $this->options['linebreak'];
  330.                         unset($childrenCount[$obj->name]);
  331.                         unset($commaString[$obj->name]);
  332.                     else {
  333.                         $commaString[$obj->name.= $content.', ';
  334.                     }
  335.                 else {
  336.                     $string $obj->name.' = '.$content $this->options['linebreak'];
  337.                 }
  338.                 break;
  339.             case 'section':
  340.                 if (!$obj->isRoot()) {
  341.                     $string '[' $obj->name . ']' $this->options['linebreak'];
  342.                 }
  343.                 if (count($obj->children> 0{
  344.                     for ($i = 0; $i count($obj->children)$i++{
  345.                         $string .= $this->toString($obj->getChild($i));
  346.                     }
  347.                 }
  348.                 break;
  349.             default:
  350.                 $string '';
  351.         }
  352.         return $string;
  353.     // end func toString
  354. // end class Config_Container_IniCommented
  355. ?>

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