You are here

public function AudiofieldCommands::update in AudioField 8

Updates Audiofield libraries from their remote repos if out of date.

@command audiofield:update @aliases audiofield-update

Parameters

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

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

File

src/Commands/AudiofieldCommands.php, line 200

Class

AudiofieldCommands
A Drush commandfile for Audiofield module.

Namespace

Drupal\audiofield\Commands

Code

public function update($updateLibrary = '', $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($updateLibrary)) {
    if (!isset($pluginList[$updateLibrary . '_audio_player'])) {
      $this
        ->logger()
        ->error(dt('Error: @library is not a valid Audiofield library.', [
        '@library' => $updateLibrary,
      ]));
      return;
    }

    // If the argument is valid, we only want to install that plugin.
    $pluginList = [
      $updateLibrary . '_audio_player' => $pluginList[$updateLibrary . '_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;
    }

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

    // If the library isn't installed at all we just run the install.
    if (!$pluginInstance
      ->checkInstalled(FALSE)) {
      $this
        ->download($pluginInstance
        ->getPluginLibraryName());
      continue;
    }

    // Don't updating the library if its up to date.
    if ($pluginInstance
      ->checkVersion(FALSE)) {
      $this
        ->logger()
        ->notice(dt('Audiofield library for @library is already up to date', [
        '@library' => $pluginInstance
          ->getPluginTitle(),
      ]));
      continue;
    }

    // Move the current installation to the temp directory.
    $this->fileSystem
      ->move($path, $this->fileSystem
      ->getTempDirectory() . '/temp_audiofield', TRUE);

    // If the directory failed to move, just delete it.
    if (is_dir($path)) {
      $this->fileSystem
        ->rmdir($path);
    }

    // Run the install command now to get the latest version.
    $this
      ->download($updateLibrary, FALSE);

    // Check if library has been properly installed.
    if ($pluginInstance
      ->checkInstalled()) {

      // Remove the temporary directory.
      $this->fileSystem
        ->rmdir($this->fileSystem
        ->getTempDirectory() . '/temp_audiofield');
      $this
        ->logger()
        ->notice(dt('Audiofield library for @library has been successfully updated at @location', [
        '@library' => $pluginInstance
          ->getPluginTitle(),
        '@location' => $pluginInstance
          ->getPluginLibraryPath(),
      ]));
    }
    else {

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

      // Restore the original install since we failed to update.
      $this->fileSystem
        ->move($this->fileSystem
        ->getTempDirectory() . '/temp_audiofield', $path, TRUE);
    }
  }
}