View source
<?php
namespace Drupal\file\Plugin\migrate\destination;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\UriItem;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StreamWrapper\LocalStream;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityFile extends EntityContentBase {
protected $streamWrapperManager;
protected $fileSystem;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, StreamWrapperManagerInterface $stream_wrappers, FileSystemInterface $file_system) {
$configuration += array(
'source_base_path' => '',
'source_path_property' => 'filepath',
'destination_path_property' => 'uri',
'move' => FALSE,
'urlencode' => FALSE,
);
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager);
$this->streamWrapperManager = $stream_wrappers;
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$entity_type = static::getEntityTypeId($plugin_id);
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('entity.manager')
->getStorage($entity_type), array_keys($container
->get('entity.manager')
->getBundleInfo($entity_type)), $container
->get('entity.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('stream_wrapper_manager'), $container
->get('file_system'));
}
protected function getEntity(Row $row, array $old_destination_id_values) {
if ($row
->isStub()) {
return parent::getEntity($row, $old_destination_id_values);
}
$destination = $row
->getDestinationProperty($this->configuration['destination_path_property']);
$entity = $this->storage
->loadByProperties([
'uri' => $destination,
]);
if ($entity) {
return reset($entity);
}
else {
return parent::getEntity($row, $old_destination_id_values);
}
}
public function import(Row $row, array $old_destination_id_values = array()) {
if ($row
->isStub()) {
return parent::import($row, $old_destination_id_values);
}
$file = $row
->getSourceProperty($this->configuration['source_path_property']);
$destination = $row
->getDestinationProperty($this->configuration['destination_path_property']);
$source = $this->configuration['source_base_path'] . $file;
if ($this
->isLocalUri($source) && !file_exists($source)) {
throw new MigrateException("File '{$source}' does not exist.");
}
if ($this
->isLocationUnchanged($source, $destination)) {
return parent::import($row, $old_destination_id_values);
}
$replace = $this
->getOverwriteMode($row);
$success = $this
->writeFile($source, $destination, $replace);
if (!$success) {
$dir = $this
->getDirectory($destination);
if (file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
$success = $this
->writeFile($source, $destination, $replace);
}
else {
throw new MigrateException("Could not create directory '{$dir}'");
}
}
if ($success) {
return parent::import($row, $old_destination_id_values);
}
else {
throw new MigrateException("File {$source} could not be copied to {$destination}.");
}
}
protected function writeFile($source, $destination, $replace = FILE_EXISTS_REPLACE) {
if ($this->configuration['move']) {
return (bool) file_unmanaged_move($source, $destination, $replace);
}
else {
$destination = file_destination($destination, $replace);
$source = $this
->urlencode($source);
return @copy($source, $destination);
}
}
protected function getOverwriteMode(Row $row) {
if (!empty($this->configuration['rename'])) {
$entity_id = $row
->getDestinationProperty($this
->getKey('id'));
if ($entity_id && ($entity = $this->storage
->load($entity_id))) {
return FILE_EXISTS_RENAME;
}
}
return FILE_EXISTS_REPLACE;
}
protected function getDirectory($uri) {
$dir = $this->fileSystem
->dirname($uri);
if (substr($dir, -3) == '://') {
return $this->fileSystem
->realpath($dir);
}
else {
return $dir;
}
}
protected function isLocationUnchanged($source, $destination) {
if ($this
->isLocalUri($source) && $this
->isLocalUri($destination)) {
return $this->fileSystem
->realpath($source) === $this->fileSystem
->realpath($destination);
}
else {
return FALSE;
}
}
protected function isLocalUri($uri) {
$scheme = $this->fileSystem
->uriScheme($uri);
return $scheme === FALSE || $this->streamWrapperManager
->getViaScheme($scheme) instanceof LocalStream;
}
protected function urlencode($filename) {
if ($this->configuration['urlencode'] && strpos($filename, '://')) {
$components = explode('/', $filename);
foreach ($components as $key => $component) {
$components[$key] = rawurlencode($component);
}
$filename = implode('/', $components);
$filename = str_replace('%3A', ':', $filename);
$filename = str_replace('%3F', '?', $filename);
$filename = str_replace('%26', '&', $filename);
}
return $filename;
}
protected function processStubRow(Row $row) {
if (!$row
->getDestinationProperty('uri')) {
$field_definitions = $this->entityManager
->getFieldDefinitions($this->storage
->getEntityTypeId(), $this
->getKey('bundle'));
$value = UriItem::generateSampleValue($field_definitions['uri']);
if (empty($value)) {
throw new MigrateException('Stubbing failed, unable to generate value for field uri');
}
$value = reset($value);
$value = 'public://' . preg_replace('|^[a-z]+://|i', '', $value);
$value = Unicode::substr($value, 0, $field_definitions['uri']
->getSetting('max_length'));
touch($value);
$row
->setDestinationProperty('uri', $value);
}
parent::processStubRow($row);
}
}