You are here

protected function FeaturesGenerationArchive::preparePackage in Features 8.3

Same name and namespace in other branches
  1. 8.4 src/Plugin/FeaturesGeneration/FeaturesGenerationArchive.php \Drupal\features\Plugin\FeaturesGeneration\FeaturesGenerationArchive::preparePackage()

Reads and merges in existing files for a given package or profile.

Overrides FeaturesGenerationMethodBase::preparePackage

File

src/Plugin/FeaturesGeneration/FeaturesGenerationArchive.php, line 90

Class

FeaturesGenerationArchive
Class for generating a compressed archive of packages.

Namespace

Drupal\features\Plugin\FeaturesGeneration

Code

protected function preparePackage(Package $package, array $existing_packages, FeaturesBundleInterface $bundle = NULL) {
  if (isset($existing_packages[$package
    ->getMachineName()])) {
    $existing_directory = $existing_packages[$package
      ->getMachineName()];
  }
  else {
    $existing_directory = $package
      ->getDirectory();
  }
  $existing_directory = $this->root . '/' . $existing_directory;
  if (is_dir($existing_directory)) {

    // Scan for all files.
    $files = $this->fileSystem
      ->scanDirectory($existing_directory, '/.*/');

    // Skip any existing .features.yml as it will be replaced.
    $exclude_files = [
      $package
        ->getMachineName() . '.features',
    ];
    foreach ($files as $file) {

      // Skip files in the any existing configuration directory, as these
      // will be replaced.
      foreach (array_keys($this->featuresManager
        ->getExtensionStorages()
        ->getExtensionStorages()) as $directory) {
        if (strpos($file->uri, $directory) !== FALSE) {
          continue 2;
        }
      }

      // Merge in the info file.
      if ($file->name == $package
        ->getMachineName() . '.info') {
        $files = $package
          ->getFiles();
        $files['info']['string'] = $this
          ->mergeInfoFile($package
          ->getFiles()['info']['string'], $file->uri);
        $package
          ->setFiles($files);
      }
      elseif (!in_array($file->name, $exclude_files)) {

        // Determine if the file is within a subdirectory of the
        // extension's directory.
        $file_directory = dirname($file->uri);
        if ($file_directory !== $existing_directory) {
          $subdirectory = substr($file_directory, strlen($existing_directory) + 1);
        }
        else {
          $subdirectory = NULL;
        }
        $package
          ->appendFile([
          'filename' => $file->filename,
          'subdirectory' => $subdirectory,
          'string' => file_get_contents($file->uri),
        ]);
      }
    }
  }
}