Compression
1 year ago
Database
1 year ago
AbstractBackupsFinder.php
1 year ago
AbstractExtractor.php
10 months ago
AbstractServiceProvider.php
1 year ago
Archiver.php
11 months ago
BackupAssets.php
2 years ago
BackupContent.php
1 year ago
BackupMetadataEditor.php
1 year ago
BackupMetadataReader.php
1 year ago
BackupSigner.php
1 year ago
BackupsFinder.php
1 year ago
Extractor.php
10 months ago
FileBackupService.php
11 months ago
FileBackupServiceProvider.php
1 year ago
ServiceInterface.php
1 year ago
ZlibCompressor.php
11 months ago
BackupMetadataEditor.php
39 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Service; |
| 4 | |
| 5 | use WPStaging\Framework\Filesystem\FileObject; |
| 6 | use WPStaging\Backup\Entity\BackupMetadata; |
| 7 | |
| 8 | class BackupMetadataEditor |
| 9 | { |
| 10 | /** |
| 11 | * @param FileObject $backupFile It must be opened with File::MODE_APPEND |
| 12 | * @param BackupMetadata $newMetadata |
| 13 | */ |
| 14 | public function setBackupMetadata(FileObject $backupFile, BackupMetadata $newMetadata) |
| 15 | { |
| 16 | $backupMetadataReader = new BackupMetadataReader($backupFile); |
| 17 | $existingMetadataPosition = $backupMetadataReader->getExistingMetadataPosition(); |
| 18 | |
| 19 | $backupFile->fseek($existingMetadataPosition); |
| 20 | |
| 21 | $maybeMetadataLine = $backupFile->readAndMoveNext(); |
| 22 | |
| 23 | // Validate metadata position |
| 24 | if (!is_array($backupMetadataReader->extractMetadata($maybeMetadataLine))) { |
| 25 | throw new \UnexpectedValueException('Could not find the existing metadata from the backup.'); |
| 26 | } |
| 27 | |
| 28 | $backupFile->ftruncate($existingMetadataPosition); |
| 29 | $backupFile->fseek($existingMetadataPosition); |
| 30 | |
| 31 | $prepandForSql = ''; |
| 32 | if ($backupFile->isSqlFile()) { |
| 33 | $prepandForSql = '-- '; |
| 34 | } |
| 35 | |
| 36 | $backupFile->fwrite($prepandForSql . json_encode($newMetadata) . "\n"); |
| 37 | } |
| 38 | } |
| 39 |