<?php
/**
 * -----------------------------------------------------------------------------
 *
 * SyL - Web Application Framework for PHP
 *
 * PHP version 4 (>= 4.3.x) or 5
 *
 * Copyright (C) 2006 k.watanabe
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * -----------------------------------------------------------------------------
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_DefinesReader.php,v 1.9 2007/01/03 11:17:37 seasonstream Exp $
 * @link      http://www.syl.jp/
 * -----------------------------------------------------------------------------
 */

/**
 * 設定ファイルクラス
 */
require_once SYL_INCLUDE_DIR . '/framework/SyL_Config.php';

/**
 * 定数設定ファイルを読み込むクラス
 *
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_DefinesReader.php,v 1.9 2007/01/03 11:17:37 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_DefinesReader
{
    /**
     * コンストラクタ
     *
     * @static
     * @access public
     * @param string 定数設定ファイル名
     */
    function readConfig($defines_file_name)
    {
        // アプリケーション設定値を読み込み定数化
        $config_file1 = SYL_APP_DIR . "/{$defines_file_name}";
        // グローバル設定値を読み込み定数化
        $config_file2 = SYL_WEBAPP_CONFIG_DIR . "/{$defines_file_name}";
        // 引数用にファイルを結合
        $config_files = $config_file1 . PATH_SEPARATOR . $config_file2;

        $controller = null; // ダミー
        $config =& SyL_Config::getObject('defines', $config_files, $controller);
        $config->parseXML();

        // 定数読み込み
        SyL_DefinesReader::readDefine($config->getConfig());
    }

    /**
     * 定数読み込み
     *
     * @static
     * @access public
     * @param string 設定ファイル名
     * @param object コントローラオブジェクト
     */
    function readDefine($config)
    {
        foreach ($config as $key => $value) {
            $value = preg_replace_callback('/(.*)\{([^\}]+)\}(.*)/', array(__CLASS__, 'replaceConstant'), $value);
            define($key, $value);
        }
    }

    /**
     * 定数置き換えコールバックメソッド
     *
     * @static
     * @access public
     * @param array マッチした配列
     @ return mixed マッチした定数値
     */
    function replaceConstant($matches)
    {
        return defined($matches[2]) ? ($matches[1] . constant($matches[2]) . $matches[3]) : $matches[0];
    }
}

?>
