View source
<?php
declare (strict_types=1);
namespace Drupal\migrate_spreadsheet\Plugin\migrate\source;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate_spreadsheet\SpreadsheetIterator;
use Drupal\migrate_spreadsheet\SpreadsheetIteratorInterface;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Spreadsheet extends SourcePluginBase implements ConfigurableInterface, DependentPluginInterface, ContainerFactoryPluginInterface {
protected $fileSystem;
protected $spreadsheetIterator;
protected $iteratorIsInitialized = FALSE;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, FileSystemInterface $file_system, SpreadsheetIteratorInterface $spreadsheet_iterator) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
$this
->setConfiguration($configuration);
$this->fileSystem = $file_system;
$this->spreadsheetIterator = $spreadsheet_iterator;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) : self {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('file_system'), new SpreadsheetIterator());
}
public function defaultConfiguration() : array {
return [
'file' => NULL,
'worksheet' => NULL,
'origin' => 'A2',
'header_row' => NULL,
'columns' => [],
'keys' => [],
'row_index_column' => NULL,
];
}
public function setConfiguration(array $configuration) : void {
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
}
public function getConfiguration() : array {
return $this->configuration;
}
public function __toString() : string {
return $this->configuration['file'] . ':' . $this->configuration['worksheet'];
}
public function getIds() : array {
$config = $this
->getConfiguration();
if (empty($config['keys'])) {
if (empty($config['row_index_column'])) {
throw new \RuntimeException("Row index should act as key but no name has been provided. Set 'row_index_column' in source config to provide a name for this column.");
}
return [
$config['row_index_column'] => [
'type' => 'integer',
],
];
}
return $config['keys'];
}
public function fields() : array {
if (!($columns = $this
->getConfiguration()['columns'])) {
$this
->initializeIterator();
$columns = array_keys($this->spreadsheetIterator
->getHeaders());
}
if ($row_index_column = $this
->getConfiguration()['row_index_column']) {
$columns[] = $row_index_column;
}
return array_combine($columns, $columns);
}
public function initializeIterator() : SpreadsheetIteratorInterface {
if (!$this->iteratorIsInitialized) {
$configuration = $this
->getConfiguration();
$configuration['worksheet'] = $this
->loadWorksheet();
$configuration['keys'] = array_keys($configuration['keys']);
unset($configuration['file'], $configuration['plugin']);
$this->spreadsheetIterator
->setConfiguration($configuration);
$this->iteratorIsInitialized = TRUE;
}
return $this->spreadsheetIterator;
}
protected function loadWorksheet() : Worksheet {
$config = $this
->getConfiguration();
if (!file_exists($config['file'])) {
throw new MigrateException("File with path '{$config['file']}' doesn't exist.");
}
if (empty($config['worksheet'])) {
throw new MigrateException('No worksheet was passed.');
}
try {
$file_path = $this->fileSystem
->realpath($config['file']);
$type = IOFactory::identify($file_path);
$reader = IOFactory::createReader($type);
$reader
->setReadDataOnly(TRUE);
$reader
->setLoadSheetsOnly($config['worksheet']);
$workbook = $reader
->load($file_path);
return $workbook
->getSheet(0);
} catch (\Exception $e) {
$class = get_class($e);
throw new MigrateException("Got '{$class}', message '{$e->getMessage()}'.");
}
}
public function calculateDependencies() : array {
return [];
}
}