You are here

public function CodeMirrorEditorCommands::downloadCodemirror in The CodeMirror Editor 8

Downloads CodeMirror library.

@command codemirror:download @aliases codemirror-download

File

src/Commands/CodeMirrorEditorCommands.php, line 112

Class

CodeMirrorEditorCommands
Drush integration for CodeMirror editor module.

Namespace

Drupal\codemirror_editor\Commands

Code

public function downloadCodemirror() {
  $io = $this
    ->io();
  $cm_library = $this->libraryDiscovery
    ->getLibraryByName('codemirror_editor', 'codemirror');
  $source_base_path = str_replace('{version}', LibraryBuilderInterface::CODEMIRROR_VERSION, LibraryBuilderInterface::CDN_URL);
  $destination_base_path = DRUPAL_ROOT . LibraryBuilderInterface::LIBRARY_PATH;
  $assets = array_merge($cm_library['js'], $cm_library['css']);
  foreach ($assets as $asset) {
    if (empty($asset['type']) || $asset['type'] != 'external') {
      $source = $source_base_path . '/' . explode(ltrim(LibraryBuilderInterface::LIBRARY_PATH, '/'), $asset['data'])[1];
      $destination = DRUPAL_ROOT . '/' . $asset['data'];
    }
    else {
      $source = $asset['data'];
      $destination = $destination_base_path . explode($source_base_path, $asset['data'])[1];
    }
    $io
      ->write("<info>{$source}</info>");
    $directory = dirname($destination);
    if (!$this->fileSystem
      ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY)) {
      $io
        ->error("Could not create directory {$directory}.");
      return 1;
    }
    try {
      $response = $this->httpClient
        ->get($source, [
        'sink' => $destination,
      ]);
    } catch (\Exception $exception) {
      $io
        ->error($exception
        ->getMessage());
      return 1;
    }
    $status_code = $response
      ->getStatusCode();
    Response::$statusTexts[$status_code];
    $io
      ->writeln(' [' . $status_code . ' ' . Response::$statusTexts[$status_code] . ']');
  }

  // Clear asset caches.
  $this->jsCollectionOptimizer
    ->deleteAll();
  $this->cssCollectionOptimizer
    ->deleteAll();
  $this->state
    ->set('system.css_js_query_string', base_convert($this->time
    ->getRequestTime(), 10, 36));
  $io
    ->success("CodeMirror library has been downloaded into {$destination_base_path} directory.");
}