|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HtaccessCapabilityTester; |
|
4
|
|
|
|
|
5
|
|
|
class SimpleTestFileLineUpper implements TestFilesLineUpperInterface |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Write missing and changed files. |
|
10
|
|
|
* |
|
11
|
|
|
* @param array $files The files that needs to be there |
|
12
|
|
|
* |
|
13
|
|
|
* @return void |
|
14
|
|
|
*/ |
|
15
|
|
|
private function writeMissingAndChangedFiles($files) |
|
16
|
|
|
{ |
|
17
|
|
|
foreach ($files as $file) { |
|
18
|
|
|
$success = true; |
|
19
|
|
|
list($filename, $content) = $file; |
|
20
|
|
|
$dir = dirname($filename); |
|
21
|
|
|
if (!is_dir($dir)) { |
|
22
|
|
|
if (!mkdir($dir, 0777, true)) { |
|
23
|
|
|
// TODO: Use custom exception |
|
24
|
|
|
throw new \Exception('Failed creating dir: ' . $dir); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
if (file_exists($filename)) { |
|
28
|
|
|
// file already exists, now check if content is the same |
|
29
|
|
|
$existingContent = file_get_contents($filename); |
|
30
|
|
|
if (($existingContent === false) || ($content != $existingContent)) { |
|
31
|
|
|
$success = file_put_contents($filename, $content); |
|
32
|
|
|
} |
|
33
|
|
|
} else { |
|
34
|
|
|
$success = file_put_contents($filename, $content); |
|
35
|
|
|
} |
|
36
|
|
|
if (!$success) { |
|
37
|
|
|
// TODO: Use custom exception |
|
38
|
|
|
throw new \Exception('Failed creating file: ' . $filename); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Remove unused files. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $files The files that needs to be there (others will be removed) |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
private function removeUnusedFiles($files) |
|
51
|
|
|
{ |
|
52
|
|
|
$dirs = []; |
|
53
|
|
|
foreach ($files as $file) { |
|
54
|
|
|
list($filename, $content) = $file; |
|
55
|
|
|
$dir = dirname($filename); |
|
56
|
|
|
if (!isset($dirs[$dir])) { |
|
57
|
|
|
$dirs[$dir] = []; |
|
58
|
|
|
} |
|
59
|
|
|
$dirs[$dir][] = basename($filename); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
foreach ($dirs as $dir => $filesSupposedToBeInDir) { |
|
63
|
|
|
$fileIterator = new \FilesystemIterator($dir); |
|
64
|
|
|
while ($fileIterator->valid()) { |
|
65
|
|
|
$filename = $fileIterator->getFilename(); |
|
66
|
|
|
if (!in_array($filename, $filesSupposedToBeInDir)) { |
|
67
|
|
|
unlink($dir . '/' . $filename); |
|
68
|
|
|
} |
|
69
|
|
|
$fileIterator->next(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Line-up test files. |
|
76
|
|
|
* |
|
77
|
|
|
* This method should make sure that the files passed in are there and are up-to-date. |
|
78
|
|
|
* - If a file is missing, it should be created. |
|
79
|
|
|
* - If a file has changed content, it should be updated |
|
80
|
|
|
* - If the directory contains a file/dir that should not be there, it should be removed |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $files The files that needs to be there |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public function lineUp($files) |
|
87
|
|
|
{ |
|
88
|
|
|
// 1. Put missing files / changed files |
|
89
|
|
|
$this->writeMissingAndChangedFiles($files); |
|
90
|
|
|
|
|
91
|
|
|
// 2. Remove unused files |
|
92
|
|
|
$this->removeUnusedFiles($files); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|