|
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-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hidev\helpers; |
|
12
|
|
|
|
|
13
|
|
|
use Yii; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Hidev FileHelper. |
|
17
|
|
|
*/ |
|
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
|
5 |
|
return false; |
|
33
|
5 |
|
} |
|
34
|
5 |
|
|
|
35
|
|
|
static::mkdir(dirname($path)); |
|
36
|
5 |
|
if (!is_writable($path) && posix_isatty(0)) { |
|
37
|
|
|
$tmp = tempnam('/tmp', 'hidev.'); |
|
38
|
|
|
file_put_contents($tmp, $content); |
|
39
|
|
|
chmod($tmp, 0644); |
|
40
|
|
|
passthru("sudo cp $tmp $path"); |
|
41
|
|
|
unlink($tmp); |
|
42
|
|
|
} else { |
|
43
|
|
|
file_put_contents($path, $content); |
|
44
|
|
|
} |
|
45
|
|
|
Yii::warning('Written file: ' . $path, 'file'); |
|
46
|
|
|
|
|
47
|
|
|
return true; |
|
48
|
5 |
|
} |
|
49
|
|
|
|
|
50
|
5 |
|
/** |
|
51
|
5 |
|
* Creates directory if not exists. |
|
52
|
5 |
|
* Creates intermediate directories when necessary. |
|
53
|
|
|
* @param string $path |
|
54
|
|
|
* @return bool true if directory did not exist and was created |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function mkdir($path) |
|
57
|
|
|
{ |
|
58
|
|
|
$path = Yii::getAlias($path); |
|
59
|
5 |
|
$path = rtrim(trim($path), '/'); |
|
60
|
|
|
if (!file_exists($path)) { |
|
61
|
|
|
mkdir($path, 0777, true); |
|
62
|
|
|
Yii::warning('Created dir: ' . $path . '/', 'file'); |
|
63
|
|
|
|
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Creates a symlink. |
|
72
|
|
|
* @param string $src existing source path |
|
73
|
|
|
* @param string $dst destionation path to be created |
|
74
|
|
|
* @return true on success or false on failure |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function symlink($src, $dst) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!is_writable($dst) && posix_isatty(0)) { |
|
79
|
|
|
passthru("sudo ln -s $src $dst", $retval); |
|
80
|
|
|
return $retval === 0; |
|
81
|
|
|
} else { |
|
82
|
|
|
return symlink($this->path, $dest); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: