123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- namespace Drupal\reinstall\Plugin\migrate\source;
- use Drupal\Component\Plugin\ConfigurablePluginInterface;
- use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
- use Drupal\migrate\MigrateException;
- use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
- use Drupal\migrate\Plugin\MigrationInterface;
- use Drupal\migrate\Row;
- use Drupal\reinstall\ReinstallEvents;
- use Drupal\reinstall\SourceEvent;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\EventDispatcher\EventDispatcherInterface;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\Yaml\Exception\ParseException;
- use Symfony\Component\Yaml\Yaml;
- class ReinstallSourceBase extends SourcePluginBase implements ContainerFactoryPluginInterface, ConfigurablePluginInterface {
- use SimpleSourceTrait;
-
- protected $eventDispatcher;
-
- public $records;
- public function __construct(
- array $configuration,
- $plugin_id,
- $plugin_definition,
- \Drupal\migrate\Plugin\MigrationInterface $migration,
- EventDispatcherInterface $eventDispatcher
- ) {
- parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
- $this->eventDispatcher = $eventDispatcher;
- $this->records = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
- $eventDispatcher->dispatch(ReinstallEvents::POST_SOURCE_PARSE, new SourceEvent($this));
- }
-
- public static function create(
- ContainerInterface $container,
- array $configuration,
- $pluginId,
- $pluginDefinition,
- MigrationInterface $migration = NULL
- ) {
- $importPath = $container->getParameter('reinstall.path');
- $configuration['importPath'] = $importPath;
- $dispatcher = $container->get('event_dispatcher');
- return new static($configuration, $pluginId, $pluginDefinition, $migration, $dispatcher);
- }
-
- public function doCount() {
- return count($this->records);
- }
-
- protected function flattenRecord(array $record) {
- $row = new Row($record);
- $this->flattenRow($row);
- return $row->getSource();
- }
-
- protected function flattenRow(Row $row) {
- $source = $row->getSource();
- foreach ($source as $key => &$item_list) {
- if (is_scalar($item_list)) {
- continue;
- }
- if (count($item_list) > 1) {
- $item = $item_list;
- }
- else {
- $item = reset($item_list);
- }
- if (isset($item['target_id'])) {
- $value = $item['target_id'];
- }
- elseif (is_scalar($item) || (count($item) != 1 && !isset($item['width']) && !isset($item['pid']))) {
- $value = $item;
- }
- elseif (isset($item['value'])) {
- $value = $item['value'];
- }
-
-
- elseif (isset($item['target_id']) && !isset($item['alt']) && !isset($item['title'])) {
- $value = $item['target_id'];
- }
- elseif (isset($item['pid'])) {
- $value = $item['alias'];
- }
- else {
- $value = $item;
- }
- if (empty($item)) {
- $value = NULL;
- }
- $row->setSourceProperty($key, $value);
- }
- }
-
- protected function initializeIterator() {
- if (!isset($this->iterator)) {
- $this->iterator = new \ArrayIterator($this->records);
- }
- return $this->iterator;
- }
-
- protected function initialParse(array $configuration, string $key = NULL) {
- $this->sstEntityType = $type = $configuration['type'];
- $bundle = $configuration['bundle'];
- $baseFilePath = "${type}/${bundle}.yml";
- $importPath = $configuration['importPath'] ?? NULL;
- $filePath = "$importPath/$baseFilePath";
- $realPath = realpath($filePath);
- if (!is_file($realPath) || !is_readable($realPath)) {
- throw new MigrateException("${filePath} is not a readable file.");
- }
- try {
- $raw = file_get_contents($filePath);
- $data = Yaml::parse($raw);
- }
- catch (ParseException $e) {
- throw new MigrateException("Cannot parse the contents of ${filePath}.");
- }
- if ($key) {
- return $data[$key] ?? [];
- }
- return $data ?? [];
- }
-
- public function __toString() {
- $current = $this->getIterator()->current();
- $ret = json_encode($current, JSON_PRETTY_PRINT);
- return $ret;
- }
-
- public function getConfiguration() {
- return $this->configuration;
- }
-
- public function setConfiguration(array $configuration) {
- $this->configuration = $configuration;
- }
-
- public function defaultConfiguration() {
- return [];
- }
-
- public function calculateDependencies() {
- return [];
- }
- }
|