1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_afterload\Manage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_afterload\AfterloadException; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class Files |
10
|
|
|
{ |
11
|
|
|
protected string $enabledPath = ''; |
12
|
|
|
protected string $disabledPath = ''; |
13
|
|
|
protected FileInput $fileInput; |
14
|
|
|
/** @var FileInput[] */ |
15
|
|
|
protected array $files = []; |
16
|
|
|
protected int $highest = 0; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string[] $manageEnabledPath |
20
|
|
|
* @param string[] $manageDisabledPath |
21
|
|
|
*/ |
22
|
6 |
|
public function __construct(array $manageEnabledPath, array $manageDisabledPath) |
23
|
|
|
{ |
24
|
6 |
|
$this->fileInput = new FileInput(); |
25
|
6 |
|
$this->enabledPath = strval(realpath(implode(DIRECTORY_SEPARATOR, $manageEnabledPath))); |
26
|
6 |
|
$this->disabledPath = strval(realpath(implode(DIRECTORY_SEPARATOR, $manageDisabledPath))); |
27
|
6 |
|
$this->loadFiles(); |
28
|
6 |
|
} |
29
|
|
|
|
30
|
6 |
|
protected function loadFiles(): void |
31
|
|
|
{ |
32
|
6 |
|
$this->files = []; |
33
|
6 |
|
if ($this->enabledPath) { |
34
|
6 |
|
$items = scandir($this->enabledPath); |
35
|
6 |
|
if (false !== $items) { |
36
|
6 |
|
foreach ($items as $item) { |
37
|
6 |
|
if (is_file($this->enabledPath . DIRECTORY_SEPARATOR . $item) && ('.' != $item[0])) { |
38
|
1 |
|
$fileInput = clone $this->fileInput; |
39
|
1 |
|
$fileInput->setData($this->enabledPath, $item, true); |
40
|
1 |
|
$this->files[] = $fileInput; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
6 |
|
if ($this->disabledPath) { |
46
|
6 |
|
$items = scandir($this->disabledPath); |
47
|
6 |
|
if (false !== $items) { |
48
|
6 |
|
foreach ($items as $item) { |
49
|
6 |
|
if (is_file($this->disabledPath . DIRECTORY_SEPARATOR . $item) && ('.' != $item[0])) { |
50
|
4 |
|
$fileInput = clone $this->fileInput; |
51
|
4 |
|
$fileInput->setData($this->disabledPath, $item, false); |
52
|
4 |
|
$this->files[] = $fileInput; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
6 |
|
foreach ($this->files as $file) { |
58
|
4 |
|
$this->highest = max($file->filePosition, $this->highest); |
59
|
|
|
} |
60
|
6 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return FileInput[] |
64
|
|
|
*/ |
65
|
6 |
|
public function getFiles(): array |
66
|
|
|
{ |
67
|
6 |
|
return $this->files; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $name |
72
|
|
|
* @param string $content |
73
|
|
|
* @throws AfterloadException |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
5 |
|
public function addInput(string $name, string $content): self |
77
|
|
|
{ |
78
|
5 |
|
$fileInfo = clone $this->fileInput; |
79
|
5 |
|
$fileInfo->setData($this->disabledPath, $name, false); |
80
|
5 |
|
if (false === @file_put_contents($fileInfo->getFullName(), $content)) { |
81
|
1 |
|
throw new AfterloadException(sprintf('Cannot write file %s', $name)); |
82
|
|
|
} |
83
|
5 |
|
$this->files[] = $fileInfo; |
84
|
5 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $name |
89
|
|
|
* @throws AfterloadException |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
4 |
|
public function getInput(string $name): string |
93
|
|
|
{ |
94
|
4 |
|
$found = $this->searchFile($name); |
95
|
3 |
|
$content = @file_get_contents($found->getFullName()); |
96
|
3 |
|
if (false === $content) { |
97
|
1 |
|
throw new AfterloadException(sprintf('File %s cannot read', $name)); |
98
|
|
|
} |
99
|
2 |
|
return $content; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $name |
104
|
|
|
* @param string $content |
105
|
|
|
* @param bool $withUnlink |
106
|
|
|
* @throws AfterloadException |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
3 |
|
public function updateInput(string $name, string $content, bool $withUnlink = true): self |
110
|
|
|
{ |
111
|
3 |
|
$found = $this->searchFile($name); |
112
|
3 |
|
if ($withUnlink && !@unlink($found->getFullName())) { |
113
|
|
|
// @codeCoverageIgnoreStart |
114
|
|
|
throw new AfterloadException(sprintf('Cannot remove old file %s', $name)); |
115
|
|
|
// @codeCoverageIgnoreEnd |
116
|
|
|
} |
117
|
3 |
|
if (false === @file_put_contents($found->getFullName(), $content)) { |
118
|
1 |
|
throw new AfterloadException(sprintf('Cannot write file %s', $name)); |
119
|
|
|
} |
120
|
2 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
5 |
|
public function removeInput(string $name): self |
124
|
|
|
{ |
125
|
|
|
// need also position, so extra |
126
|
5 |
|
$found = null; |
127
|
5 |
|
$position = null; |
128
|
5 |
|
foreach ($this->files as $pos => $file) { |
129
|
5 |
|
if ($name == $file->fileName) { |
130
|
5 |
|
$found = $file; |
131
|
5 |
|
$position = $pos; |
132
|
5 |
|
break; |
133
|
|
|
} |
134
|
|
|
} |
135
|
5 |
|
if ($found) { |
136
|
5 |
|
unlink($found->getFullName()); |
137
|
5 |
|
unset($this->files[$position]); |
138
|
|
|
} |
139
|
5 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $name |
144
|
|
|
* @throws AfterloadException |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
2 |
|
public function enable(string $name): self |
148
|
|
|
{ |
149
|
2 |
|
$found = $this->searchFile($name); |
150
|
2 |
|
if (!$found->enabled) { |
151
|
2 |
|
$this->highest = $found->filePosition = $this->highest + 1; |
152
|
2 |
|
rename($found->targetDir . DIRECTORY_SEPARATOR . $found->fileName, $this->enabledPath . DIRECTORY_SEPARATOR . $found->getName()); |
153
|
2 |
|
$found->targetDir = $this->enabledPath; |
154
|
2 |
|
$found->enabled = true; |
155
|
|
|
} |
156
|
2 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $name |
161
|
|
|
* @throws AfterloadException |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
2 |
|
public function disable(string $name): self |
165
|
|
|
{ |
166
|
2 |
|
$found = $this->searchFile($name); |
167
|
2 |
|
if ($found->enabled) { |
168
|
2 |
|
rename($found->getFullName(), $this->disabledPath . DIRECTORY_SEPARATOR . $found->fileName); |
169
|
2 |
|
$found->filePosition = 0; |
170
|
2 |
|
$found->targetDir = $this->disabledPath; |
171
|
2 |
|
$found->enabled = false; |
172
|
|
|
} |
173
|
2 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $name |
178
|
|
|
* @throws AfterloadException |
179
|
|
|
* @return FileInput |
180
|
|
|
*/ |
181
|
5 |
|
protected function &searchFile(string $name): FileInput |
182
|
|
|
{ |
183
|
5 |
|
foreach ($this->files as $file) { |
184
|
4 |
|
if ($name == $file->fileName) { |
185
|
4 |
|
return $file; |
186
|
|
|
} |
187
|
|
|
} |
188
|
1 |
|
throw new AfterloadException(sprintf('File %s not found', $name)); |
189
|
|
|
} |
190
|
|
|
} |