1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MagentoHackathon\Composer\Magento; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class GitIgnore |
7
|
|
|
* @package MagentoHackathon\Composer\Magento |
8
|
|
|
* @author Aydin Hassan <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class GitIgnore |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $lines = array(); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string|null |
19
|
|
|
*/ |
20
|
|
|
protected $gitIgnoreLocation; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $hasChanges = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $fileLocation |
29
|
|
|
*/ |
30
|
11 |
|
public function __construct($fileLocation) |
31
|
|
|
{ |
32
|
11 |
|
$this->gitIgnoreLocation = $fileLocation; |
33
|
11 |
|
if (file_exists($fileLocation)) { |
34
|
6 |
|
$this->lines = $this->removeDuplicates(file($fileLocation, FILE_IGNORE_NEW_LINES)); |
35
|
|
|
} |
36
|
11 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $file |
40
|
|
|
*/ |
41
|
4 |
|
public function addEntry($file) |
42
|
|
|
{ |
43
|
4 |
|
$file = $this->prependSlashIfNotExist($file); |
44
|
4 |
|
if (!in_array($file, $this->lines)) { |
45
|
4 |
|
$this->lines[] = $file; |
46
|
|
|
} |
47
|
4 |
|
$this->hasChanges = true; |
48
|
4 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $files |
52
|
|
|
*/ |
53
|
1 |
|
public function addMultipleEntries(array $files) |
54
|
|
|
{ |
55
|
1 |
|
foreach ($files as $file) { |
56
|
1 |
|
$this->addEntry($file); |
57
|
|
|
} |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $file |
62
|
|
|
*/ |
63
|
2 |
|
public function removeEntry($file) |
64
|
|
|
{ |
65
|
2 |
|
$file = $this->prependSlashIfNotExist($file); |
66
|
2 |
|
$key = array_search($file, $this->lines); |
67
|
2 |
|
if (false !== $key) { |
68
|
2 |
|
unset($this->lines[$key]); |
69
|
2 |
|
$this->hasChanges = true; |
70
|
|
|
|
71
|
|
|
// renumber array |
72
|
2 |
|
$this->lines = array_values($this->lines); |
73
|
|
|
} |
74
|
2 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $files |
78
|
|
|
*/ |
79
|
1 |
|
public function removeMultipleEntries(array $files) |
80
|
|
|
{ |
81
|
1 |
|
foreach ($files as $file) { |
82
|
1 |
|
$this->removeEntry($file); |
83
|
|
|
} |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
8 |
|
public function getEntries() |
90
|
|
|
{ |
91
|
8 |
|
return $this->lines; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Write the file |
96
|
|
|
*/ |
97
|
2 |
|
public function write() |
98
|
|
|
{ |
99
|
2 |
|
if ($this->hasChanges) { |
100
|
1 |
|
file_put_contents($this->gitIgnoreLocation, implode("\n", $this->lines)); |
101
|
|
|
} |
102
|
2 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Prepend a forward slash to a path |
106
|
|
|
* if it does not already start with one. |
107
|
|
|
* |
108
|
|
|
* @param string $file |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
6 |
|
private function prependSlashIfNotExist($file) |
112
|
|
|
{ |
113
|
6 |
|
return sprintf('/%s', ltrim($file, '/')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Removes duplicate patterns from the input array, without touching comments, line breaks etc. |
118
|
|
|
* Will remove the last duplicate pattern. |
119
|
|
|
* |
120
|
|
|
* @param array $lines |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
6 |
|
private function removeDuplicates($lines) |
124
|
|
|
{ |
125
|
|
|
// remove empty lines |
126
|
6 |
|
$duplicates = array_filter($lines); |
127
|
|
|
|
128
|
|
|
// remove comments |
129
|
|
|
$duplicates = array_filter($duplicates, function ($line) { |
130
|
6 |
|
return strpos($line, '#') !== 0; |
131
|
6 |
|
}); |
132
|
|
|
|
133
|
|
|
// check if duplicates exist |
134
|
6 |
|
if (count($duplicates) !== count(array_unique($duplicates))) { |
135
|
2 |
|
$duplicates = array_filter(array_count_values($duplicates), function ($count) { |
136
|
2 |
|
return $count > 1; |
137
|
2 |
|
}); |
138
|
|
|
|
139
|
|
|
// search from bottom to top |
140
|
2 |
|
$lines = array_reverse($lines); |
141
|
2 |
|
foreach ($duplicates as $duplicate => $count) { |
142
|
|
|
// remove all duplicates, except the first one |
143
|
2 |
|
for ($i = 1; $i < $count; $i++) { |
144
|
2 |
|
$key = array_search($duplicate, $lines); |
145
|
2 |
|
unset($lines[$key]); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// restore original order |
150
|
2 |
|
$lines = array_values(array_reverse($lines)); |
151
|
|
|
} |
152
|
|
|
|
153
|
6 |
|
return $lines; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|