1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Automation tool mixed with code generator for easier continuous development |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hidev |
6
|
|
|
* @package hidev |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hidev\helpers; |
12
|
|
|
|
13
|
|
|
use Yii; |
14
|
|
|
use yii\log\Logger; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Hidev FileHelper. |
18
|
|
|
*/ |
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) |
27
|
|
|
{ |
28
|
|
|
$path = Yii::getAlias($path); |
29
|
|
|
|
30
|
|
|
if (is_readable($path)) { |
31
|
|
|
return file_get_contents($path); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$level = $die ? Logger::LEVEL_ERROR : Logger::LEVEL_WARNING; |
35
|
|
|
Yii::getLogger()->log('Failed to read file: ' . $path, $level, 'file'); |
36
|
|
|
|
37
|
|
|
return false; |
38
|
|
|
} |
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
|
|
|
} 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) |
79
|
|
|
{ |
80
|
3 |
|
$path = Yii::getAlias($path); |
81
|
3 |
|
$path = rtrim(trim($path), '/'); |
82
|
3 |
|
if (!file_exists($path)) { |
83
|
|
|
mkdir($path, 0777, true); |
84
|
|
|
Yii::warning('Created dir: ' . $path . '/', 'file'); |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
return false; |
90
|
|
|
} |
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) |
99
|
|
|
{ |
100
|
|
|
try { |
101
|
|
|
// remove old link |
102
|
|
|
if (is_link($dst) && readlink($dst) !== $src) { |
103
|
|
|
unlink($dst); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!is_link($dst)) { |
107
|
|
|
|
108
|
|
|
return symlink($src, $dst); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} catch (\Exception $e) { |
111
|
|
|
if (posix_isatty(0)) { |
112
|
|
|
passthru("sudo ln -s $src $dst", $retval); |
113
|
|
|
|
114
|
|
|
return $retval === 0; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|