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

/**
 * PATH_INFO環境変数からフレームワーク遷移値を取得するクラス
 *
 * +++ 取得値
 * ・アクションファイル名
 * ・アクションクラス名
 * ・アクション名
 * ・テンプレートファイル名
 *
 * +++ 例）
 *   /dir/app1.php/hoge1/hoge2/cmd.test.html
 * 
 * [アクションファイル名]
 *   /hoge1/hoge2/test.php
 *   ※例1) .../app1.php/test2_2/test.html => /test2_2/test.php
 *   ※例2) .../app1.php/test.html => /test.php
 *   ※例3) .../app1.php/ => /（デフォルトアクション名）
 * [アクションクラス名]
 *   Hoge1_Hoge2_Test
 * [アクション名]
 *   cmd
 *   ※例1) .../app1.php/test2_2/test.html => （無し）
 *   ※例2) .../app1.php/ => （無し）
 * [テンプレートファイル名]
 *   /hoge1/hoge2/test.html
 *
 * @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_RouterPathinfo2.php,v 1.7 2007/01/03 11:17:37 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_RouterPathinfo2 extends SyL_Router
{
    /**
     * アクションの区切り文字
     *
     * @access private
     * @var string
     */
    var $action_delimiter = SYL_ROUTER_ACTION_DELIMITER;

    /**
     * コンストラクタ
     *
     * @access public
     */
    function SyL_RouterPathinfo2(&$action)
    {
        // PATH_INFO環境変数取得
        $pathinfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';

        // パスチェック
        if ($this->isInvalidPath($pathinfo)) {
            SyL_Response::redirect404();
        }

        if ($pathinfo == '') {
            $this->action_name = '';
            $this->action_file = '/' . $this->action_file_default;
        } else if (substr($pathinfo, -1) == '/') {
            $this->action_name = '';
            $this->action_file = $pathinfo . $this->action_file_default;
        } else {
            // 拡張子判定
            if ($this->ext && !preg_match('/^(.+)(' . preg_quote($this->url_ext) . ')$/', $pathinfo)) {
                SyL_Response::redirect404();
            }
            $pathinfo_file  = basename($pathinfo);
            $pathinfo_dir   = dirname($pathinfo);
            $pathinfo_files = explode($this->action_delimiter, $pathinfo_file, 2);
            if ($pathinfo_dir != '/') {
                $pathinfo_dir = $pathinfo_dir . DIRECTORY_SEPARATOR;
            }
            if (count($pathinfo_files) == 2) {
                if (($this->action_delimiter == '.') && ($this->url_ext == ('.' . $pathinfo_files[1]))) {
                    // アクションなし
                    $this->action_name = '';
                    $this->action_file = $pathinfo_dir . $pathinfo_files[0] . $this->exe_ext;
                } else {
                    if (preg_match('/^(.+)(' . preg_quote($this->url_ext) . '|' . preg_quote($this->exe_ext) . ')$/', $pathinfo_files[1], $matches)) {
                        // アクションあり
                        $this->action_name = $pathinfo_files[0];
                        $this->action_file = $pathinfo_dir . $matches[1] . $this->exe_ext;
                    } else {
                        // アクションあり
                        $this->action_name = $pathinfo_files[0];
                        $this->action_file = $pathinfo_dir . $pathinfo_files[1] . $this->exe_ext;
                    }
                }
            } else {
                // アクションなし
                $this->action_name = '';
                if (preg_match('/^(.+)(' . preg_quote($this->url_ext) . '|' . preg_quote($this->exe_ext) . ')$/', $pathinfo_files[1], $matches)) {
                    $this->action_file = $pathinfo_dir . $matches[1] . $this->exe_ext;
                } else {
                    $this->action_file = $pathinfo_dir . $pathinfo_files[1] . $this->exe_ext;
                }
            }
        }

        // アクションの置き換え
        if ($action !== null) {
            $this->action_name = $action;
        }

        // PATH_INFO配列取得
        $this->pathinfo = explode('/', $pathinfo);
        array_shift($this->pathinfo);
        if (count($this->pathinfo) > 0) {
            array_pop($this->pathinfo);
        }

        // クラスファイル取得
        if ($this->action_file != '') {
            $classname = implode('_', array_map('ucfirst', $this->pathinfo));
            if ($classname != '') {
                $this->class_name = $classname . '_' . ucfirst(basename($this->action_file, $this->exe_ext));
            } else {
                $this->class_name = ucfirst(basename($this->action_file,$this->exe_ext));
            }
        }

        // カレントURL取得
        $syl_self = $_SERVER['PHP_SELF'];
        if ($pathinfo == '') {
            $syl_self = $syl_self . '/';
        } else if (substr($pathinfo, -1) == '/') {
            $syl_self = $syl_self;
        } else {
            $syl_self = dirname($syl_self) . '/';
        }
        $this->setSyLSelf($syl_self);

/*
echo "pathinfo: " . $pathinfo . "<br>";
echo "action_name: " . $this->action_name . "<br>";
echo "action_file: " . $this->action_file . "<br>";
echo "class_name: " . $this->class_name . "<br>";
echo "syl_self: " . $this->syl_self . "<br>";
echo "<pre>";
print_r($this->pathinfo);
echo "</pre>";
exit;
*/

    }

}

?>
