FeaturesGenerationMethodBase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Drupal\features;
  3. use Drupal\Component\Serialization\Yaml;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\StringTranslation\StringTranslationTrait;
  6. /**
  7. * Base class for package assignment methods.
  8. */
  9. abstract class FeaturesGenerationMethodBase implements FeaturesGenerationMethodInterface {
  10. use StringTranslationTrait;
  11. /**
  12. * The features manager.
  13. *
  14. * @var \Drupal\features\FeaturesManagerInterface
  15. */
  16. protected $featuresManager;
  17. /**
  18. * The features assigner.
  19. *
  20. * @var \Drupal\features\FeaturesAssignerInterface
  21. */
  22. protected $assigner;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function setFeaturesManager(FeaturesManagerInterface $features_manager) {
  27. $this->featuresManager = $features_manager;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function setAssigner(FeaturesAssignerInterface $assigner) {
  33. $this->assigner = $assigner;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function exportFormSubmit(array &$form, FormStateInterface $form_state) {
  39. }
  40. /**
  41. * Merges an info file into a package's info file.
  42. *
  43. * @param string $package_info
  44. * The Yaml encoded package info.
  45. * @param string $info_file_uri
  46. * The info file's URI.
  47. */
  48. protected function mergeInfoFile($package_info, $info_file_uri) {
  49. $package_info = Yaml::decode($package_info);
  50. /** @var \Drupal\Core\Extension\InfoParserInterface $existing_info */
  51. $existing_info = \Drupal::service('info_parser')->parse($info_file_uri);
  52. return Yaml::encode($this->featuresManager->mergeInfoArray($existing_info, $package_info));
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function prepare(array &$packages = array(), FeaturesBundleInterface $bundle = NULL) {
  58. // If no packages were specified, get all packages.
  59. if (empty($packages)) {
  60. $packages = $this->featuresManager->getPackages();
  61. }
  62. // If any packages exist, read in their files.
  63. $existing_packages = $this->featuresManager->listPackageDirectories(array_keys($packages), $bundle);
  64. foreach ($packages as &$package) {
  65. list($full_name, $path) = $this->featuresManager->getExportInfo($package, $bundle);
  66. if (empty($package->getDirectory())) {
  67. $package->setDirectory($path);
  68. }
  69. // If this is the profile, its directory is already assigned.
  70. if (!isset($bundle) || !$bundle->isProfilePackage($package->getMachineName())) {
  71. $current_path = $package->getDirectory();
  72. if (strpos($current_path, $full_name) < strlen($current_path) - strlen($full_name)) {
  73. // Only append package name if it isn't already there.
  74. $package->setDirectory($package->getDirectory() . '/' . $full_name);
  75. }
  76. }
  77. $this->preparePackage($package, $existing_packages, $bundle);
  78. }
  79. // Clean up the $package pass by reference.
  80. unset($package);
  81. }
  82. /**
  83. * Performs any required changes on a package prior to generation.
  84. *
  85. * @param \Drupal\features\Package $package
  86. * The package to be prepared.
  87. * @param array $existing_packages
  88. * An array of existing packages with machine names as keys and paths as
  89. * values.
  90. * @param \Drupal\features\FeaturesBundleInterface $bundle
  91. * Optional bundle used for export
  92. */
  93. abstract protected function preparePackage(Package $package, array $existing_packages, FeaturesBundleInterface $bundle = NULL);
  94. }