1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ecodev\Felix\Service; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Exception; |
9
|
|
|
use PDO; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Check missing files on disk and non-needed files on disk. |
13
|
|
|
* |
14
|
|
|
* It is up to the user to then take appropriate action based on that information. |
15
|
|
|
*/ |
16
|
|
|
class FileChecker |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Connection |
20
|
|
|
*/ |
21
|
|
|
private $connection; |
22
|
|
|
|
23
|
|
|
public function __construct(Connection $connection) |
24
|
|
|
{ |
25
|
|
|
$this->connection = $connection; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Print the result |
30
|
|
|
* |
31
|
|
|
* @param array $config must be $table => $basePath |
32
|
|
|
*/ |
33
|
|
|
public function check(array $config): void |
34
|
|
|
{ |
35
|
|
|
$filesInDb = $this->fetchFromDb($config); |
36
|
|
|
$filesOnDisk = $this->readDisk($config); |
37
|
|
|
|
38
|
|
|
$missingFiles = array_diff($filesInDb, $filesOnDisk); |
39
|
|
|
$unneededFiles = array_diff($filesOnDisk, $filesInDb); |
40
|
|
|
|
41
|
|
|
$this->printFiles('List of missing files on disk:', $missingFiles); |
42
|
|
|
$this->printFiles('List of unneeded files on disk:', $unneededFiles); |
43
|
|
|
|
44
|
|
|
echo ' |
45
|
|
|
Total files in DB : ' . count($filesInDb) . ' |
46
|
|
|
Total files on disk : ' . count($filesOnDisk) . ' |
47
|
|
|
Missing files on disk : ' . count($missingFiles) . ' |
48
|
|
|
Unneeded files on disk: ' . count($unneededFiles) . ' |
49
|
|
|
'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function fetchFromDb(array $config): array |
53
|
|
|
{ |
54
|
|
|
$queries = []; |
55
|
|
|
foreach ($config as $table => $basePath) { |
56
|
|
|
$q = 'SELECT DISTINCT CONCAT(' . $this->connection->quote($basePath) . ', filename) FROM ' . $this->connection->quoteIdentifier($table) . ' WHERE filename != "" ORDER BY filename'; |
57
|
|
|
$queries[] = '(' . $q . ')'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$query = implode(' UNION ', $queries); |
61
|
|
|
|
62
|
|
|
return $this->connection->executeQuery($query)->fetchAll(PDO::FETCH_COLUMN); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function readDisk(array $config): array |
66
|
|
|
{ |
67
|
|
|
$files = []; |
68
|
|
|
|
69
|
|
|
foreach ($config as $basePath) { |
70
|
|
|
$filesFound = glob($basePath . '*'); |
71
|
|
|
if ($filesFound === false) { |
72
|
|
|
throw new Exception('Could not glob path: ' . $basePath); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$files = array_merge($files, $filesFound); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
sort($files); |
79
|
|
|
|
80
|
|
|
return $files; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Print a list of files if non empty |
85
|
|
|
*/ |
86
|
|
|
private function printFiles(string $title, array $files): void |
87
|
|
|
{ |
88
|
|
|
if (!$files) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
echo $title . PHP_EOL . PHP_EOL; |
93
|
|
|
|
94
|
|
|
foreach ($files as $file) { |
95
|
|
|
echo ' ' . escapeshellarg($file) . PHP_EOL; |
96
|
|
|
} |
97
|
|
|
echo PHP_EOL; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|