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