You are here

public function AudiofieldCommands::download in AudioField 8

Downloads the suggested Audiofield libraries from their remote repos.

@command audiofield:download @aliases audiofield-download

Parameters

string $installLibrary: The name of the library. If omitted, all libraries will be installed.

bool $print_messages: Flag indicating if messages should be displayed.

1 call to AudiofieldCommands::download()
AudiofieldCommands::update in src/Commands/AudiofieldCommands.php
Updates Audiofield libraries from their remote repos if out of date.

File

src/Commands/AudiofieldCommands.php, line 60

Class

AudiofieldCommands
A Drush commandfile for Audiofield module.

Namespace

Drupal\audiofield\Commands

Code

public function download($installLibrary = '', $print_messages = TRUE) {

  // Get a list of the audiofield plugins.
  $pluginList = $this->playerManager
    ->getDefinitions();

  // If there is an argument, check to make sure its valid.
  if (!empty($installLibrary)) {
    if (!isset($pluginList[$installLibrary . '_audio_player'])) {
      $this
        ->logger()
        ->error(dt('Error: @library is not a valid Audiofield library.', [
        '@library' => $installLibrary,
      ]));
      return;
    }

    // If the argument is valid, we only want to install that plugin.
    $pluginList = [
      $installLibrary . '_audio_player' => $pluginList[$installLibrary . '_audio_player'],
    ];
  }

  // Loop over each plugin and make sure it's library is installed.
  foreach ($pluginList as $pluginName => $plugin) {

    // Create an instance of this plugin.
    $pluginInstance = $this->playerManager
      ->createInstance($pluginName);

    // Only check install if there is a library for the plugin.
    if (!$pluginInstance
      ->getPluginLibrary()) {
      continue;
    }

    // Skip if the plugin is installed.
    if ($pluginInstance
      ->checkInstalled()) {
      if ($print_messages) {
        $this
          ->logger()
          ->notice(dt('Audiofield library for @library is already installed at @location', [
          '@library' => $pluginInstance
            ->getPluginTitle(),
          '@location' => $pluginInstance
            ->getPluginLibraryPath(),
        ]));
      }
      continue;
    }

    // Get the library install path.
    $path = DRUPAL_ROOT . $pluginInstance
      ->getPluginLibraryPath();

    // Create the install directory if it does not exist.
    if (!is_dir($path)) {
      $this->fileSystem
        ->mkdir($path);
    }

    // Download the file.
    $destination = $this->fileSystem
      ->tempnam($this->fileSystem
      ->getTempDirectory(), 'file.') . "tar.gz";
    system_retrieve_file($pluginInstance
      ->getPluginRemoteSource(), $destination, FALSE);
    if (!file_exists($destination)) {

      // Remove the directory.
      $this->fileSystem
        ->rmdir($path);
      $this
        ->logger()
        ->error(dt('Error: unable to download @library.', [
        '@library' => $pluginInstance
          ->getPluginTitle(),
      ]));
      continue;
    }
    $this->fileSystem
      ->move($destination, $path . '/audiofield-dl.zip');
    if (!file_exists($path . '/audiofield-dl.zip')) {

      // Remove the directory where we tried to install.
      $this->fileSystem
        ->rmdir($path);
      if ($print_messages) {
        $this
          ->logger()
          ->error(dt('Error: unable to download Audiofield library @library', [
          '@library' => $pluginInstance
            ->getPluginTitle(),
        ]));
        continue;
      }
    }

    // Unzip the file.
    $zipFile = $this->archiverManager
      ->getInstance([
      'filepath' => $path . '/audiofield-dl.zip',
    ]);
    $zipFile
      ->extract($path);

    // Remove the downloaded zip file.
    $this->fileSystem
      ->unlink($path . '/audiofield-dl.zip');

    // If the library still is not installed, we need to move files.
    if (!$pluginInstance
      ->checkInstalled()) {

      // Find all folders in this directory and move their
      // subdirectories up to the parent directory.
      $directories = $this->fileSystem
        ->scanDirectory($path, '/.*?/', [
        'recurse' => FALSE,
      ]);
      foreach ($directories as $dirName) {
        $this->fileSystem
          ->move($path . '/' . $dirName->filename, $this->fileSystem
          ->getTempDirectory() . '/temp_audiofield', FileSystemInterface::EXISTS_REPLACE);
        $this->fileSystem
          ->rmdir($path);
        $this->fileSystem
          ->move($this->fileSystem
          ->getTempDirectory() . '/temp_audiofield', $path, FileSystemInterface::EXISTS_REPLACE);
      }

      // Projekktor source files need to be installed.
      if ($pluginInstance
        ->getPluginId() == 'projekktor_audio_player') {
        $process = Drush::process('npm install', $path);
        $process
          ->run();
        $process = Drush::process('grunt --force', $path);
        $process
          ->run();
      }

      // Wavesurfer source files need to be installed.
      if ($pluginInstance
        ->getPluginId() == 'wavesurfer_audio_player') {
        $this
          ->logger()
          ->notice(dt('Installing @library', [
          '@library' => $pluginInstance
            ->getPluginTitle(),
        ]));
        $process = Drush::process('npm install', $path);
        $process
          ->run();
        $this
          ->logger()
          ->notice(dt('Building @library', [
          '@library' => $pluginInstance
            ->getPluginTitle(),
        ]));
        $process = Drush::process('npm run build', $path);
        $process
          ->run();
      }
    }
    if ($pluginInstance
      ->checkInstalled()) {
      if ($print_messages) {
        $this
          ->logger()
          ->notice(dt('Audiofield library for @library has been successfully installed at @location', [
          '@library' => $pluginInstance
            ->getPluginTitle(),
          '@location' => $pluginInstance
            ->getPluginLibraryPath(),
        ]));
      }
    }
    else {

      // Remove the directory where we tried to install.
      $this->fileSystem
        ->rmdir($path);
      if ($print_messages) {
        $this
          ->logger()
          ->error(dt('Error: unable to install Audiofield library @library', [
          '@library' => $pluginInstance
            ->getPluginTitle(),
        ]));
      }
    }
  }
}