<?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_ConfigComponents.php,v 1.9 2007/01/03 11:17:37 seasonstream Exp $
 * @link      http://www.syl.jp/
 * -----------------------------------------------------------------------------
 */

/**
 * コンポーネント設定ファイルを読み込むクラス
 *
 * @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_ConfigComponents.php,v 1.9 2007/01/03 11:17:37 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_ConfigComponents extends SyL_Config
{
    /**
     * デフォルト実行順序
     * 
     * @access private
     * @param int
     */
    var $default_priority = 3;

    /**
     * コンストラクタ
     *
     * @access public
     * @param string XMLファイル名
     */
    function SyL_ConfigComponents($file_name)
    {
        // 親クラスのコンストラクタ
        parent::SyL_Config($file_name);
    }

    /**
     * XMLファイルの解析処理
     *
     * @access public
     */
    function parseXML()
    {
        if (SYL_WEBAPP_CACHE_DIR) {
            // キャッシュを使用する
            $key = implode('', $this->file_names);
            $cache =& SyL_Cache::getObject($key, 'fileupdate');
            $cache->setCacheDir(SYL_WEBAPP_CACHE_DIR . '/components');
            $this->config = $cache->read($this->file_names);
            if (!$this->config) {
                parent::parseXML();
                $cache->write($this->config);
            }
        } else {
            // キャッシュを使用しない
            parent::parseXML();
        }
    }

    /**
     * XMLデータ内容取得ハンドラメソッド
     * ※オーバーライド
     *
     * @access protected
     * @param string 現在XML解析中のパス
     * @param string XMLのタグの属性値
     * @param string XMLのタグの値
     */
    function getElement($current_path, $attribute, $text)
    {
        static $name  = '';
        static $event = '';
        static $method_name = '';

//echo $current_path . "<br>";

        // XML解析結果を取得
        switch ($current_path) {
        case '/components/component':
            if (isset($attribute['enable']) && ($attribute['enable'] === 'false')) {
                $name = '';
                return;
            }
            $name = $attribute['name'];
            $this->config[$name]['class']     = isset($attribute['class'])  ? $attribute['class']  : $name;
            $this->config[$name]['type']      = 'component';
            $this->config[$name]['file']      = isset($attribute['file'])   ? $attribute['file']   : '';
            $this->config[$name]['reference'] = (isset($attribute['reference']) && ($attribute['reference'] === 'true'));
            // 実行順序
            $this->config[$name]['priority'] = isset($attribute['priority']) ? (int)$attribute['priority'] : $this->default_priority;
            // コンストラクタインジェクションフラグ
            $this->config[$name]['constructor'] = false;
            // イベントタグ
            $event = isset($attribute['event']) ? $attribute['event'] : $this->event;
            $this->config[$name]['event'][$event][] = $name;
            // 引数
            $this->config[$name]['args'] = array();
            break;

        default:
            if (!$name) return;

            switch ($current_path) {
            case '/components/component/constructor':
                $method_name = 'constructor:' . (isset($attribute['static']) ? $attribute['static'] : '');
                // コンストラクタインジェクションフラグ
                $this->config[$name]['constructor'] = true;
                // イベントタグ
                $event = isset($attribute['event']) ? $attribute['event'] : $event;
                // コンストラクタインジェクションの場合、元のメソッドは削除
                $index = array_search($name, $this->config[$name]['event'][$event]);
                if ($index !== false) {
                    unset($this->config[$name]['event'][$event][$index]);
                }
                $this->config[$name]['event'][$event][] = $method_name;
                $this->config[$name]['args'][$method_name] = array();
                break;

            case '/components/component/constructor/arg':
                $type = isset($attribute['type']) ? $attribute['type'] : 'value';
                $ref  = (isset($attribute['reference']) && ($attribute['reference'] === 'true')) ? 'true' : 'false';
                $this->config[$name]['args'][$method_name][] = $type . ':' . $ref . ':' . $text;
                break;

            case '/components/component/setter':
                // ダイレクトセットタグ
                if (isset($attribute['direct']) && ($attribute['direct'] === 'true')) {
                    $method_name = 'setter:' . $attribute['name'] . ':true';
                } else {
                    $method_name = 'setter:set' . ucfirst(strtolower($attribute['name'])) . ':false';
                }
                // イベントタグ
                $event = isset($attribute['event']) ? $attribute['event'] : $event;
                $this->config[$name]['event'][$event][] = $method_name;
                $this->config[$name]['args'][$method_name] = array();
                break;

            case '/components/component/setter/arg':
                $type = isset($attribute['type']) ? $attribute['type'] : 'value';
                $ref  = (isset($attribute['reference']) && ($attribute['reference'] === 'true')) ? 'true' : 'false';
                $this->config[$name]['args'][$method_name][] = $type . ':' . $ref . ':' . $text;
                break;

            case '/components/component/method':
                $method_name = 'method:' . $attribute['name'];
                // イベントタグ
                $event = isset($attribute['event']) ? $attribute['event'] : $event;
                $this->config[$name]['event'][$event][] = $method_name;
                $this->config[$name]['args'][$method_name] = array();
                break;

            case '/components/component/method/arg':
                $type = isset($attribute['type']) ? $attribute['type'] : 'value';
                $ref  = (isset($attribute['reference']) && ($attribute['reference'] === 'true')) ? 'true' : 'false';
                $this->config[$name]['args'][$method_name][] = $type . ':' . $ref . ':' . $text;
                break;
            }
        }
    }
}

?>
