Skip to content

Commit

Permalink
Refactorizes export/import porcess
Browse files Browse the repository at this point in the history
  • Loading branch information
david4lim authored and david-nova-glb committed Aug 30, 2017
1 parent b5bf4fb commit c76500c
Show file tree
Hide file tree
Showing 32 changed files with 2,157 additions and 148 deletions.
12 changes: 8 additions & 4 deletions content_sync.batch.inc
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,17 @@ function processContentSyncSnapshot($files, &$context) {
}
else {
// Store the data for diff
$entity = _content_sync_db_to_entity($entity_type,$entity_bundle,$entity_id);
$entity = $entityTypeManager->getStorage($entity_type)
->load($entity_id);
// Generate the YAML file.
$serializer_context = [];
$exported_entity = \Drupal::service('content_sync.exporter')->exportEntity($entity, $serializer_context);
// Create the name
$name = $entity_type . "." . $entity_bundle . "." . $entity['values'][0]['uuid'][0]['value'];
$name = $entity_type . "." . $entity->bundle() . "." . $entity->uuid();

// Insert Data
$activeStorage = new Drupal\Core\Config\DatabaseStorage(\Drupal::database(), 'cs_db_snapshot');
$activeStorage->write($name, $entity);
$activeStorage->write($name, Yaml::decode($exported_entity));
$context['message'] = $name;
$context['results'][] = $name;
}
Expand Down Expand Up @@ -772,4 +776,4 @@ function generateSiteUUIDFile($data, &$context) {
$context['message'] = $name;
$context['results'][] = $name;
$context['finished'] = 1;
}
}
2 changes: 2 additions & 0 deletions content_sync.info.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ description: 'Allows to import/export content using YAML files'
package: Custom
core: 8.x
configure: content.sync
dependencies:
- serialization
12 changes: 8 additions & 4 deletions content_sync.module
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function content_sync_get_content_directory($type) {
function content_sync_entity_update(Drupal\Core\Entity\EntityInterface $entity){
// Get submitted values
$entity_type = $entity->getEntityTypeId();
$entity_bundle = $entity->Bundle();
$entity_bundle = $entity->bundle();
$entity_id = $entity->id();

//Validate that it is a Content Entity
Expand All @@ -212,13 +212,17 @@ function content_sync_entity_update(Drupal\Core\Entity\EntityInterface $entity){
if ( isset($instances[$entity_type]) && $instances[$entity_type] instanceof ContentEntityType ) {
// Store the data for diff
module_load_include('inc', 'content_sync', 'content_sync.batch');
$new_entity = _content_sync_db_to_entity($entity_type,$entity_bundle,$entity_id);
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)
->load($entity_id);
// Generate the YAML file.
$serializer_context = [];
$exported_entity = \Drupal::service('content_sync.exporter')->exportEntity($entity, $serializer_context);
// Create the name
$name = $entity_type . "." . $entity_bundle . "." . $new_entity['values'][0]['uuid'][0]['value'];
$name = $entity_type . "." . $entity_bundle . "." . $entity->uuid();

//Insert/Update Data
$activeStorage = new Drupal\Core\Config\DatabaseStorage(\Drupal::database(), 'cs_db_snapshot');
$activeStorage->write($name, $new_entity);
$activeStorage->write($name, \Drupal\Core\Serialization\Yaml::decode($exported_entity));
}
}
/**
Expand Down
10 changes: 10 additions & 0 deletions content_sync.routing.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ content.export_single:
requirements:
_permission: 'export content'

content.export_multiple_confirm:
path: '/admin/config/development/content/export/confirm'
defaults:
_form: '\Drupal\content_sync\Form\ContentExportMultiple'
_title: 'Content export'
requirements:
_permission: 'export content'
options:
_admin_route: TRUE

content.overview:
path: '/admin/config/development/content/logs'
defaults:
Expand Down
31 changes: 31 additions & 0 deletions content_sync.services.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,34 @@ services:
content_sync.help_manager:
class: Drupal\content_sync\ContentSyncHelpManager
arguments: ['@current_user', '@config.factory', '@module_handler', '@state', '@path.matcher']
content_sync.exporter:
class: Drupal\content_sync\Exporter\ContentExporter
arguments: ['@serializer']
content_sync.importer:
class: Drupal\content_sync\Importer\ContentImporter
arguments: ['@serializer', '@entity_type.manager']
content_sync.manager:
class: Drupal\content_sync\ContentSyncManager
arguments: ['@serializer', '@entity_type.manager','@content_sync.exporter', '@content_sync.importer']
content_sync.normalizer.content_entity:
class: Drupal\content_sync\Normalizer\ContentEntityNormalizer
arguments: ['@entity.manager', '@plugin.manager.sync_normalizer_decorator']
tags:
- { name: normalizer, priority: 6 }
content_sync.normalizer.file_entity:
class: Drupal\content_sync\Normalizer\FileEntityNormalizer
arguments: ['@entity.manager', '@plugin.manager.sync_normalizer_decorator', '@file_system']
tags:
- { name: normalizer, priority: 7 }
content_sync.normalizer.user_entity:
class: Drupal\content_sync\Normalizer\UserEntityNormalizer
arguments: ['@entity.manager', '@plugin.manager.sync_normalizer_decorator']
tags:
- { name: normalizer, priority: 7 }
content_sync.normalizer.text_item:
class: Drupal\content_sync\Normalizer\TextItemNormalizer
tags:
- { name: normalizer, priority: 10 }
plugin.manager.sync_normalizer_decorator:
class: Drupal\content_sync\Plugin\SyncNormalizerDecoratorManager
parent: default_plugin_manager
34 changes: 34 additions & 0 deletions src/Annotation/SyncNormalizerDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Drupal\content_sync\Annotation;

use Drupal\Component\Annotation\Plugin;

/**
* Defines a Sync normalizer decorator item annotation object.
*
* @see \Drupal\content_sync\Plugin\SyncNormalizerDecoratorManager
* @see plugin_api
*
* @Annotation
*/
class SyncNormalizerDecorator extends Plugin {


/**
* The plugin ID.
*
* @var string
*/
public $id;

/**
* The label of the plugin.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $label;

}
98 changes: 98 additions & 0 deletions src/ContentSyncManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Drupal\content_sync;


use Drupal\content_sync\DependencyResolver\ImportQueueResolver;
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 {

/**
* @var \Symfony\Component\Serializer\Serializer
*/
protected $serializer;

/**
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* @var \Drupal\content_sync\Exporter\ContentExporterInterface
*/
protected $contentExporter;

/**
* @var \Drupal\content_sync\Importer\ContentImporterInterface
*/
protected $contentImporter;

/**
* ContentSyncManager constructor.
*/
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;
}

/**
* @return \Drupal\content_sync\Exporter\ContentExporterInterface
*/
public function getContentExporter() {
return $this->contentExporter;
}

/**
* @return \Drupal\content_sync\Importer\ContentImporterInterface
*/
public function getContentImporter() {
return $this->contentImporter;
}


/**
* @param $file_names
* @param $directory
*
* @return array
*/
public function generateImportQueue($file_names, $directory) {
$queue = [];
foreach ($file_names as $file) {
$file_path = $directory . "/" . $file . ".yml";
if (!file_exists($file_path)) {
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;
}

/**
* @return \Symfony\Component\Serializer\Serializer
*/
public function getSerializer() {
return $this->serializer;
}

/**
* @return \Drupal\Core\Entity\EntityTypeManagerInterface
*/
public function getEntityTypeManager() {
return $this->entityTypeManager;
}

}
33 changes: 33 additions & 0 deletions src/ContentSyncManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Drupal\content_sync;


/**
* Interface ContentSyncManagerInterface.
*
* @package Drupal\content_sync
*/
interface ContentSyncManagerInterface {

/**
* @return \Drupal\content_sync\Importer\ContentImporterInterface
*/
public function getContentImporter();

/**
* @return \Drupal\content_sync\Exporter\ContentExporterInterface
*/
public function getContentExporter();

/**
* @return \Symfony\Component\Serializer\Serializer
*/
public function getSerializer();

/**
* @return \Drupal\Core\Entity\EntityTypeManagerInterface
*/
public function getEntityTypeManager();

}
9 changes: 9 additions & 0 deletions src/DependencyResolver/ContentSyncResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Drupal\content_sync\DependencyResolver;


interface ContentSyncResolverInterface {

public function resolve(array $normalized_entities);
}
59 changes: 59 additions & 0 deletions src/DependencyResolver/ImportQueueResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Drupal\content_sync\DependencyResolver;


use Drupal\Component\Graph\Graph;

class ImportQueueResolver implements ContentSyncResolverInterface {

public function resolve(array $normalized_entities) {
$queue = [];
$graph = [];
$uuids = [];
foreach ($normalized_entities as $identifier => $entity) {
$ids = explode('.', $identifier);
list($entity_type_id, $bundle, $uuid) = $ids;
if (!empty($entity['_content_sync']['entity_dependencies'])) {
foreach ($entity['_content_sync']['entity_dependencies'] as $ref_entity_type_id => $references) {
foreach ($references as $reference) {
if (!empty($normalized_entities[$reference])) {
$graph[$identifier]['edges'][$reference] = 1;
}
}
}
}
else {
$uuids[] = $identifier;
$queue[] = [
'entity_type_id' => $entity_type_id,
'decoded_entity' => $entity,
];
}

}
$graph = new Graph($graph);
$entities = $graph->searchAndSort();
uasort($entities, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
foreach ($entities as $uuid => $vertex) {
foreach ($vertex['edges'] as $key => $value) {
if (!in_array($key, $uuids) && $uuid != $key) {
$uuids[] = $key;
$ids = explode('.', $key);
$queue[] = [
'entity_type_id' => $ids[0],
'decoded_entity' => $normalized_entities[$key],
];
}
}
$uuids[] = $uuid;
$ids = explode('.', $uuid);
$queue[] = [
'entity_type_id' => $ids[0],
'decoded_entity' => $normalized_entities[$uuid],
];
}
return $queue;
}

}
Loading

0 comments on commit c76500c

Please sign in to comment.