You are here

class AcquiaContentHubCDFCommands in Acquia Content Hub 8.2

Class AcquiaContentHubCommands.

@package Drupal\acquia_contenthub\Commands

Hierarchy

Expanded class hierarchy of AcquiaContentHubCDFCommands

1 string reference to 'AcquiaContentHubCDFCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses AcquiaContentHubCDFCommands
acquia_contenthub.commands.cdf in ./drush.services.yml
Drupal\acquia_contenthub\Commands\AcquiaContentHubCDFCommands

File

src/Commands/AcquiaContentHubCDFCommands.php, line 17

Namespace

Drupal\acquia_contenthub\Commands
View source
class AcquiaContentHubCDFCommands extends DrushCommands {

  /**
   * AcquiaContentHubCDFCommands constructor.
   */
  public function __construct() {
  }

  /**
   * Generates a CDF Document from a manifest file.
   *
   * @param string $manifest
   *   The location of the manifest file.
   *
   * @command acquia:contenthub-export-local-cdf
   * @aliases ach-elc
   *
   * @return false|string
   *   The json output if successful or false.
   *
   * @throws \Exception
   */
  public function exportCdf($manifest) {
    if (!file_exists($manifest)) {
      throw new \Exception("The provided manifest file does not exist in the specified location.");
    }
    $manifest = Yaml::decode(file_get_contents($manifest));
    $entities = [];
    $entityTypeManager = \Drupal::entityTypeManager();

    /** @var \Drupal\Core\Entity\EntityRepositoryInterface $repository */
    $repository = \Drupal::service('entity.repository');
    foreach ($manifest['entities'] as $entity) {
      [
        $entity_type_id,
        $entity_id,
      ] = explode(":", $entity);
      if (!Uuid::isValid($entity_id)) {
        $entities[] = $entityTypeManager
          ->getStorage($entity_type_id)
          ->load($entity_id);
        continue;
      }
      $entities[] = $repository
        ->loadEntityByUuid($entity_type_id, $entity_id);
    }
    if (!$entities) {
      throw new \Exception("No entities loaded from the manifest.");
    }

    /** @var \Drupal\acquia_contenthub\ContentHubCommonActions $common */
    $common = \Drupal::service('acquia_contenthub_common_actions');
    return $common
      ->getLocalCdfDocument(...$entities)
      ->toString();
  }

  /**
   * Imports entities from a CDF Document.
   *
   * @param string $location
   *   The location of the cdf file.
   *
   * @command acquia:contenthub-import-local-cdf
   * @aliases ach-ilc
   *
   * @throws \Exception
   */
  public function importCdf($location) : void {
    if (!file_exists($location)) {
      throw new \Exception("The cdf to import was not found in the specified location.");
    }
    $json = file_get_contents($location);
    $data = Json::decode($json);
    $document_parts = [];
    foreach ($data['entities'] as $entity) {
      $document_parts[] = CDFObject::fromArray($entity);
    }
    $cdf_document = new CDFDocument(...$document_parts);

    /** @var \Drupal\acquia_contenthub\ContentHubCommonActions $common */
    $common = \Drupal::service('acquia_contenthub_common_actions');
    $stack = $common
      ->importEntityCdfDocument($cdf_document);
    $this->output
      ->writeln(dt("Imported @items from @location.", [
      '@items' => count($stack
        ->getDependencies()),
      '@location' => $location,
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiaContentHubCDFCommands::exportCdf public function Generates a CDF Document from a manifest file.
AcquiaContentHubCDFCommands::importCdf public function Imports entities from a CDF Document.
AcquiaContentHubCDFCommands::__construct public function AcquiaContentHubCDFCommands constructor.