/**
  * Reads and merges in existing files for a given package or profile.
  */
 protected function preparePackage(Package $package, array $existing_packages, FeaturesBundleInterface $bundle = NULL)
 {
     if (isset($existing_packages[$package->getMachineName()])) {
         $existing_directory = $existing_packages[$package->getMachineName()];
         // Scan for all files.
         $files = file_scan_directory($existing_directory, '/.*/');
         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);
             } else {
                 // 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)]);
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Generates and adds .info.yml files to a package.
  *
  * @param \Drupal\features\Package $package
  *   The package.
  */
 protected function addInfoFile(Package $package)
 {
     $info = ['name' => $package->getName(), 'description' => $package->getDescription(), 'type' => $package->getType(), 'core' => $package->getCore(), 'dependencies' => $package->getDependencies(), 'themes' => $package->getThemes(), 'version' => $package->getVersion()];
     $features_info = [];
     // Assign to a "package" named for the profile.
     if ($package->getBundle()) {
         $bundle = $this->getAssigner()->getBundle($package->getBundle());
     }
     // Save the current bundle in the info file so the package
     // can be reloaded later by the AssignmentPackages plugin.
     if (isset($bundle) && !$bundle->isDefault()) {
         $info['package'] = $bundle->getName();
         $features_info['bundle'] = $bundle->getMachineName();
     } else {
         unset($features_info['bundle']);
     }
     if ($package->getConfig()) {
         foreach (array('excluded', 'required') as $constraint) {
             if (!empty($package->{'get' . $constraint}())) {
                 $features_info[$constraint] = $package->{'get' . $constraint}();
             } else {
                 unset($features_info[$constraint]);
             }
         }
         if (empty($features_info)) {
             $features_info = TRUE;
         }
     }
     // The name and description need to be cast as strings from the
     // TranslatableMarkup objects returned by t() to avoid raising an
     // InvalidDataTypeException on Yaml serialization.
     foreach (array('name', 'description') as $key) {
         $info[$key] = (string) $info[$key];
     }
     // Add profile-specific info data.
     if ($info['type'] == 'profile') {
         // Set the distribution name.
         $info['distribution'] = ['name' => $info['name']];
     }
     $package->appendFile(['filename' => $package->getMachineName() . '.info.yml', 'subdirectory' => NULL, 'string' => Yaml::encode(array_filter($info))], 'info');
     $package->appendFile(['filename' => $package->getMachineName() . '.features.yml', 'subdirectory' => NULL, 'string' => Yaml::encode($features_info)], 'features');
 }