SidecarMetadataDestinationTrait.php in Backup and Migrate 5.0.x
File
src/Core/Destination/SidecarMetadataDestinationTrait.php
View source
<?php
namespace Drupal\backup_migrate\Core\Destination;
use Drupal\backup_migrate\Core\Plugin\FileProcessorTrait;
use Drupal\backup_migrate\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 saveTheFileMetadata(BackupFileInterface $file) {
$meta = $file
->getMetaAll();
$ini = $this
->arrayToIni($meta);
$meta_file = $this
->getTempFileManager()
->pushExt($file, 'info');
$meta_file
->write($ini);
$this
->saveTheFile($meta_file);
}
public function deleteFile($id) {
$this
->deleteTheFile($id);
$this
->deleteTheFile($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;
}
}