You are here

abstract class FeaturesGenerationMethodBase in Features 8.4

Same name and namespace in other branches
  1. 8.3 src/FeaturesGenerationMethodBase.php \Drupal\features\FeaturesGenerationMethodBase

Base class for package assignment methods.

Hierarchy

Expanded class hierarchy of FeaturesGenerationMethodBase

2 files declare their use of FeaturesGenerationMethodBase
FeaturesGenerationArchive.php in src/Plugin/FeaturesGeneration/FeaturesGenerationArchive.php
FeaturesGenerationWrite.php in src/Plugin/FeaturesGeneration/FeaturesGenerationWrite.php

File

src/FeaturesGenerationMethodBase.php, line 12

Namespace

Drupal\features
View source
abstract class FeaturesGenerationMethodBase implements FeaturesGenerationMethodInterface {
  use StringTranslationTrait;

  /**
   * The features manager.
   *
   * @var \Drupal\features\FeaturesManagerInterface
   */
  protected $featuresManager;

  /**
   * The features assigner.
   *
   * @var \Drupal\features\FeaturesAssignerInterface
   */
  protected $assigner;

  /**
   * {@inheritdoc}
   */
  public function setFeaturesManager(FeaturesManagerInterface $features_manager) {
    $this->featuresManager = $features_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function setAssigner(FeaturesAssignerInterface $assigner) {
    $this->assigner = $assigner;
  }

  /**
   * {@inheritdoc}
   */
  public function exportFormSubmit(array &$form, FormStateInterface $form_state) {
  }

  /**
   * Merges an info file into a package's info file.
   *
   * @param string $package_info
   *   The Yaml encoded package info.
   * @param string $info_file_uri
   *   The info file's URI.
   */
  protected function mergeInfoFile($package_info, $info_file_uri) {
    $package_info = Yaml::decode($package_info);

    // \Drupal\Core\Extension\InfoParser::parse() makes changes we don't want
    // here such as adding a core_incompatible key. Instead parse the file
    // directly.
    $existing_info = Yaml::decode(file_get_contents($info_file_uri));

    // Remove the 'core' property if present since it has been replaced with
    // 'core_version_requirement'.
    if (isset($existing_info['core'])) {
      unset($existing_info['core']);
    }
    return Yaml::encode($this->featuresManager
      ->mergeInfoArray($existing_info, $package_info));
  }

  /**
   * {@inheritdoc}
   */
  public function prepare(array &$packages = [], FeaturesBundleInterface $bundle = NULL) {

    // If no packages were specified, get all packages.
    if (empty($packages)) {
      $packages = $this->featuresManager
        ->getPackages();
    }

    // If any packages exist, read in their files.
    $existing_packages = $this->featuresManager
      ->listPackageDirectories(array_keys($packages), $bundle);
    foreach ($packages as &$package) {
      list($full_name, $path) = $this->featuresManager
        ->getExportInfo($package, $bundle);
      if (empty($package
        ->getDirectory())) {
        $package
          ->setDirectory($path);
      }

      // If this is the profile, its directory is already assigned.
      if (!isset($bundle) || !$bundle
        ->isProfilePackage($package
        ->getMachineName())) {
        $current_path = $package
          ->getDirectory();
        if (strpos($current_path, $full_name) < strlen($current_path) - strlen($full_name)) {

          // Only append package name if it isn't already there.
          $package
            ->setDirectory($package
            ->getDirectory() . '/' . $full_name);
        }
      }
      $this
        ->preparePackage($package, $existing_packages, $bundle);
    }

    // Clean up the $package pass by reference.
    unset($package);
  }

  /**
   * Performs any required changes on a package prior to generation.
   *
   * @param \Drupal\features\Package $package
   *   The package to be prepared.
   * @param array $existing_packages
   *   An array of existing packages with machine names as keys and paths as
   *   values.
   * @param \Drupal\features\FeaturesBundleInterface $bundle
   *   Optional bundle used for export.
   */
  protected abstract function preparePackage(Package $package, array $existing_packages, FeaturesBundleInterface $bundle = NULL);

}

Members

Namesort descending Modifiers Type Description Overrides
FeaturesGenerationMethodBase::$assigner protected property The features assigner.
FeaturesGenerationMethodBase::$featuresManager protected property The features manager.
FeaturesGenerationMethodBase::exportFormSubmit public function Responds to the submission of \Drupal\features_ui\Form\FeaturesExportForm. Overrides FeaturesGenerationMethodInterface::exportFormSubmit 1
FeaturesGenerationMethodBase::mergeInfoFile protected function Merges an info file into a package's info file.
FeaturesGenerationMethodBase::prepare public function Prepares packages for generation. Overrides FeaturesGenerationMethodInterface::prepare
FeaturesGenerationMethodBase::preparePackage abstract protected function Performs any required changes on a package prior to generation. 2
FeaturesGenerationMethodBase::setAssigner public function Injects the features assigner. Overrides FeaturesGenerationMethodInterface::setAssigner
FeaturesGenerationMethodBase::setFeaturesManager public function Injects the features manager. Overrides FeaturesGenerationMethodInterface::setFeaturesManager
FeaturesGenerationMethodInterface::generate public function Performs package generation. 2
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.