You are here

public function Micon::archiveExtract in Micon 2.x

Same name and namespace in other branches
  1. 8 src/Entity/Micon.php \Drupal\micon\Entity\Micon::archiveExtract()

Properly extract and store an IcoMoon zip file.

Parameters

string $zip_path: The absolute path to the zip file.

1 call to Micon::archiveExtract()
Micon::archiveDecode in src/Entity/Micon.php
Take base64 encoded archive and save it to a temporary file for extraction.

File

src/Entity/Micon.php, line 277

Class

Micon
Defines the Micon entity.

Namespace

Drupal\micon\Entity

Code

public function archiveExtract($zip_path) {
  $file_system = \Drupal::service('file_system');
  $archiver = \Drupal::service('plugin.manager.archiver')
    ->getInstance([
    'filepath' => $file_system
      ->realpath($zip_path),
  ]);
  if (!$archiver) {
    throw new \Exception(t('Cannot extract %file, not a valid archive.', [
      '%file' => $zip_path,
    ]));
  }
  $directory = $this
    ->getDirectory();
  $file_system
    ->deleteRecursive($directory);
  $file_system
    ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  $archiver
    ->extract($directory);

  // Remove unnecessary files.
  $file_system
    ->deleteRecursive($directory . '/demo-files');
  $file_system
    ->deleteRecursive($directory . '/demo.html');
  $file_system
    ->delete($directory . '/Read Me.txt');

  // Set package type to svg.
  if (file_exists($directory . '/symbol-defs.svg')) {
    $this
      ->setAsSvg();

    // Update symbol to match new id.
    $file_path = $directory . '/symbol-defs.svg';
    $file_contents = file_get_contents($file_path);
    $file_contents = str_replace($this
      ->getPrefix(), $this
      ->id() . '-', $file_contents);
    file_put_contents($file_path, $file_contents);
  }
  else {
    $font_directory = $directory . '/fonts';
    $files_to_rename = $font_directory . '/*.*';
    foreach (glob($file_system
      ->realpath($files_to_rename)) as $file_to_rename_path) {
      $file_new_path = str_replace('fonts/' . $this
        ->getName(), 'fonts/' . $this
        ->id(), $file_to_rename_path);
      if ($file_to_rename_path !== $file_new_path) {
        $file_system
          ->move($file_to_rename_path, $file_new_path, FileSystemInterface::EXISTS_REPLACE);
      }
    }
  }

  // Update IcoMoon selection.json.
  $file_path = $directory . '/selection.json';
  $file_contents = file_get_contents($file_path);

  // Protect icon keys.
  $file_contents = str_replace('"icons":', 'MICONSIcons', $file_contents);
  $file_contents = str_replace('"icon":', 'MICONIcon', $file_contents);
  $file_contents = str_replace('iconIdx', 'MICONIdx', $file_contents);
  $file_contents = str_replace($this
    ->getPrefix(), 'MICONPrefix', $file_contents);

  // The name and selector should be updated to match entity info.
  $file_contents = str_replace($this
    ->getName(), $this
    ->id(), $file_contents);

  // Return protected keys.
  $file_contents = str_replace('MICONSIcons', '"icons":', $file_contents);
  $file_contents = str_replace('MICONIcon', '"icon":', $file_contents);
  $file_contents = str_replace('MICONIdx', 'iconIdx', $file_contents);
  $file_contents = str_replace('MICONPrefix', $this
    ->id() . '-', $file_contents);
  file_put_contents($file_path, $file_contents);

  // Update IcoMoon stylesheet.
  $file_path = $directory . '/style.css';
  $file_contents = file_get_contents($file_path);

  // The style.css file provided by IcoMoon contains query parameters where it
  // loads in the font files. Drupal CSS aggregation doesn't handle this well
  // so we need to remove it.
  $file_contents = preg_replace('(\\?[a-zA-Z0-9#\\-\\_]*)', '', $file_contents);

  // Protect prefixes.
  $file_contents = str_replace($this
    ->getPrefix(), 'MICON', $file_contents);

  // The name and selector should be updated to match entity info.
  $file_contents = str_replace($this
    ->getName(), $this
    ->id(), $file_contents);

  // Return changed prefixes. This prevents something like m-icon from
  // becoming m-icon-icon.
  $file_contents = str_replace('MICON', $this
    ->id() . '-', $file_contents);
  file_put_contents($file_path, $file_contents);
}