<?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_CacheFileupdate.php,v 1.5 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_CacheFileupdate.php,v 1.5 2007/01/03 11:17:37 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_CacheFileupdate extends SyL_Cache
{
    /**
     * 最新の更新日時
     *
     * @access private
     * @var int
     */
    var $mtime;

    /**
     * コンストラクタ
     *
     * @access public
     */
    function SyL_CacheFileupdate($key)
    {
        // 親クラスのコンストラクタ
        parent::SyL_Cache($key);
        // 時間キャッシュ無効
        $this->setLifeTime(null);
    }

    /**
     * キャッシュデータを取得
     *
     * @access public
     * @param mixed ファイル名(配列)
     */
    function read($files)
    {
        if (!is_array($files)) {
            $files = array($files);
        }
        $mtime = false;
        // 最新の更新日付を取得する
        foreach ($files as $file) {
            if (!file_exists($file)) {
                trigger_error("[SyL error] File not found for cache update ({$file})", E_USER_ERROR);
            }
            $mtime_tmp = filemtime($file);
            if ($mtime) {
                if ($mtime_tmp > $mtime) {
                    $mtime = $mtime_tmp;
                }
            } else {
                $mtime = $mtime_tmp;
            }
        }

        $cache_mtime = $this->getModifyTime();
        if ($mtime && $cache_mtime && ($mtime <= $cache_mtime)) {
            // キャッシュ取得
            return parent::read(false);
        } else {
            // キャッシュ無し
            $this->mtime = $mtime;
            return false;
        }
    }

    /**
     * キャッシュファイルを保存する
     *
     * @access private
     * @param mixed キャッシュデータ
     * @return bool true: 保存成功、false: 保存エラー
     */
    function write($data)
    {
        return parent::write($data, $this->mtime);
    }
}

?>
