Filters
6 months ago
Scanning
5 years ago
AbstractFileObject.php
1 year ago
AbstractFilesystemScanner.php
2 months ago
DebugLogReader.php
2 years ago
DirectoryListing.php
5 months ago
DiskWriteCheck.php
5 months ago
FileObject.php
1 year ago
Filesystem.php
6 months ago
FilesystemExceptions.php
5 years ago
FilesystemScanner.php
2 months ago
FilesystemScannerDto.php
1 year ago
FilterableDirectoryIterator.php
1 year ago
LogCleanup.php
5 months ago
LogFiles.php
1 year ago
MissingFileException.php
3 years ago
OPcache.php
5 months ago
PartIdentifier.php
8 months ago
PathChecker.php
2 years ago
PathIdentifier.php
6 months ago
Permissions.php
5 months ago
WpUploadsFolderSymlinker.php
2 months 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 |