Test Failed
Push — master ( ac3303...b3b99f )
by 世昌
02:03
created

FileHelper::put()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\helper;
3
4
use nebula\component\loader\Path;
5
6
/**
7
 * 文件辅助函数
8
 */
9
class FileHelper
10
{
11
    /**
12
     * 判断文件是否存在
13
     *
14
     * @return boolean
15
     */
16
    public static function exist(string $path):bool
17
    {
18
        return Path::format($path) !== null;
19
    }
20
21
    /**
22
     * 删除文件
23
     *
24
     * @return boolean
25
     */
26
    public static function delete(string $filename):bool
27
    {
28
        if (($path=Path::format($filename)) !== null) {
29
            if (!is_writable($path)) {
30
                return false;
31
            }
32
            return unlink($path);
33
        }
34
        return true;
35
    }
36
37
    /**
38
     * 移动文件
39
     *
40
     * @param string $src
41
     * @param string $dest
42
     * @return boolean
43
     */    
44
    public static function move(string $src, string $dest):bool
45
    {
46
        if (($path=Path::format($src)) !== null && is_writable(dirname($dest))) {
47
            return rename($path, $dest);
48
        }
49
        return false;
50
    }
51
52
    /**
53
     * 复制文件
54
     *
55
     * @param string $src
56
     * @param string $dest
57
     * @return boolean
58
     */
59
    public static function copy(string $src, string $dest):bool
60
    {
61
        if (($path=Path::format($src)) !== null && is_writable(dirname($dest))) {
62
            return copy($path, $dest);
63
        }
64
        return false;
65
    }
66
67
    /**
68
     * 写入文件
69
     *
70
     * @param string $filename
71
     * @param string $content
72
     * @param integer $flags
73
     * @return boolean
74
     */
75
    public static function put(string $filename, string $content, int $flags = 0):bool
76
    {
77
        if (is_writeable(dirname($filename))) {
78
            return file_put_contents($filename, $content, $flags) > 0;
79
        }
80
        return false;
81
    }
82
83
    /**
84
     * 读取文件
85
     *
86
     * @param string $name
87
     * @return string|null
88
     */
89
    public static function get(string $filename):?string
90
    {
91
        if (is_readable($filename) && ($path=Path::format($filename)) !== null) {
92
            return file_get_contents($path);
93
        }
94
        return null;
95
    }
96
}
97