INI形式ファイルのパーサー

お前の予定!で使っている自作クラス IniParserは、phpでini形式のファイルを読み込むためのクラスです。簡易形式の設定ファイルをもとにphpを動かすことを目的に作りました。
あれ?これってなんか意味あんのかな?あとで見直さないとダメだ。

<?php /* -*-java-*- */
/*
 * require_once('PEAR.php');
 */

/**
 * INI形式ファイルを扱う IniParser.class
 * パースして配列で取得する。
 * parse_ini_file() を使用している。
 * http://jp.php.net/manual/ja/function.parse-ini-file.php
 * INI形式とは、以下のようなやつね。行末の ;(セミコロン)は無視されますよ。
 * <code>
 * [GENERAL]  <<-- フィールド名
 * error.display = 1 <<-- 要素名 = 値
 * error.notice = E~ALL
 *
 * [MULTIBYTE]
 * mbstring.default_encoding = EUC-JP,SJIS;
 * mbstring.http_input = auto;
 * </code>
 *
 * 使い方:
 * <code>
 * $ini =& new IniParser( $filename );
 * $indexies = $ini->getValues( $elementname, $fieldname );
 * </code>
 * <code>
 * $ini =& new IniParser();
 * if ( $ini->parse( $filename ) ) {
 *    $ary =& $ini->getIni();
 *    $ini->getFields();                // すべてのフィールド名を取得
 *    $ini->getElements('multibyte');   // 指定したフィールドの要素を取得
 * }
 * else {
 *    PEAR::raseError();
 * }
 * </code>
 *
 * @todo        配列からINIファイルを作成する。要素名を 03 などとしたいときに問題がある。自分で作ったメソッドを使うほうが良いかもしれない。
 * @author      k5959k+yamada@gmail.com
 * @create      2006-09-30
 * @package     ya--mada
 * @see         INIConfigParser
 */
class IniParser {

    /**
     * パース後のデータ配列
     *
     * @access private
     * @var    array
     */
    var $_inimem = NULL;

    /**
     * ini形式ファイルのファイルパス
     *
     * @access public
     * @var    string
     */
    var $filename = NULL;


    /**
     * constractor IniParser class.
     *
     * @param string $filename
     */
    function IniParser ( $filename=NULL )
    {
        if ( !is_null($filename) ) {
            $this->filename = $filename;
            $this->parse( $filename );
        }
    }

    /**
     * パース後のメモリ上の値を取得する
     * <pre>
     * Array( [Google] => Array ( [query]  => Array( [0] => q ),
     *                            [client] => Array( [0] => sourceid,
     *                                               [1] => client )
     *                          ),
     *        [Yahoo!] => Array ( [query]  => Array( [0] => p ),
     *                            [client] => Array( [0] => fr )
     *                          ),
     *       [InfoSeek] => Array( [query]  => Array( [0] => qt )
     *                          )
     *      )
     * </pre>
     *
     * @access public
     * @return array
     */
    function & getIni () {

        if ( is_null($this->_inimem) )
            $this->parse();

        return $this->_inimem;
    }

    /**
     * パースする(本当はパースの準備)
     * セクション名付きで iniファイルを読み込みます。
     * ファイル名がセットされていないときには False を返す。
     *
     * @access public
     * @param  string $filename
     * @retun  bool
     */
    function parse ( $filename=NULL ) {

        if ( !is_null($filename) )
            $this->filename = $filename;

        if ( is_null($this->filename) )
            return FALSE;

        if ( !file_exists($this->filename) || !is_readable($this->filename) ) {
            echo "cannot read your file : $this->filename.\n";
            return FALSE;
        }

        return $this->_resolveArray();
    }


    /**
     * 実際には、このメソッド内でパースをおこなう。
     * $_inimem に、ini形式を展開する。
     *
     * @access private
     * @param  array
     * @return bool
     */
    function _resolveArray () {

        /*
         * parse_ini_file() を使用する
         *
        $this->_inimem = parse_ini_file( $this->filename, TRUE );
        *
         *
         */


        /*
         * parse_ini_file() を使用しない
         */
        $this->_inimem = array();
        $array = file( $this->filename );
        foreach( $array as $line ) {
            $line = trim( $line );
                if ( preg_match( '/^\[(.+)\]$/', $line, $matches ) ) {
                    $index = $matches[1];
                }
                if ( preg_match( '/^[ ]*(.*)[ ]*=(.*)/i', $line, $matches ) ) {
                    $key = trim($matches[1]);
                    $val = trim($matches[2]);
                    $val = preg_replace( '/(.*);(.*)/', '${1}', $val );
                    $val = trim($val);
                    if ( preg_match( '/^[;#]/', $key ) ) {
                        $key = '';
                        $val = '';
                    }

                    if ( !isset($index) ) $index = 'general';
                    if ( !isset($this->_inimem[$index]) )
                        $this->_inimem[$index] = array();

                    if ( !empty($key) ) {
                        $this->_inimem[$index] =
                            $this->_inimem[$index] + array((string)$key=>$val);
                    }
                }
        }
        // print_r($this->_inimem);
        /*
         *
         */
        return empty($this->_inimem) ? FALSE : TRUE;
    }


    /**
     * iniで定義されたフィールド名をすべて取得する。
     * 結果は、配列で返す。
     *
     * @access public
     * @param  bool   $insense 大小文字を区別しない(小文字にして評価)
     * @todo   要素名、値を取り出すときには trim() で余計な空白を除去
     * @return array
     */
    function & getFields ( $insense=TRUE ) {

        if ( is_null($this->_inimem) ) $this->parse();

        $ary = array();
        foreach ( $this->_inimem as $key => $val ) {
            if ( $insense )
                $ary[] = strToLower($key);
            else
                $ary[] = $key;
        }

        return $ary;
    }


    /**
     * 指定されたフィールドの要素すべてを取得する。
     * 結果は、配列で返す。
     *
     * @access public
     * @param  string $name フィールド名
     * @param  bool   $insense 大小文字を区別しない(小文字にして評価)
     * @return array
     * @todo   要素名、値を取り出すときには trim() で余計な空白を除去
     */
    function getElements ( $name=NULL, $insense=TRUE ) {

        if ( is_null($name) ) $name = 'general';

        if ( is_null($this->_inimem) ) {
            if( !$this->parse() )
                return null;
        }

        if ( $insense ) {
            foreach ( $this->_inimem as $key => $val ) {
                if ( strToLower($name) == strToLower($key) )
                    return $this->_inimem[$key];
            }
        }

        return isset($this->_inimem[$name]) ? $this->_inimem[$name] : null;
    }

    /**
     * フィールド名と要素名を指定して値を取得する
     *
     * @access public
     * @param  string $elementname
     * @param  string $fieldname ヌルの場合、General で
     * @param  bool   $insense 大小文字を区別しない
     * @return ary
     */
    function getValue ( $elementname, $fieldname=NULL, $insense=TRUE ) {

        $ary = $this->getElements( $fieldname, $insense );

        return isset($ary[$elementname]) ? $ary[$elementname] : NULL;
    }
}