1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author: Viskov Sergey |
4
|
|
|
* @date : 4/28/16 |
5
|
|
|
* @time : 6:49 PM |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace LTDBeget\vim; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Vim |
12
|
|
|
* |
13
|
|
|
* @package LTDBeget\vim |
14
|
|
|
*/ |
15
|
|
|
final class Vim |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Options |
19
|
|
|
*/ |
20
|
|
|
private $options; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $editor; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $tempFiles; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $originalContent; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $prependCommand; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getPrependCommand() |
47
|
|
|
{ |
48
|
|
|
return $this->prependCommand; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $prependCommand |
54
|
|
|
* |
55
|
|
|
* @return Vim |
56
|
|
|
*/ |
57
|
|
|
public function setPrependCommand($prependCommand) : Vim |
58
|
|
|
{ |
59
|
|
|
$this->prependCommand = $prependCommand; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Vim constructor. |
66
|
|
|
* |
67
|
|
|
* @param Options|NULL $options |
68
|
|
|
*/ |
69
|
|
|
public function __construct(Options $options = NULL) |
70
|
|
|
{ |
71
|
|
|
$editor = shell_exec('which vim'); |
72
|
|
|
if(empty($editor)) { |
73
|
|
|
throw new \LogicException('Install vim'); |
74
|
|
|
} |
75
|
|
|
$this->editor = trim($editor); |
76
|
|
|
$this->options = $options ?? new Options(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Открывает Vim в консоли для редактирования |
81
|
|
|
*/ |
82
|
|
|
public function execute() |
83
|
|
|
{ |
84
|
|
|
passthru($this->getCommand()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $filename |
89
|
|
|
* @param string $content |
90
|
|
|
* @return Vim |
91
|
|
|
*/ |
92
|
|
|
public function addFileContent(string $filename, string $content) : Vim |
93
|
|
|
{ |
94
|
|
|
$this->originalContent[$filename] = $content; |
95
|
|
|
$this->tempFiles[$filename] = $this->makeTemporaryFile($filename, $content); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $key |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function isChanged(string $key) : bool |
105
|
|
|
{ |
106
|
|
|
return $this->originalContent[$key] !== $this->getContent($key); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $key |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getContent(string $key) : string |
114
|
|
|
{ |
115
|
|
|
return file_get_contents($this->tempFiles[$key]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $name |
120
|
|
|
* @param string $content |
121
|
|
|
* @return resource |
122
|
|
|
*/ |
123
|
|
|
private function makeTemporaryFile(string $name, string $content) |
124
|
|
|
{ |
125
|
|
|
$filename = tempnam(sys_get_temp_dir(), $name.'_'); |
126
|
|
|
file_put_contents($filename, $content); |
127
|
|
|
return $filename; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
public function __destruct() |
132
|
|
|
{ |
133
|
|
|
foreach ($this->getFilesPath() as $fileName) { |
134
|
|
|
if(is_file($fileName)) { |
135
|
|
|
unlink($fileName); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
private function getFilesPath() : array |
144
|
|
|
{ |
145
|
|
|
$paths =[]; |
146
|
|
|
foreach ($this->tempFiles as $fileName) { |
147
|
|
|
$paths[] = $fileName; |
148
|
|
|
} |
149
|
|
|
return $paths; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
private function getCommand() : string |
156
|
|
|
{ |
157
|
|
|
$command = []; |
158
|
|
|
|
159
|
|
|
if (!empty($this->prependCommand)) { |
160
|
|
|
$command[] = escapeshellcmd($this->prependCommand) . " &&"; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$command = array_merge($command, [ |
164
|
|
|
$this->editor, |
165
|
|
|
(string)$this->options, |
166
|
|
|
implode(' ', array_map(function (string $path) { |
167
|
|
|
return escapeshellarg($path); |
168
|
|
|
}, $this->getFilesPath())), |
169
|
|
|
'2> /dev/null' |
170
|
|
|
]); |
171
|
|
|
|
172
|
|
|
return implode(" ", $command); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|