View source
<?php
namespace Drupal\content_sync\Normalizer;
use Drupal\content_sync\Plugin\SyncNormalizerDecoratorManager;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Component\Render\FormattableMarkup;
class FileEntityNormalizer extends ContentEntityNormalizer {
protected $supportedInterfaceOrClass = 'Drupal\\file\\FileInterface';
protected $fileSystem;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityRepositoryInterface $entity_repository, SyncNormalizerDecoratorManager $decorator_manager, FileSystemInterface $file_system) {
parent::__construct($entity_type_manager, $entity_type_repository, $entity_field_manager, $entity_type_bundle_info, $entity_repository, $decorator_manager);
$this->fileSystem = $file_system;
}
public function denormalize($data, $class, $format = NULL, array $serializer_context = array()) {
$file_data = '';
if (!empty($data['data'][0]['value'])) {
$file_data = $data['data'][0]['value'];
unset($data['data']);
}
if (!empty($serializer_context['content_sync_directory_files'])) {
$scheme = \Drupal::service('stream_wrapper_manager')
->getScheme($data['uri'][0]['value']);
if (!empty($scheme)) {
$source_path = realpath($serializer_context['content_sync_directory_files']) . '/' . $scheme . '/';
$source = str_replace($scheme . '://', $source_path, $data['uri'][0]['value']);
if (file_exists($source)) {
$file = $this->fileSystem
->realpath($data['uri'][0]['value']);
if (!file_exists($file) || md5_file($file) !== md5_file($source)) {
$dir = $this->fileSystem
->dirname($data['uri'][0]['value']);
$this->fileSystem
->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY);
$uri = $this->fileSystem
->copy($source, $data['uri'][0]['value']);
$data['uri'] = [
[
'value' => $uri,
'url' => str_replace($GLOBALS['base_url'], '', file_create_url($uri)),
],
];
$file_data = '';
}
}
}
}
$entity = parent::denormalize($data, $class, $format, $serializer_context);
if ($file_data) {
$file_contents = base64_decode($file_data);
$dirname = $this->fileSystem
->dirname($entity
->getFileUri());
$this->fileSystem
->prepareDirectory($dirname, FileSystemInterface::CREATE_DIRECTORY);
if ($uri = $this->fileSystem
->saveData($file_contents, $entity
->getFileUri())) {
$entity
->setFileUri($uri);
}
else {
throw new \RuntimeException(new FormattableMarkup('Failed to write @filename.', [
'@filename' => $entity
->getFilename(),
]));
}
}
return $entity;
}
public function normalize($object, $format = NULL, array $serializer_context = array()) {
$data = parent::normalize($object, $format, $serializer_context);
if (!empty($serializer_context['content_sync_directory_files'])) {
$uri = $object
->getFileUri();
$scheme = \Drupal::service('stream_wrapper_manager')
->getScheme($uri);
$destination = "{$serializer_context['content_sync_directory_files']}/{$scheme}/";
$destination = str_replace($scheme . '://', $destination, $uri);
$this->fileSystem
->prepareDirectory($this->fileSystem
->dirname($destination), FileSystemInterface::CREATE_DIRECTORY);
$this->fileSystem
->copy($uri, $destination, FileSystemInterface::EXISTS_REPLACE);
}
if (!empty($serializer_context['content_sync_file_base_64'])) {
$file_data = base64_encode(file_get_contents($object
->getFileUri()));
$data['data'] = [
[
'value' => $file_data,
],
];
}
return $data;
}
}