ContentSyncManager.php in Content Synchronization 8.2
File
src/ContentSyncManager.php
View source
<?php
namespace Drupal\content_sync;
use Drupal\content_sync\DependencyResolver\ImportQueueResolver;
use Drupal\content_sync\DependencyResolver\ExportQueueResolver;
use Drupal\content_sync\Exporter\ContentExporterInterface;
use Drupal\content_sync\Importer\ContentImporterInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\Serializer\Serializer;
class ContentSyncManager implements ContentSyncManagerInterface {
const DELIMITER = '.';
protected $serializer;
protected $entityTypeManager;
protected $contentExporter;
protected $contentImporter;
public function __construct(Serializer $serializer, EntityTypeManagerInterface $entity_type_manager, ContentExporterInterface $content_exporter, ContentImporterInterface $content_importer) {
$this->serializer = $serializer;
$this->entityTypeManager = $entity_type_manager;
$this->contentExporter = $content_exporter;
$this->contentImporter = $content_importer;
}
public function getContentExporter() {
return $this->contentExporter;
}
public function getContentImporter() {
return $this->contentImporter;
}
public function generateImportQueue($file_names, $directory) {
$queue = [];
foreach ($file_names as $file) {
$ids = explode('.', $file);
list($entity_type_id, $bundle, $uuid) = $ids;
$file_path = $directory . "/" . $entity_type_id . "/" . $bundle . "/" . $file . ".yml";
if (!file_exists($file_path) || !$this
->isValidFilename($file)) {
continue;
}
$content = file_get_contents($file_path);
$format = $this->contentImporter
->getFormat();
$decoded_entity = $this->serializer
->decode($content, $format);
$decoded_entities[$file] = $decoded_entity;
}
if (!empty($decoded_entities)) {
$resolver = new ImportQueueResolver();
$queue = $resolver
->resolve($decoded_entities);
}
return $queue;
}
public function generateExportQueue($decoded_entities, $visited) {
$queue = [];
if (!empty($decoded_entities)) {
$resolver = new ExportQueueResolver();
$queue = $resolver
->resolve($decoded_entities, $visited);
}
return $queue;
}
public function getSerializer() {
return $this->serializer;
}
public function getEntityTypeManager() {
return $this->entityTypeManager;
}
protected function isValidFilename($filename) {
$parts = explode(static::DELIMITER, $filename);
return count($parts) === 3;
}
}