Skip to content

Commit

Permalink
chore: release version 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
K.I.T.T committed Apr 27, 2022
1 parent 1e3dcdc commit 8c513a0
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 12 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [1.2.0](https://github.com/AmazeeLabs/silverback-mono/compare/@-amazeelabs/[email protected]...@-amazeelabs/[email protected]) (2022-04-27)


### Features

* a way to update already imported default content ([f9d7a43](https://github.com/AmazeeLabs/silverback-mono/commit/f9d7a4329243de0adee530f8946e525f2589c569))





## [1.1.4](https://github.com/AmazeeLabs/silverback-mono/compare/@-amazeelabs/[email protected]...@-amazeelabs/[email protected]) (2022-04-06)


Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Helpers for managing the Drupal's default content.",
"type": "package",
"license": "GPL-2.0-or-later",
"version": "1.1.4",
"version": "1.2.0",
"require": {
"amazeelabs/proxy-default-content": "^1.1"
},
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@-amazeelabs/default-content",
"author": "Amazee Labs <[email protected]>",
"version": "1.1.4",
"version": "1.2.0",
"scripts": {
"version": "sync-composer-version"
},
Expand All @@ -14,5 +14,5 @@
"repository": "[email protected]:AmazeeLabs/default-content.git",
"branch": "main"
},
"gitHead": "bf77c2e91aca0a22e68e3e6fb890e728e7276a94"
"gitHead": "499f53863135ee9e8e4efc571997ca033267197f"
}
17 changes: 17 additions & 0 deletions src/AmazeeLabs/DefaultContent/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace AmazeeLabs\DefaultContent;

abstract class Base {

public static function getContentDir(string $module): string {
/** @var \Drupal\Core\Extension\ModuleExtensionList $extensionList */
$extensionList = \Drupal::service('extension.list.module');
return DRUPAL_ROOT .
DIRECTORY_SEPARATOR .
$extensionList->getPath($module) .
DIRECTORY_SEPARATOR .
'content';
}

}
10 changes: 2 additions & 8 deletions src/AmazeeLabs/DefaultContent/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@

use Drupal\Core\Entity\ContentEntityType;

abstract class Export {
abstract class Export extends Base {

public static function run(string $module, array $excludedContentEntityTypes): void {
/** @var \Drupal\Core\Extension\ModuleExtensionList $extensionList */
$extensionList = \Drupal::service('extension.list.module');
$dir = DRUPAL_ROOT .
DIRECTORY_SEPARATOR .
$extensionList->getPath($module) .
DIRECTORY_SEPARATOR .
'content';
$dir = self::getContentDir($module);
self::rrmdir($dir);
/** @var \Drupal\default_content\ExporterInterface $exporter */
$exporter = \Drupal::service('default_content.exporter');
Expand Down
97 changes: 96 additions & 1 deletion src/AmazeeLabs/DefaultContent/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,107 @@

namespace AmazeeLabs\DefaultContent;

abstract class Import {
use Drupal\Component\Utility\DiffArray;
use Drupal\Core\Serialization\Yaml;

abstract class Import extends Base {

public static function run(string $module): void {
/** @var \Drupal\default_content\ImporterInterface $importer */
$importer = \Drupal::service('default_content.importer');
$importer->importContent($module);
}

/**
* Same as "run", but also updates the already imported content.
*
* It was introduced to solve the following problem:
* There is too much of the default content, the import takes too long.
* How to solve it:
* - Put the default content into the Drupal install cache
* - Use this method instead of the "run" one
*/
public static function runWithUpdate(string $module): void {
$dir = self::getContentDir($module);
/** @var \Drupal\default_content\ExporterInterface $exporter */
$exporter = \Drupal::service('default_content.exporter');
$stats = [
'updated' => [],
'removed' => [],
];

$map = [];
if (file_exists($dir) && is_dir($dir)) {
foreach (scandir($dir) as $dirName) {
if (
$dirName !== '.' &&
$dirName !== '..' &&
is_dir($dir . DIRECTORY_SEPARATOR . $dirName) &&
!is_link($dir . DIRECTORY_SEPARATOR . $dirName)
) {
$entityType = $dirName;
foreach (scandir($dir . DIRECTORY_SEPARATOR . $dirName) as $fileName) {
if (
$dirName !== '.' &&
$dirName !== '..' &&
is_file($dir . DIRECTORY_SEPARATOR . $dirName . DIRECTORY_SEPARATOR . $fileName) &&
!is_link($dir . DIRECTORY_SEPARATOR . $dirName . DIRECTORY_SEPARATOR . $fileName)
) {
[$uuid, $extension] = explode('.', $fileName);
if ($extension === 'yml') {
$map[$entityType][$uuid] = $dir . DIRECTORY_SEPARATOR . $dirName . DIRECTORY_SEPARATOR . $fileName;
}
}
}
}
}
}

// Find entities which need an update.
foreach ($map as $entityType => $data) {
/** @var \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository */
$entityRepository = \Drupal::service('entity.repository');
foreach ($data as $uuid => $path) {
$entity = $entityRepository->loadEntityByUuid($entityType, $uuid);
if ($entity) {
$imported = $exporter->exportContentWithReferences($entityType, $entity->id());
$imported = reset($imported);
$imported = reset($imported);
$exported = file_get_contents($path);
if ($imported !== $exported) {
$imported = Yaml::decode($imported);
$exported = Yaml::decode($exported);
$diff = DiffArray::diffAssocRecursive($imported, $exported);
if ($diff) {
// Instead of messing with the entity update, we delete it.
$entity->delete();
if (!isset($stats['updated'][$entityType])) {
$stats['updated'][$entityType] = 0;
}
$stats['updated'][$entityType]++;
}
}
}
}

// Delete entities not existing in the exported content.
$uuidKey = \Drupal::entityTypeManager()->getDefinition($entityType)->getKey('uuid') ?: 'uuid';
$query = \Drupal::entityQuery($entityType)->condition($uuidKey, array_keys($data), 'NOT IN');
if ($entityType === 'user') {
$query->condition('uid', [0, 1], 'NOT IN');
}
$ids = $query->execute();
if ($ids) {
$storage = \Drupal::entityTypeManager()->getStorage($entityType);
$storage->delete($storage->loadMultiple($ids));
$stats['removed'][$entityType] = count($ids);
}
}

echo "Default content update stats:\n";
var_dump($stats);

self::run($module);
}

}

0 comments on commit 8c513a0

Please sign in to comment.