PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.3.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.3.0
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Filesystem / LogFiles.php
wp-staging / Framework / Filesystem Last commit date
Filters 1 year ago Scanning 5 years ago AbstractFileObject.php 1 year ago AbstractFilesystemScanner.php 1 year ago DebugLogReader.php 2 years ago DirectoryListing.php 2 years ago DiskWriteCheck.php 1 year ago FileObject.php 1 year ago Filesystem.php 1 year ago FilesystemExceptions.php 5 years ago FilesystemScanner.php 10 months ago FilesystemScannerDto.php 1 year ago FilterableDirectoryIterator.php 1 year ago LogCleanup.php 2 years ago LogFiles.php 1 year ago MissingFileException.php 3 years ago OPcache.php 2 years ago PartIdentifier.php 11 months ago PathChecker.php 2 years ago PathIdentifier.php 1 year ago Permissions.php 1 year ago WpUploadsFolderSymlinker.php 4 years ago
LogFiles.php
117 lines
1 <?php
2
3 namespace WPStaging\Framework\Filesystem;
4
5 use WPStaging\Framework\Adapter\Directory;
6
7 class LogFiles
8 {
9 /**
10 * @var string
11 */
12 private $logsDirectory;
13
14 /**
15 * @var array
16 */
17 private $availableLogFileTypes;
18
19 /**
20 * @var array
21 */
22 private $latestLogFiles;
23
24 /**
25 * @param Directory $directory
26 */
27 public function __construct(Directory $directory)
28 {
29 $this->logsDirectory = $directory->getLogDirectory();
30 $this->availableLogFileTypes = ['push', 'backup_restore', 'cloning', 'backup_job', 'staging_plugins_updater'];
31 $this->latestLogFiles = [];
32 }
33
34 /**
35 * @return array
36 */
37 public function getLatestLogFiles(): array
38 {
39 $logFiles = scandir($this->logsDirectory);
40 foreach ($logFiles as $logFile) {
41 $this->findLatestLogFiles($logFile);
42 }
43
44 return $this->latestLogFiles;
45 }
46
47 /**
48 * @param string $fileName
49 * @return void
50 */
51 private function findLatestLogFiles(string $fileName)
52 {
53 foreach ($this->availableLogFileTypes as $logFilePrefix) {
54 if (strpos($fileName, $logFilePrefix) !== 0) {
55 continue;
56 }
57
58 $logFilePath = trailingslashit($this->logsDirectory) . $fileName;
59 if (!isset($this->latestLogFiles[$logFilePrefix]) || filemtime($logFilePath) > filemtime($this->latestLogFiles[$logFilePrefix])) {
60 $this->latestLogFiles[$logFilePrefix] = $logFilePath;
61 }
62
63 break;
64 }
65 }
66
67 /**
68 * @return string
69 */
70 public function getLogsDirectory(): string
71 {
72 return trailingslashit($this->logsDirectory);
73 }
74
75 public function getAvailableLogFileTypes(): array
76 {
77 return $this->availableLogFileTypes;
78 }
79
80 /**
81 * @param int $days
82 * @return array
83 */
84 public function getRetentionLogFiles(int $days = 14): array
85 {
86 $logPrefix = implode('|', $this->availableLogFileTypes);
87 $logFiles = [];
88 $dayStart = strtotime('-' . $days);
89
90 $dirIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->logsDirectory));
91 foreach ($dirIterator as $fileInfo) {
92 if (!$fileInfo->isFile() || $fileInfo->isLink() || $fileInfo->getExtension() !== 'log') {
93 continue;
94 }
95
96 $filePath = $fileInfo->getRealPath();
97 $fileName = $fileInfo->getFilename();
98
99 if (!preg_match('@^(' . $logPrefix . ')@', $fileName, $matches)) {
100 continue;
101 }
102
103 $fileType = $matches[1];
104 $fileMTime = $fileInfo->getMTime();
105 if (!$fileMTime) {
106 continue;
107 }
108
109 if ($dayStart >= $fileMTime) {
110 $logFiles[$fileType][] = $filePath;
111 }
112 }
113
114 return $logFiles;
115 }
116 }
117