You are here

class DeployManager in Default Content Deploy 8

Hierarchy

Expanded class hierarchy of DeployManager

4 files declare their use of DeployManager
DefaultContentDeployCommands.php in src/Commands/DefaultContentDeployCommands.php
DownloadController.php in src/Controller/DownloadController.php
ExportForm.php in src/Form/ExportForm.php
ImportForm.php in src/Form/ImportForm.php
1 string reference to 'DeployManager'
default_content_deploy.services.yml in ./default_content_deploy.services.yml
default_content_deploy.services.yml
1 service uses DeployManager
default_content_deploy.manager in ./default_content_deploy.services.yml
Drupal\default_content_deploy\DeployManager

File

src/DeployManager.php, line 13

Namespace

Drupal\default_content_deploy
View source
class DeployManager {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * Site settings.
   *
   * @var \Drupal\Core\Site\Settings
   */
  protected $settings;

  /**
   * The File system.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request|null
   */
  protected $request;

  /**
   * DeployManager constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config
   *   The config factory.
   * @param \Drupal\Core\Site\Settings $settings
   *   Site settings.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The File system.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The current request.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config, Settings $settings, FileSystemInterface $file_system, RequestStack $request_stack) {
    $this->entityTypeManager = $entity_type_manager;
    $this->config = $config;
    $this->settings = $settings;
    $this->fileSystem = $file_system;
    $this->request = $request_stack
      ->getCurrentRequest();
  }

  /**
   * Get UUID of entity.
   *
   * @param $entity_type
   *   Entity type ID.
   * @param $id
   *   ID of entity.
   *
   * @return string
   *   UUID value.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getEntityUuidById($entity_type, $id) {
    $entity = $this->entityTypeManager
      ->getStorage($entity_type)
      ->load($id);
    return $entity
      ->uuid();
  }

  /**
   * Get all Content Entity Types.
   *
   * @return array
   *   Array of available content entity definitions keyed by type ID.
   *   [entity_type => \Drupal\Core\Entity\EntityTypeInterface]
   */
  public function getContentEntityTypes() {
    $types = [];
    $entity_types = $this->entityTypeManager
      ->getDefinitions();
    foreach ($entity_types as $id => $definition) {
      if ($definition instanceof ContentEntityType) {
        $types[$id] = $definition
          ->getLabel();
      }
    }
    ksort($types);
    return $types;
  }

  /**
   * Get content folder.
   *
   * Folder is automatically created on install inside files folder.
   * Or you can override content folder in settings.php file.
   *
   * If no configuration is found, directory is created
   * automatically at public://content_{hash_salt_derived_key};
   *
   * @return string
   *   Return path to the content folder.
   * @example Backward compatibility usage:
   *   $config_directories['content_directory'] = '../content';
   *   $config['content_directory'] = '../content';
   *
   * @example Recommended usage:
   *   $settings['default_content_deploy_content_directory'] = '../content';
   *
   * @throws \Exception
   */
  public function getContentFolder() {
    $config = $this->config
      ->get('default_content_deploy.content_directory');
    if ($directory = $config
      ->get('content_directory')) {
      return $directory;
    }
    elseif ($directory = $this->settings
      ->get('default_content_deploy_content_directory')) {
      return $directory;
    }
    throw new \Exception('Directory for content deploy is not set.');
  }

  /**
   * Gets host of current site.
   *
   * @return string
   */
  public function getCurrentHost() {
    $protocol = $this->request
      ->getScheme();
    $host = $this->request
      ->getHttpHost();
    return "{$protocol}://{$host}";
  }

  /**
   * Compress the content files to an archive.
   *
   * @return $this
   */
  public function compressContent() {
    $folder = $this->fileSystem
      ->getTempDirectory() . '/dcd';
    $content_folder = $folder . '/content';

    // Remove old archive.
    $this->fileSystem
      ->deleteRecursive($folder . '/content.tar.gz');
    $archive = new ArchiveTar($folder . '/content.tar.gz', 'gz');
    $archive
      ->addModify($content_folder, basename($content_folder), $content_folder);
    return $this;
  }

  /**
   * Uncompressed an archive with content files.
   *
   * @param $file
   *
   * @return $this
   *
   * @throws \Exception
   */
  public function uncompressContent($file) {
    $folder = $this->fileSystem
      ->getTempDirectory() . '/dcd';
    $content_folder = $folder . '/content';

    // Remove old folder.
    $this->fileSystem
      ->deleteRecursive($content_folder);
    $archive = new ArchiveTar($file, 'gz');
    $list = $archive
      ->listContent();

    // Checking the folder structure.
    if (!stripos($list[0]['filename'], 'content/')) {
      $archive
        ->extract($folder);
    }
    else {
      throw new \Exception('The wrong folder structure');
    }
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeployManager::$config protected property The config factory.
DeployManager::$entityTypeManager protected property The entity type manager.
DeployManager::$fileSystem protected property The File system.
DeployManager::$request protected property The current request.
DeployManager::$settings protected property Site settings.
DeployManager::compressContent public function Compress the content files to an archive.
DeployManager::getContentEntityTypes public function Get all Content Entity Types.
DeployManager::getContentFolder public function Get content folder.
DeployManager::getCurrentHost public function Gets host of current site.
DeployManager::getEntityUuidById public function Get UUID of entity.
DeployManager::uncompressContent public function Uncompressed an archive with content files.
DeployManager::__construct public function DeployManager constructor.