Browse Source

Completed multi-namespace file export.

Frederic G. MARAND 7 years ago
parent
commit
c9498af96a
3 changed files with 42 additions and 16 deletions
  1. 1 0
      reinstall.services.yml
  2. 6 7
      src/Dumper.php
  3. 35 9
      src/EventSubscriber/FilePostDump.php

+ 1 - 0
reinstall.services.yml

@@ -16,6 +16,7 @@ services:
   reinstall.post_dump.file:
     class: 'Drupal\reinstall\EventSubscriber\FilePostDump'
     arguments:
+      - '@file_system'
       - '%reinstall.path%'
     tags:
       - { name: 'event_subscriber' }

+ 6 - 7
src/Dumper.php

@@ -198,7 +198,7 @@ class Dumper {
         $eventPre = new DumperEvent($storage, $bundleName, $bundleEntities);
         $this->eventDispatcher->dispatch(DumperEvents::PRE_DUMP, $eventPre);
 
-        $this->dumpEntities($bundleEntities, $entityTypeName, $bundleName);
+        $this->dumpEntities($entityTypeName, $bundleName, $bundleEntities);
 
         // Allow extra work after exporting, like copying files.
         $eventPost = new DumperEvent($storage, $bundleName, $bundleEntities);
@@ -211,17 +211,16 @@ class Dumper {
   /**
    * Generate import YAML for entities.
    *
-   * @param array $entities
-   *   The entities.
    * @param string $entityTypeName
    *   The entity type.
-   * @param string $bundle
+   * @param string $bundleName
    *   The bundle name.
+   * @param array $entities
+   *   The entities.
    */
-  public function dumpEntities(array $entities, string $entityTypeName, string $specifiedBundle = NULL) {
-    $bundle = $specifiedBundle ?: $entityTypeName;
+  public function dumpEntities(string $entityTypeName, string $bundleName, array $entities) {
     $array = $this->toArray($entities);
-    $path = $this->prepareDestination($entityTypeName, $bundle);
+    $path = $this->prepareDestination($entityTypeName, $bundleName);
     file_put_contents($path, Yaml::dump($array, static::INLINE_DEPTH, 2));
   }
 

+ 35 - 9
src/EventSubscriber/FilePostDump.php

@@ -2,20 +2,47 @@
 
 namespace Drupal\reinstall\EventSubscriber;
 
-
-use Drupal\reinstall\Dumper;
+use Drupal\Component\Utility\Unicode;
+use Drupal\Core\File\FileSystem;
+use Drupal\Core\File\FileSystemInterface;
+use Drupal\file\Entity\File;
 use Drupal\reinstall\DumperEvent;
 use Drupal\reinstall\DumperEvents;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
+/**
+ * Class FilePostDump performs physical file copy on file dump.
+ */
 class FilePostDump implements EventSubscriberInterface {
 
-  protected $importPath = Dumper::IMPORT_PATH;
+  /**
+   * The file_system service.
+   *
+   * @var \Drupal\Core\File\FileSystemInterface
+   */
+  protected $fileSystem;
 
-  public function __construct(string $importPath) {
+  /**
+   * The export/import path.
+   *
+   * @var string
+   */
+  protected $importPath;
+
+  /**
+   * FilePostDump constructor.
+   *
+   * @param string $importPath
+   *   The reinstall.path parameter.
+   */
+  public function __construct(FileSystemInterface $fileSystem, string $importPath) {
+    $this->fileSystem = $fileSystem;
     $this->importPath = $importPath;
   }
 
+  /**
+   * {@inheritdoc}
+   */
   public static function getSubscribedEvents() {
     return [
       DumperEvents::POST_DUMP => 'onDumpPost',
@@ -23,18 +50,16 @@ class FilePostDump implements EventSubscriberInterface {
   }
 
   public function onDumpPost(DumperEvent $event) {
-    $args = func_get_args();
     if ($event->storage->getEntityTypeId() !== 'file') {
       return;
     }
 
-    echo "coucou" . $event->bundleName . "\n";
-    static::dumpFiles($event->bundleName, $event->entities);
+    $this->dumpFiles($event->bundleName, $event->entities);
   }
 
-  public function dumpFiles(string $bundle, array $files) {
+  public function dumpFiles(string $bundleName, array $files) {
     $importPath = $this->importPath;
-    $dir = "$importPath/file";
+    $dir = "$importPath/$bundleName";
     $usedNamespaces = array_keys(array_reduce($files, [__CLASS__, 'namespacesReducer'], []));
     $lists = [];
 
@@ -62,6 +87,7 @@ class FilePostDump implements EventSubscriberInterface {
     foreach ($files as $fid => $file) {
       $uri = $file->getFileUri();
       $target = file_uri_target($uri);
+      $ns = $this->fileSystem->uriScheme($uri);
       fputs($lists[$ns]['handle'], $target . "\n");
       $dest = $lists[$ns]['dir'] . '/' . $target;