forked from sean-e-dietrich/content_sync
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5bf4fb
commit c76500c
Showing
32 changed files
with
2,157 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.