Test Failed
Push — master ( e43c4d...0a31ff )
by Sergey
02:26
created

Vim::__destruct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
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
     * Vim constructor.
39
     *
40
     * @param Options|NULL $options
41
     */
42
    public function __construct(Options $options = NULL)
43
    {
44
        $editor = shell_exec('which vim');
45
        if(empty($editor)) {
46
            throw new \LogicException('Install vim');
47
        }
48
        $this->editor  =  trim($editor);
49
        $this->options = $options ?? new Options();
50
    }
51
52
    /**
53
     * Открывает Vim в консоли для редактирования
54
     */
55
    public function execute()
56
    {
57
        system($this->getCommand());
58
    }
59
60
    /**
61
     * @param string $filename
62
     * @param string $content
63
     * @return Vim
64
     */
65
    public function addFileContent(string $filename, string $content) : Vim
66
    {
67
        $this->originalContent[$filename] = $content;
68
        $this->tempFiles[$filename]       = $this->makeTemporaryFile($filename, $content);
69
        
70
        return $this;
71
    }
72
73
    /**
74
     * @param string $key
75
     * @return bool
76
     */
77
    public function isChanged(string $key) : bool
78
    {
79
        return $this->originalContent[$key] !== $this->getContent($key);
80
    }
81
82
    /**
83
     * @param string $key
84
     * @return string
85
     */
86
    public function getContent(string $key) : string
87
    {
88
        return file_get_contents($this->tempFiles[$key]);
89
    }
90
91
    /**
92
     * @param string $name
93
     * @param string $content
94
     * @return resource
95
     */
96
    private function makeTemporaryFile(string $name, string $content)
97
    {
98
        $filename = tempnam(sys_get_temp_dir(), $name.'_');
99
        file_put_contents($filename, $content);
100
        return $filename;
101
    }
102
103
104
    public function __destruct()
105
    {
106
        foreach ($this->getFilesPath() as $fileName) {
107
            if(is_file($fileName)) {
108
                unlink($fileName);
109
            }
110
        }
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    private function getFilesPath() : array 
117
    {
118
        $paths =[];
119
        foreach ($this->tempFiles as $fileName) {
120
            $paths[] = $fileName;
121
        }
122
        return $paths;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    private function getCommand() : string 
129
    {
130
        return 
131
            $this->editor .  ' ' .
132
            (string) $this->options . ' ' .
133
            implode(' ', array_map(function(string $path) {
134
                return escapeshellarg($path);
135
            }, $this->getFilesPath())) . ' ' .
136
            sprintf('> /proc/%s/fd/1', posix_getpid());
137
    }
138
}