1 | <?php |
||
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 | 6 | } |
|
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 | 4 | } |
|
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 | 1 | } |
|
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 | 2 | } |
|
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 | 1 | } |
|
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 | 1 | } |
|
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) |
|
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) |
|
155 | } |
||
156 |