File::statChecker()   C
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 49
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 49
rs 6.1403
cc 8
eloc 26
nc 9
nop 1
1
<?php
2
/**
3
 * File
4
 *
5
 * ファイル操作のためのファンクション群
6
 *
7
 * @package           risoluto
8
 * @author            Risoluto Developers
9
 * @license           http://opensource.org/licenses/bsd-license.php new BSD license
10
 * @copyright     (C) 2008-2015 Risoluto Developers / All Rights Reserved.
11
 */
12
13
//------------------------------------------------------//
14
// 名前空間の定義
15
//------------------------------------------------------//
16
namespace Risoluto;
17
18
class File
19
{
20
    //------------------------------------------------------//
21
    // クラスメソッド定義
22
    //------------------------------------------------------//
23
    /**
24
     * __construct()
25
     *
26
     * コンストラクタ
27
     */
28
    private function __construct()
29
    {
30
    }
31
32
    /**
33
     * statChecker(array $target)
34
     *
35
     * 指定されたファイルやディレクトリのステータスをチェックする
36
     *
37
     * @access    public
38
     *
39
     * @param     array $target チェック対象の情報が格納された連想配列
40
     *
41
     * @return    array    チェック結果が格納された連想配列
42
     */
43
    public static function statChecker( array $target )
44
    {
45
        // ローカル変数の初期化
46
        $result = [
47
            'path' => 'unknown',
48
            'required' => 'unknown',
49
            'real' => 'unknown',
50
            'result' => 'unknown'
51
        ];
52
53
        // 引数が配列でない場合は即時return
54
        if (!is_array( $target )) {
55
            return $result;
56
        }
57
58
        // 共通情報はまとめてセット
59
        $result[ 'path' ] = $target[ 'path' ];
60
        $result[ 'required' ] = $target[ 'stat' ];
61
62
        // 対象が存在しない場合は「missing」をセットし、即時return
63
        clearstatcache();
64
        if (!file_exists( $target[ 'path' ] )) {
65
            // 結果をセット
66
            $result[ 'real' ] = 'missing';
67
            $result[ 'result' ] = 'NG';
68
69
            return $result;
70
        }
71
72
        // キャッシュステータスのクリア
73
        clearstatcache();
74
        // 判定を実施(defaultは書かない)
75
        // 評価項目を増やすにはここにcaseを追加してください
76
        switch ($target[ 'stat' ]) {
77
            // 読込可
78
            case 'readable':
79
                $result[ 'real' ] = $target[ 'stat' ];
80
                $result[ 'result' ] = ( ( is_readable( $target[ 'path' ] ) and !is_writable( $target[ 'path' ] ) ) ? 'OK' : 'NG' );
81
                break;
82
83
            // 書込可
84
            case 'writable':
85
                $result[ 'real' ] = 'writable';
86
                $result[ 'result' ] = ( is_writable( $target[ 'path' ] ) ? 'OK' : 'NG' );
87
                break;
88
        }
89
90
        return $result;
91
    }
92
93
    /**
94
     * fileOperator($operation, $target, $destination = null, $prefix = null)
95
     *
96
     * 指定されたファイルやディレクトリに対し、作成/コピー/移動/削除等を行う
97
     *
98
     * @access    public
99
     *
100
     * @param     string $operation 処理内容を示す文字列(make/copy/move/unlink/mkdir/rmdir/symlink)
101
     * @param     string $target 対象となるパス
102
     * @param     string $destination コピー又は移動先となるパス
103
     * @param     string $prefix ファイル中の「[[[_PREFIX]]]」を置換する文字列
104
     *
105
     * @return    boolean   処理結果(true:成功/false:失敗)
106
     */
107
    public static function fileOperator( $operation, $target, $destination = null, $prefix = null )
108
    {
109
        // 処理結果の初期化
110
        $retVal = false;
111
112
        // operationの内容によって、処理を分ける
113
        switch ($operation) {
114
            // make
115
            case 'make':
116
                if (@touch( $target )) {
117
                    $retVal = true;
118
                }
119
                break;
120
121
            // copy
122
            case 'copy':
123
                if (@copy( $target, $destination )) {
124
                    $retVal = true;
125
                }
126
                break;
127
128
            // copy with replace
129
            case 'copy_with_replace':
130
                // ファイルの中身を一度すべて取得
131
                $text = @file_get_contents( $target );
132
133
                // $prefixがセットされていたら置換処理を実施
134
                if (!empty( $prefix )) {
135
                    $text = str_replace( '[[[_PREFIX]]]', $prefix, $text );
136
                }
137
138
                // ファイルを出力
139
                if (@file_put_contents( $destination, $text )) {
140
                    $retVal = true;
141
                }
142
                break;
143
144
            // move
145
            case 'move':
146
                if (@rename( $target, $destination )) {
147
                    $retVal = true;
148
                }
149
                break;
150
151
            // unlink
152
            case 'unlink':
153
                if (@unlink( $target )) {
154
                    $retVal = true;
155
                }
156
                break;
157
158
            // mkdir
159
            case 'mkdir':
160
                if (@mkdir( $target, 0777, true )) {
161
                    $retVal = true;
162
                }
163
                break;
164
165
            // rmdir
166
            case 'rmdir':
167
                if (@rmdir( $target )) {
168
                    $retVal = true;
169
                }
170
                break;
171
172
            // symlink
173
            case 'symlink':
174
                if (@symlink( $target, $destination )) {
175
                    $retVal = true;
176
                }
177
                break;
178
        }
179
180
        return $retVal;
181
    }
182
183
}