1 | <?php |
||
18 | class FileHelper |
||
19 | { |
||
20 | /** |
||
21 | * Writes given content to the file. |
||
22 | * Doesn't touch file if it has exactly same content. |
||
23 | * Creates intermediate directories when necessary. |
||
24 | * @param string $path |
||
25 | * @param string $content |
||
26 | * @return bool true if file was changed |
||
27 | */ |
||
28 | 5 | public static function write($path, $content) |
|
29 | { |
||
30 | 5 | $path = Yii::getAlias($path); |
|
31 | 5 | if (is_file($path) && file_get_contents($path) === $content) { |
|
32 | return false; |
||
33 | } |
||
34 | |||
35 | 5 | static::mkdir(dirname($path)); |
|
36 | try { |
||
37 | 5 | file_put_contents($path, $content); |
|
38 | } catch (\Exception $e) { |
||
39 | if (posix_isatty(0)) { |
||
40 | $tmp = tempnam('/tmp', 'hidev.'); |
||
41 | file_put_contents($tmp, $content); |
||
42 | chmod($tmp, 0644); |
||
43 | passthru("sudo cp $tmp $path"); |
||
44 | unlink($tmp); |
||
45 | } |
||
46 | } |
||
47 | 5 | Yii::warning('Written file: ' . $path, 'file'); |
|
48 | |||
49 | 5 | return true; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * Creates directory if not exists. |
||
54 | * Creates intermediate directories when necessary. |
||
55 | * @param string $path |
||
56 | * @return bool true if directory did not exist and was created |
||
57 | */ |
||
58 | 5 | public static function mkdir($path) |
|
71 | |||
72 | /** |
||
73 | * Creates a symlink. |
||
74 | * @param string $src existing source path |
||
75 | * @param string $dst destionation path to be created |
||
76 | * @return true on success and false/null on failure |
||
77 | */ |
||
78 | public static function symlink($src, $dst) |
||
89 | } |
||
90 |