<?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_ConfigFilters.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_ConfigFilters.php,v 1.9 2007/01/03 11:17:37 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_ConfigFilters extends SyL_Config
{
    /**
     * フィルタイベント（アクション前）
     * 
     * @access private
     * @var string
     */
    var $filter_event_before = 'loadStream';
    /**
     * フィルタイベント（アクション後、ビュー表示前）
     * 
     * @access private
     * @var string
     */
    var $filter_event_mid = 'middleStream';
    /**
     * フィルタイベント（ビュー表示後）
     * 
     * @access private
     * @var string
     */
    var $filter_event_after = 'unloadStream';
    /**
     * デフォルト実行順序
     * 
     * @param int
     */
    var $default_priority = 7;
    /**
     * アクションファイル名
     * 
     * @access private
     * @var string
     */
    var $action_file = '';

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

    /**
     * コントローラをセット
     * ※オーバーライド
     *
     * @access public
     * @param object コントローラオブジェクト
     */
    function setController(&$controller)
    {
        $dm =& $controller->container->getComponent('datamanager');
        $this->action_file = $dm->getActionFile();
    }

    /**
     * XMLファイルの解析処理
     *
     * @access public
     */
    function parseXML()
    {
        if (SYL_WEBAPP_CACHE_DIR) {
            // キャッシュを使用する
            $key = implode('', $this->file_names) . $this->action_file;
            $cache =& SyL_Cache::getObject($key, 'fileupdate');
            $cache->setCacheDir(SYL_WEBAPP_CACHE_DIR . '/filters');
            $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 $method_name = '';

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

        // XML解析結果を取得
        switch ($current_path) {
        case '/filters/filter':
            if (isset($attribute['enable']) && ($attribute['enable'] === 'false')) {
                $name = '';
                return;
            }

            // アクセスアクション名確認
            $reg = isset($attribute['path']) ? str_replace('/', '\\/', $attribute['path']) : '(.*)';
            if (preg_match('/^' . $reg . '$/', $this->action_file)) {
                $name = '__filter_' . $attribute['name'];
                $this->config[$name]['class']     = $attribute['class'];
                $this->config[$name]['type']      = 'filter';
                $this->config[$name]['file']      = isset($attribute['file']) ? $attribute['file'] : '';
                $this->config[$name]['reference'] = false;
                // 実行順序
                $this->config[$name]['priority'] = $this->default_priority;
                // コンストラクタインジェクションフラグ
                $this->config[$name]['constructor'] = false;
                // イベントタグ（オブジェクト作成時）
                $this->config[$name]['event'][$this->event][] = $name;
                // 引数
                $this->config[$name]['args'] = array();
            } else {
                $name = '';
            }
            break;

        default:
            if (!$name) return;

            switch ($current_path) {
            case '/filters/filter/before':
               $method_name = 'method:' . $attribute['name'];
               // イベントタグ
               $this->config[$name]['event'][$this->filter_event_before][] = $method_name;
               $this->config[$name]['args'][$method_name]  = array();
               break;

            case '/filters/filter/mid':
               $method_name = 'method:' . $attribute['name'];
               // イベントタグ
               $this->config[$name]['event'][$this->filter_event_mid][] = $method_name;
               $this->config[$name]['args'][$method_name]  = array();
               break;

            case '/filters/filter/after':
               $method_name = 'method:' . $attribute['name'];
               // イベントタグ
               $this->config[$name]['event'][$this->filter_event_after][] = $method_name;
               $this->config[$name]['args'][$method_name]  = array();
               break;

            case '/filters/filter/mid/arg':
            case '/filters/filter/before/arg':
            case '/filters/filter/after/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;
            }
        }
    }
}

?>
