PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.5
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.5
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / htaccess-capability-tester / src / SimpleTestFileLineUpper.php

SimpleTestFileLineUpper.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.5, at vendor/rosell-dk/htaccess-capability-tester/src/SimpleTestFileLineUpper.php

100 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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