1 | <?php |
||
19 | class FileHelper |
||
20 | { |
||
21 | /** |
||
22 | * Reads and returns file content. |
||
23 | * @param string $path |
||
24 | * @return string|false false if can't read |
||
25 | */ |
||
26 | public static function read($path, $die = true) |
||
39 | |||
40 | /** |
||
41 | * Writes given content to the file. |
||
42 | * Doesn't touch file if it has exactly same content. |
||
43 | * Creates intermediate directories when necessary. |
||
44 | * @param string $path |
||
45 | * @param string $content |
||
46 | * @return bool true if file was changed |
||
47 | */ |
||
48 | 3 | public static function write($path, $content) |
|
49 | { |
||
50 | 3 | $path = Yii::getAlias($path); |
|
51 | 3 | if (is_file($path) && file_get_contents($path) === $content) { |
|
52 | return false; |
||
53 | } |
||
54 | |||
55 | 3 | static::mkdir(dirname($path)); |
|
56 | try { |
||
57 | 3 | file_put_contents($path, $content); |
|
58 | 3 | } catch (\Exception $e) { |
|
59 | if (posix_isatty(0)) { |
||
60 | $tmp = tempnam('/tmp', 'hidev.'); |
||
61 | file_put_contents($tmp, $content); |
||
62 | chmod($tmp, 0644); |
||
63 | passthru("sudo cp $tmp $path"); |
||
64 | unlink($tmp); |
||
65 | } |
||
66 | } |
||
67 | 3 | Yii::warning('Written file: ' . $path, 'file'); |
|
68 | |||
69 | 3 | return true; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Creates directory if not exists. |
||
74 | * Creates intermediate directories when necessary. |
||
75 | * @param string $path |
||
76 | * @return bool true if directory did not exist and was created |
||
77 | */ |
||
78 | 3 | public static function mkdir($path) |
|
91 | |||
92 | /** |
||
93 | * Creates a symlink. |
||
94 | * @param string $src existing source path |
||
95 | * @param string $dst destionation path to be created |
||
96 | * @return true on success and false/null on failure |
||
97 | */ |
||
98 | public static function symlink($src, $dst) |
||
109 | } |
||
110 |