source: branches/feature-module-update/data/class/batch/SC_Batch_Update.php @ 15666

Revision 15666, 3.7 KB checked in by nanasess, 17 years ago (diff)

メンバ変数修正

  • Property svn:keywords set to "Id Revision Date"
Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8// {{{ requires
9require_once(CLASS_PATH . "pages/LC_Page.php");
10
11/**
12 * アップデート機能 のバッチクラス.
13 *
14 * XXX Singleton にするべき...
15 *
16 * @package Batch
17 * @author LOCKON CO.,LTD.
18 * @version $Id$
19 */
20class SC_Batch_Update extends SC_Batch {
21
22    /**
23     * 変換したいファイルの拡張子をカンマ区切りで羅列.
24     */
25    var $includes = "php,inc,tpl,css,sql,js";
26
27    /**
28     * 除外するファイル名をカンマ区切りで羅列.
29     */
30    var $excludes = "distinfo.php";
31
32    /**
33     * バッチ処理を実行する.
34     *
35     * @param string $target アップデータファイルのディレクトリパス
36     * @return void
37     */
38    function execute($target = ".") {
39        $includeArray = explode(',', $this->includes);
40        $excludeArray = explode(',', $this->excludes);
41        $fileArrays = listdirs($target);
42
43        foreach ($fileArrays as $path) {
44            if (is_file($path)) {
45
46                // ファイル名を取得
47                $fileName = pathinfo($path, PATHINFO_BASENAME);
48
49                // 拡張子を取得
50                $suffix = pathinfo($path, PATHINFO_EXTENSION);
51
52                // distinfo の変数定義
53                $distinfo = isset($distinfo) ? $distinfo : "";
54
55                // distinfo.php を読み込む
56                if ($fileName == "distinfo.php") {
57                    @include_once($fileName);
58                }
59
60                // 除外ファイルをスキップ
61                if (in_array($fileName, $excludeArray)) {
62                    echo "excludes by " . $path . "\n";
63                    continue;
64                }
65
66                // sha1 を取得
67                $sha1 = sha1_file($path);
68
69                echo $sha1 . " => " . $path . "\n";
70
71
72                // 変換対象を順に処理
73                foreach ($includeArray as $include) {
74                    if ($suffix == $include) {
75
76                        // ファイル内容を取得
77                        $contents = file_get_contents($path);
78
79                        // 書き出し先を取得
80                        if (!empty($distinfo[$sha1])) {
81                            $out = $distinfo[$sha1];
82                        } else {
83                            die("ハッシュ値が一致しないため, コピー先が取得できません.");
84                        }
85
86                        // ファイルを書き出しモードで開く
87                        $handle = fopen($out, "w");
88                        if (!$handle) {
89                            echo "Cannot open file (". $out . ")";
90                            continue;
91                        }
92
93                        // 取得した内容を書き込む
94                        if (fwrite($handle, $contents) === false) {
95                            echo "Cannot write to file (" . $out . ")";
96                            continue;
97                        }
98
99                        echo "copyed " . $out . "\n";
100                        // ファイルを閉じる
101                        fclose($handle);
102                    }
103                }
104            }
105        }
106        echo "Finished Successful!\n";
107    }
108
109    /**
110     * $dir を再帰的に辿ってパス名を配列で返す.
111     *
112     * @param string 任意のパス名
113     * @return array $dir より下層に存在するパス名の配列
114     * @see http://www.php.net/glob
115     */
116    function listdirs($dir) {
117        static $alldirs = array();
118        $dirs = glob($dir . '/*');
119        if (count($dirs) > 0) {
120            foreach ($dirs as $d) $alldirs[] = $d;
121        }
122        foreach ($dirs as $dir) listdirs($dir);
123        return $alldirs;
124    }
125}
126?>
Note: See TracBrowser for help on using the repository browser.