SidecarMetadataDestinationTrait.php in Backup and Migrate 8.4
File
lib/backup_migrate_core/src/Destination/SidecarMetadataDestinationTrait.php
View source
<?php
namespace BackupMigrate\Core\Destination;
use BackupMigrate\Core\Plugin\FileProcessorTrait;
use BackupMigrate\Core\File\BackupFileInterface;
trait SidecarMetadataDestinationTrait {
use FileProcessorTrait;
protected function _loadFileMetadataArray(BackupFileInterface $file) {
$info = [];
$id = $file
->getFullName();
$filename = $id . '.info';
if ($this
->fileExists($filename)) {
$meta_file = $this
->getFile($filename);
$meta_file = $this
->loadFileForReading($meta_file);
$info = $this
->_INIToArray($meta_file
->readAll());
}
return $info;
}
protected function _saveFileMetadata(BackupFileInterface $file) {
$meta = $file
->getMetaAll();
$ini = $this
->_arrayToINI($meta);
$meta_file = $this
->getTempFileManager()
->pushExt($file, 'info');
$meta_file
->write($ini);
$this
->_saveFile($meta_file);
}
public function deleteFile($id) {
$this
->_deleteFile($id);
$this
->_deleteFile($id . '.info');
}
protected function _INIToArray($ini) {
$out = [];
$lines = explode("\n", $ini);
foreach ($lines as $line) {
$line = trim($line);
if (substr($line, 0, 1) == ';') {
continue;
}
$matches = [];
if (preg_match('/^([^=]+)\\s?=\\s?"(.*)"$/', $line, $matches)) {
$key = $matches[1];
$val = $matches[2];
$keys = explode('[', $key);
$insert =& $out;
foreach ($keys as $part) {
$part = trim($part, ' ]');
$insert[$part] = '';
$insert =& $insert[$part];
}
$insert = $val;
}
}
return $out;
}
protected function _arrayToINI($data, $prefix = '') {
$content = "";
foreach ($data as $key => $val) {
if ($prefix) {
$key = $prefix . '[' . $key . ']';
}
if (is_array($val)) {
$content .= $this
->_arrayToINI($val, $key);
}
else {
$content .= $key . " = \"" . $val . "\"\n";
}
}
return $content;
}
}