FeaturesGenerator.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Drupal\features;
  3. use Drupal\Component\Plugin\PluginManagerInterface;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\StringTranslation\StringTranslationTrait;
  6. /**
  7. * Class responsible for performing package generation.
  8. */
  9. class FeaturesGenerator implements FeaturesGeneratorInterface {
  10. use StringTranslationTrait;
  11. /**
  12. * The package generation method plugin manager.
  13. *
  14. * @var \Drupal\Component\Plugin\PluginManagerInterface
  15. */
  16. protected $generatorManager;
  17. /**
  18. * The features manager.
  19. *
  20. * @var \Drupal\features\FeaturesManagerInterface
  21. */
  22. protected $featuresManager;
  23. /**
  24. * The features assigner.
  25. *
  26. * @var \Drupal\features\FeaturesAssignerInterface
  27. */
  28. protected $assigner;
  29. /**
  30. * Local cache for package generation method instances.
  31. *
  32. * @var array
  33. */
  34. protected $methods;
  35. /**
  36. * Constructs a new FeaturesGenerator object.
  37. *
  38. * @param \Drupal\features\FeaturesManagerInterface $features_manager
  39. * The features manager.
  40. * @param \Drupal\Component\Plugin\PluginManagerInterface $generator_manager
  41. * The package generation methods plugin manager.
  42. */
  43. public function __construct(FeaturesManagerInterface $features_manager, PluginManagerInterface $generator_manager, FeaturesAssignerInterface $assigner) {
  44. $this->featuresManager = $features_manager;
  45. $this->generatorManager = $generator_manager;
  46. $this->assigner = $assigner;
  47. }
  48. /**
  49. * Initializes the injected features manager with the generator.
  50. *
  51. * This should be called right after instantiating the generator to make it
  52. * available to the features manager without introducing a circular
  53. * dependency.
  54. */
  55. public function initFeaturesManager() {
  56. $this->featuresManager->setGenerator($this);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function reset() {
  62. $this->methods = array();
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function applyGenerationMethod($method_id, array $packages = array(), FeaturesBundleInterface $bundle = NULL) {
  68. $method = $this->getGenerationMethodInstance($method_id);
  69. $method->prepare($packages, $bundle);
  70. return $method->generate($packages, $bundle);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function applyExportFormSubmit($method_id, &$form, FormStateInterface $form_state) {
  76. $method = $this->getGenerationMethodInstance($method_id);
  77. $method->exportFormSubmit($form, $form_state);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getGenerationMethods() {
  83. return $this->generatorManager->getDefinitions();
  84. }
  85. /**
  86. * Returns an instance of the specified package generation method.
  87. *
  88. * @param string $method_id
  89. * The string identifier of the package generation method to use to package
  90. * configuration.
  91. *
  92. * @return \Drupal\features\FeaturesGenerationMethodInterface
  93. */
  94. protected function getGenerationMethodInstance($method_id) {
  95. if (!isset($this->methods[$method_id])) {
  96. $instance = $this->generatorManager->createInstance($method_id, array());
  97. $instance->setFeaturesManager($this->featuresManager);
  98. $instance->setAssigner($this->assigner);
  99. $this->methods[$method_id] = $instance;
  100. }
  101. return $this->methods[$method_id];
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function generatePackages($method_id, FeaturesBundleInterface $bundle, array $package_names = array()) {
  107. $this->featuresManager->setPackageBundleNames($bundle, $package_names);
  108. return $this->generate($method_id, $bundle, $package_names);
  109. }
  110. /**
  111. * Generates a file representation of configuration packages and, optionally,
  112. * an install profile.
  113. *
  114. * @param string $method_id
  115. * The ID of the generation method to use.
  116. * @param \Drupal\features\FeaturesBundleInterface $bundle
  117. * The bundle used for the generation.
  118. * @param string[] $package_names
  119. * Names of packages to be generated. If none are specified, all
  120. * available packages will be added.
  121. *
  122. * @return array
  123. * Array of results for profile and/or packages, each result including the
  124. * following keys:
  125. * - 'success': boolean TRUE or FALSE for successful writing.
  126. * - 'display': boolean TRUE if the message should be displayed to the
  127. * user, otherwise FALSE.
  128. * - 'message': a message about the result of the operation.
  129. * - 'variables': an array of substitutions to be used in the message.
  130. */
  131. protected function generate($method_id, FeaturesBundleInterface $bundle, array $package_names = array()) {
  132. $packages = $this->featuresManager->getPackages();
  133. // Filter out the packages that weren't requested.
  134. if (!empty($package_names)) {
  135. $packages = array_intersect_key($packages, array_fill_keys($package_names, NULL));
  136. }
  137. $this->featuresManager->assignInterPackageDependencies($bundle, $packages);
  138. // Prepare the files.
  139. $this->featuresManager->prepareFiles($packages);
  140. $return = $this->applyGenerationMethod($method_id, $packages, $bundle);
  141. foreach ($return as $message) {
  142. if ($message['display']) {
  143. $type = $message['success'] ? 'status' : 'error';
  144. drupal_set_message($this->t($message['message'], $message['variables']), $type);
  145. }
  146. $type = $message['success'] ? 'notice' : 'error';
  147. \Drupal::logger('features')->{$type}($message['message'], $message['variables']);
  148. }
  149. return $return;
  150. }
  151. }