FeaturesManager.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. <?php
  2. namespace Drupal\features;
  3. use Drupal;
  4. use Drupal\Component\Serialization\Yaml;
  5. use Drupal\Component\Utility\NestedArray;
  6. use Drupal\Core\Config\ConfigFactoryInterface;
  7. use Drupal\Core\Config\ConfigManagerInterface;
  8. use Drupal\Core\Config\InstallStorage;
  9. use Drupal\Core\Config\StorageInterface;
  10. use Drupal\Core\Entity\EntityTypeManagerInterface;
  11. use Drupal\Core\Entity\EntityTypeInterface;
  12. use Drupal\Core\Extension\Extension;
  13. use Drupal\Core\Extension\ExtensionDiscovery;
  14. use Drupal\Core\Extension\ModuleHandlerInterface;
  15. use Drupal\Core\StringTranslation\StringTranslationTrait;
  16. use Drupal\config_update\ConfigRevertInterface;
  17. /**
  18. * The FeaturesManager provides helper functions for building packages.
  19. */
  20. class FeaturesManager implements FeaturesManagerInterface {
  21. use StringTranslationTrait;
  22. /**
  23. * The entity type manager.
  24. *
  25. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  26. */
  27. protected $entityTypeManager;
  28. /**
  29. * The target storage.
  30. *
  31. * @var \Drupal\Core\Config\StorageInterface
  32. */
  33. protected $configStorage;
  34. /**
  35. * The extension storages.
  36. *
  37. * @var \Drupal\features\FeaturesExtensionStoragesInterface
  38. */
  39. protected $extensionStorages;
  40. /**
  41. * The configuration manager.
  42. *
  43. * @var \Drupal\Core\Config\ConfigManagerInterface
  44. */
  45. protected $configManager;
  46. /**
  47. * The configuration factory.
  48. *
  49. * @var \Drupal\Core\Config\ConfigFactoryInterface
  50. */
  51. protected $configFactory;
  52. /**
  53. * The module handler.
  54. *
  55. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  56. */
  57. protected $moduleHandler;
  58. /**
  59. * The config reverter.
  60. *
  61. * @var \Drupal\config_update\ConfigRevertInterface
  62. */
  63. protected $configReverter;
  64. /**
  65. * The Features settings.
  66. *
  67. * @var array
  68. */
  69. protected $settings;
  70. /**
  71. * The app root.
  72. *
  73. * @var string
  74. */
  75. protected $root;
  76. /**
  77. * The configuration present on the site.
  78. *
  79. * @var \Drupal\features\ConfigurationItem[]
  80. */
  81. private $configCollection;
  82. /**
  83. * The packages to be generated.
  84. *
  85. * @var \Drupal\features\Package[]
  86. */
  87. protected $packages;
  88. /**
  89. * Whether the packages have been assigned a bundle prefix.
  90. *
  91. * @var boolean
  92. */
  93. protected $packagesPrefixed;
  94. /**
  95. * The package assigner.
  96. *
  97. * @var \Drupal\features\FeaturesAssigner
  98. */
  99. protected $assigner;
  100. /**
  101. * Cache module.features.yml data keyed by module name.
  102. *
  103. * @var array
  104. */
  105. protected $featureInfoCache;
  106. /**
  107. * Constructs a FeaturesManager object.
  108. *
  109. * @param string $root
  110. * The app root.
  111. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  112. * The entity type manager.
  113. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  114. * The configuration factory.
  115. * @param \Drupal\Core\Config\StorageInterface $config_storage
  116. * The target storage.
  117. * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
  118. * The configuration manager.
  119. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  120. * The module handler.
  121. * @param \Drupal\config_update\ConfigRevertInterface $config_reverter
  122. */
  123. public function __construct($root, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory,
  124. StorageInterface $config_storage, ConfigManagerInterface $config_manager,
  125. ModuleHandlerInterface $module_handler, ConfigRevertInterface $config_reverter) {
  126. $this->root = $root;
  127. $this->entityTypeManager = $entity_type_manager;
  128. $this->configStorage = $config_storage;
  129. $this->configManager = $config_manager;
  130. $this->moduleHandler = $module_handler;
  131. $this->configFactory = $config_factory;
  132. $this->configReverter = $config_reverter;
  133. $this->settings = $config_factory->getEditable('features.settings');
  134. $this->extensionStorages = new FeaturesExtensionStorages($this->configStorage);
  135. $this->extensionStorages->addStorage(InstallStorage::CONFIG_INSTALL_DIRECTORY);
  136. $this->extensionStorages->addStorage(InstallStorage::CONFIG_OPTIONAL_DIRECTORY);
  137. $this->packages = [];
  138. $this->packagesPrefixed = FALSE;
  139. $this->configCollection = [];
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function setRoot($root) {
  145. $this->root = $root;
  146. // Clear cache.
  147. $this->featureInfoCache = [];
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function getActiveStorage() {
  153. return $this->configStorage;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function getExtensionStorages() {
  159. return $this->extensionStorages;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function getFullName($type, $name) {
  165. if ($type == FeaturesManagerInterface::SYSTEM_SIMPLE_CONFIG || !$type) {
  166. return $name;
  167. }
  168. $definition = $this->entityTypeManager->getDefinition($type);
  169. $prefix = $definition->getConfigPrefix() . '.';
  170. return $prefix . $name;
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function getConfigType($fullname) {
  176. $result = array(
  177. 'type' => '',
  178. 'name_short' => '',
  179. );
  180. $prefix = FeaturesManagerInterface::SYSTEM_SIMPLE_CONFIG . '.';
  181. if (strpos($fullname, $prefix) !== FALSE) {
  182. $result['type'] = FeaturesManagerInterface::SYSTEM_SIMPLE_CONFIG;
  183. $result['name_short'] = substr($fullname, strlen($prefix));
  184. }
  185. else {
  186. foreach ($this->entityTypeManager->getDefinitions() as $entity_type => $definition) {
  187. if ($definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
  188. $prefix = $definition->getConfigPrefix() . '.';
  189. if (strpos($fullname, $prefix) === 0) {
  190. $result['type'] = $entity_type;
  191. $result['name_short'] = substr($fullname, strlen($prefix));
  192. }
  193. }
  194. }
  195. }
  196. return $result;
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. public function reset() {
  202. $this->packages = [];
  203. // Don't use getConfigCollection because reset() may be called in
  204. // cases where we don't need to load config.
  205. foreach ($this->configCollection as $config) {
  206. $config->setPackage(NULL);
  207. }
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function getConfigCollection($reset = FALSE) {
  213. $this->initConfigCollection($reset);
  214. return $this->configCollection;
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function setConfigCollection(array $config_collection) {
  220. $this->configCollection = $config_collection;
  221. }
  222. /**
  223. * {@inheritdoc}
  224. */
  225. public function getPackages() {
  226. return $this->packages;
  227. }
  228. /**
  229. * {@inheritdoc}
  230. */
  231. public function setPackages(array $packages) {
  232. $this->packages = $packages;
  233. }
  234. /**
  235. * {@inheritdoc}
  236. */
  237. public function getPackage($machine_name) {
  238. if (isset($this->packages[$machine_name])) {
  239. return $this->packages[$machine_name];
  240. }
  241. return NULL;
  242. }
  243. /**
  244. * {@inheritdoc}
  245. */
  246. public function findPackage($machine_name) {
  247. $result = $this->getPackage($machine_name);
  248. if (!isset($result)) {
  249. // Didn't find direct match, but now go through and look for matching
  250. // full name (bundle_machinename)
  251. foreach ($this->packages as $name => $package) {
  252. if ($package->getFullName() == $machine_name) {
  253. return $this->packages[$name];
  254. }
  255. }
  256. }
  257. return $result;
  258. }
  259. /**
  260. * {@inheritdoc}
  261. */
  262. public function setPackage(Package $package) {
  263. if ($package->getMachineName()) {
  264. $this->packages[$package->getMachineName()] = $package;
  265. }
  266. }
  267. /**
  268. * {@inheritdoc}
  269. */
  270. public function loadPackage($module_name, $any = FALSE) {
  271. $package = $this->getPackage($module_name);
  272. // Load directly from module if packages are not loaded or
  273. // if we want to include ANY module regardless of its a feature.
  274. if ((empty($this->packages) || $any) && !isset($package)) {
  275. $module_list = $this->moduleHandler->getModuleList();
  276. if (!empty($module_list[$module_name])) {
  277. $extension = $module_list[$module_name];
  278. $package = $this->initPackageFromExtension($extension);
  279. $config = $this->listExtensionConfig($extension);
  280. $package->setConfigOrig($config);
  281. $package->setStatus(FeaturesManagerInterface::STATUS_INSTALLED);
  282. }
  283. }
  284. return $package;
  285. }
  286. /**
  287. * {@inheritdoc}
  288. */
  289. public function filterPackages(array $packages, $namespace = '', $only_exported = FALSE) {
  290. $result = array();
  291. /** @var \Drupal\features\Package $package */
  292. foreach ($packages as $key => $package) {
  293. // A package matches the namespace if:
  294. // - it's prefixed with the namespace, or
  295. // - it's assigned to a bundle named for the namespace, or
  296. // - we're looking only for exported packages and it's not exported.
  297. if (empty($namespace) || (strpos($package->getMachineName(), $namespace . '_') === 0) ||
  298. ($package->getBundle() && $package->getBundle() === $namespace) ||
  299. ($only_exported && $package->getStatus() === FeaturesManagerInterface::STATUS_NO_EXPORT)) {
  300. $result[$key] = $package;
  301. }
  302. }
  303. return $result;
  304. }
  305. /**
  306. * {@inheritdoc}
  307. */
  308. public function getAssigner() {
  309. if (empty($this->assigner)) {
  310. $this->setAssigner(\Drupal::service('features_assigner'));
  311. }
  312. return $this->assigner;
  313. }
  314. /**
  315. * {@inheritdoc}
  316. */
  317. public function setAssigner(FeaturesAssignerInterface $assigner) {
  318. $this->assigner = $assigner;
  319. $this->reset();
  320. }
  321. /**
  322. * {@inheritdoc}
  323. */
  324. public function getGenerator() {
  325. return $this->generator;
  326. }
  327. /**
  328. * {@inheritdoc}
  329. */
  330. public function setGenerator(FeaturesGeneratorInterface $generator) {
  331. $this->generator = $generator;
  332. }
  333. /**
  334. * {@inheritdoc}
  335. */
  336. public function getExportSettings() {
  337. return $this->settings->get('export');
  338. }
  339. /**
  340. * {@inheritdoc}
  341. */
  342. public function getSettings() {
  343. return $this->settings;
  344. }
  345. /**
  346. * {@inheritdoc}
  347. */
  348. public function getExtensionInfo(Extension $extension) {
  349. return \Drupal::service('info_parser')->parse($this->root . '/' . $extension->getPathname());
  350. }
  351. /**
  352. * {@inheritdoc}
  353. */
  354. public function isFeatureModule(Extension $module, FeaturesBundleInterface $bundle = NULL) {
  355. if ($features_info = $this->getFeaturesInfo($module)) {
  356. // If no bundle was requested, it's enough that this is a feature.
  357. if (is_null($bundle)) {
  358. return TRUE;
  359. }
  360. // If the default bundle was requested, look for features where
  361. // the bundle is not set.
  362. elseif ($bundle->isDefault()) {
  363. return !isset($features_info['bundle']);
  364. }
  365. // If we have a bundle name, look for it.
  366. else {
  367. return (isset($features_info['bundle']) && ($features_info['bundle'] == $bundle->getMachineName()));
  368. }
  369. }
  370. return FALSE;
  371. }
  372. /**
  373. * {@inheritdoc}
  374. */
  375. public function listPackageDirectories(array $machine_names = array(), FeaturesBundleInterface $bundle = NULL) {
  376. if (empty($machine_names)) {
  377. $machine_names = array_keys($this->getPackages());
  378. }
  379. // If the bundle is a profile, then add the profile's machine name.
  380. if (isset($bundle) && $bundle->isProfile() && !in_array($bundle->getProfileName(), $machine_names)) {
  381. $machine_names[] = $bundle->getProfileName();
  382. }
  383. // If we are checking the default bundle, return all features.
  384. if (isset($bundle) && $bundle->isDefault()) {
  385. $bundle = NULL;
  386. }
  387. $modules = $this->getFeaturesModules($bundle);
  388. // Filter to include only the requested packages.
  389. $modules = array_filter($modules, function ($module) use ($bundle, $machine_names) {
  390. return in_array($module->getName(), $machine_names);
  391. });
  392. $directories = array();
  393. foreach ($modules as $module) {
  394. $directories[$module->getName()] = $module->getPath();
  395. }
  396. return $directories;
  397. }
  398. /**
  399. * {@inheritdoc}
  400. */
  401. public function getAllModules() {
  402. static $modules;
  403. if (!isset($modules)) {
  404. // ModuleHandler::getModuleDirectories() returns data only for installed
  405. // modules. system_rebuild_module_data() includes only the site's install
  406. // profile directory, while we may need to include a custom profile.
  407. // @see _system_rebuild_module_data().
  408. $listing = new ExtensionDiscovery($this->root);
  409. $profile_directories = $listing->setProfileDirectoriesFromSettings()->getProfileDirectories();
  410. $installed_profile = $this->drupalGetProfile();
  411. if (isset($bundle) && $bundle->isProfile()) {
  412. $profile_directory = 'profiles/' . $bundle->getProfileName();
  413. if (($bundle->getProfileName() != $installed_profile) && is_dir($profile_directory)) {
  414. $profile_directories[] = $profile_directory;
  415. }
  416. }
  417. $listing->setProfileDirectories($profile_directories);
  418. // Find modules.
  419. $modules = $listing->scan('module');
  420. // Find installation profiles.
  421. $profiles = $listing->scan('profile');
  422. foreach ($profiles as $key => $profile) {
  423. $modules[$key] = $profile;
  424. }
  425. }
  426. return $modules;
  427. }
  428. /**
  429. * {@inheritdoc}
  430. */
  431. public function getFeaturesModules(FeaturesBundleInterface $bundle = NULL, $installed = FALSE) {
  432. $modules = $this->getAllModules();
  433. // Filter by bundle.
  434. $features_manager = $this;
  435. $modules = array_filter($modules, function ($module) use ($features_manager, $bundle) {
  436. return $features_manager->isFeatureModule($module, $bundle);
  437. });
  438. // Filtered by installed status.
  439. if ($installed) {
  440. $features_manager = $this;
  441. $modules = array_filter($modules, function ($extension) use ($features_manager) {
  442. return $features_manager->extensionEnabled($extension);
  443. });
  444. }
  445. return $modules;
  446. }
  447. /**
  448. * {@inheritdoc}
  449. */
  450. public function extensionEnabled(Extension $extension) {
  451. return $this->moduleHandler->moduleExists($extension->getName());
  452. }
  453. /**
  454. * {@inheritdoc}
  455. */
  456. public function initPackage($machine_name, $name = NULL, $description = '', $type = 'module', FeaturesBundleInterface $bundle = NULL, Extension $extension = NULL) {
  457. if (isset($this->packages[$machine_name])) {
  458. return $this->packages[$machine_name];
  459. }
  460. // Also look for existing package within the bundle
  461. elseif (isset($bundle) && isset($this->packages[$bundle->getFullName($machine_name)])) {
  462. return $this->packages[$bundle->getFullName($machine_name)];
  463. }
  464. return $this->packages[$machine_name] = $this->getPackageObject($machine_name, $name, $description, $type, $bundle, $extension);
  465. }
  466. /**
  467. * {@inheritdoc}
  468. */
  469. public function initPackageFromExtension(Extension $extension) {
  470. $info = $this->getExtensionInfo($extension);
  471. $features_info = $this->getFeaturesInfo($extension);
  472. $bundle = $this->getAssigner()->findBundle($info, $features_info);
  473. // Use the full extension name as the short_name. Important to allow
  474. // multiple modules with different namespaces such as oa_media, test_media.
  475. $short_name = $extension->getName();
  476. return $this->initPackage($short_name, $info['name'], !empty($info['description']) ? $info['description'] : '', $info['type'], $bundle, $extension);
  477. }
  478. /**
  479. * Helper function to update dependencies array for a specific config item
  480. * @param \Drupal\features\ConfigurationItem $config a config item
  481. * @param array $module_list
  482. * @return array $dependencies
  483. */
  484. protected function getConfigDependency(ConfigurationItem $config, $module_list = array()) {
  485. $dependencies = [];
  486. $type = $config->getType();
  487. if ($type != FeaturesManagerInterface::SYSTEM_SIMPLE_CONFIG) {
  488. $provider = $this->entityTypeManager->getDefinition($type)->getProvider();
  489. // Ensure the provider is an installed module and not, for example, 'core'
  490. if (isset($module_list[$provider])) {
  491. $dependencies[] = $provider;
  492. }
  493. // For configuration in the InstallStorage::CONFIG_INSTALL_DIRECTORY
  494. // directory, set any module dependencies of the configuration item
  495. // as package dependencies.
  496. // As its name implies, the core-provided
  497. // InstallStorage::CONFIG_OPTIONAL_DIRECTORY should not create
  498. // dependencies.
  499. if ($config->getSubdirectory() === InstallStorage::CONFIG_INSTALL_DIRECTORY &&
  500. isset($config->getData()['dependencies']['module'])
  501. ) {
  502. $dependencies = array_merge($dependencies, $config->getData()['dependencies']['module']);
  503. }
  504. }
  505. return $dependencies;
  506. }
  507. /**
  508. * {@inheritdoc}
  509. */
  510. public function assignConfigPackage($package_name, array $item_names, $force = FALSE) {
  511. $config_collection = $this->getConfigCollection();
  512. $module_list = $this->moduleHandler->getModuleList();
  513. $packages =& $this->packages;
  514. if (isset($packages[$package_name])) {
  515. $package =& $packages[$package_name];
  516. }
  517. else {
  518. throw new \Exception($this->t('Failed to package @package_name. Package not found.', ['@package_name' => $package_name]));
  519. }
  520. foreach ($item_names as $item_name) {
  521. if (isset($config_collection[$item_name])) {
  522. // Add to the package if:
  523. // - force is set or
  524. // - the item hasn't already been assigned elsewhere, and
  525. // - the package hasn't been excluded.
  526. // - and the item isn't already in the package.
  527. $item = &$config_collection[$item_name];
  528. $already_assigned = !empty($item->getPackage());
  529. // If this is the profile package, we can reassign extension-provided configuration.
  530. $package_bundle = $this->getAssigner()->getBundle($package->getBundle());
  531. $is_profile_package = isset($package_bundle) ? $package_bundle->isProfilePackage($package_name) : FALSE;
  532. // An item is assignable if:
  533. // - it is not provider excluded or this is the profile package, and
  534. // - it is not flagged as excluded.
  535. $assignable = (!$item->isProviderExcluded() || $is_profile_package) && !$item->isExcluded();
  536. // An item is assignable if it was provided by the current package
  537. $assignable = $assignable || ($item->getProvider() == $package->getMachineName());
  538. $excluded_from_package = in_array($package_name, $item->getPackageExcluded());
  539. $already_in_package = in_array($item_name, $package->getConfig());
  540. if (($force || (!$already_assigned && $assignable && !$excluded_from_package)) && !$already_in_package) {
  541. // Add the item to the package's config array.
  542. $package->appendConfig($item_name);
  543. // Mark the item as already assigned.
  544. $item->setPackage($package_name);
  545. $module_dependencies = $this->getConfigDependency($item, $module_list);
  546. $package->setDependencies($this->mergeUniqueItems($package->getDependencies(), $module_dependencies));
  547. }
  548. // Return memory
  549. unset($item);
  550. }
  551. }
  552. $this->setConfigCollection($config_collection);
  553. }
  554. /**
  555. * {@inheritdoc}
  556. */
  557. public function assignConfigByPattern(array $patterns) {
  558. // Regular expressions for items that are likely to generate false
  559. // positives when assigned by pattern.
  560. $false_positives = [
  561. // Blocks with the page title should not be assigned to a 'page' package.
  562. '/block\.block\..*_page_title/',
  563. ];
  564. $config_collection = $this->getConfigCollection();
  565. // Sort by key so that specific package will claim items before general
  566. // package. E.g., event_registration and registration_event will claim
  567. // before event.
  568. uksort($patterns, function($a, $b) {
  569. // Count underscores to determine specificity of the package.
  570. return (int) (substr_count($a, '_') <= substr_count($b, '_'));
  571. });
  572. foreach ($patterns as $pattern => $machine_name) {
  573. if (isset($this->packages[$machine_name])) {
  574. foreach ($config_collection as $item_name => $item) {
  575. // Test for and skip false positives.
  576. foreach ($false_positives as $false_positive) {
  577. if (preg_match($false_positive, $item_name)) {
  578. continue 2;
  579. }
  580. }
  581. if (!$item->getPackage() && preg_match('/(\.|-|_|^)' . $pattern . '(\.|-|_|$)/', $item->getShortName())) {
  582. try {
  583. $this->assignConfigPackage($machine_name, [$item_name]);
  584. }
  585. catch (\Exception $exception) {
  586. \Drupal::logger('features')->error($exception->getMessage());
  587. }
  588. }
  589. }
  590. }
  591. }
  592. }
  593. /**
  594. * {@inheritdoc}
  595. */
  596. public function assignConfigDependents(array $item_names = NULL, $package = NULL) {
  597. $config_collection = $this->getConfigCollection();
  598. if (empty($item_names)) {
  599. $item_names = array_keys($config_collection);
  600. }
  601. foreach ($item_names as $item_name) {
  602. // Make sure the extension provided item exists in the active
  603. // configuration storage.
  604. if (isset($config_collection[$item_name]) && $config_collection[$item_name]->getPackage()) {
  605. foreach ($config_collection[$item_name]->getDependents() as $dependent_item_name) {
  606. if (isset($config_collection[$dependent_item_name]) && (!empty($package) || empty($config_collection[$dependent_item_name]->getPackage()))) {
  607. try {
  608. $package_name = !empty($package) ? $package : $config_collection[$item_name]->getPackage();
  609. $this->assignConfigPackage($package_name, [$dependent_item_name]);
  610. }
  611. catch (\Exception $exception) {
  612. \Drupal::logger('features')->error($exception->getMessage());
  613. }
  614. }
  615. }
  616. }
  617. }
  618. }
  619. /**
  620. * {@inheritdoc}
  621. */
  622. public function setPackageBundleNames(FeaturesBundleInterface $bundle, array &$package_names = []) {
  623. $this->packagesPrefixed = TRUE;
  624. if (!$bundle->isDefault()) {
  625. $new_package_names = [];
  626. // Assign the selected bundle to the exports.
  627. $packages = $this->getPackages();
  628. if (empty($package_names)) {
  629. $package_names = array_keys($packages);
  630. }
  631. foreach ($package_names as $package_name) {
  632. // Rename package to use bundle prefix.
  633. $package = $packages[$package_name];
  634. // The install profile doesn't need renaming.
  635. if ($package->getType() != 'profile') {
  636. unset($packages[$package_name]);
  637. $package->setMachineName($bundle->getFullName($package->getMachineName()));
  638. $packages[$package->getMachineName()] = $package;
  639. }
  640. // Set the bundle machine name.
  641. $packages[$package->getMachineName()]->setBundle($bundle->getMachineName());
  642. $new_package_names[] = $package->getMachineName();
  643. }
  644. $this->setPackages($packages);
  645. $package_names = $new_package_names;
  646. }
  647. }
  648. /**
  649. * {@inheritdoc}
  650. */
  651. public function assignPackageDependencies(Package $package = NULL) {
  652. if (is_null($package)) {
  653. $packages = $this->getPackages();
  654. }
  655. else {
  656. $packages = array($package);
  657. }
  658. $module_list = $this->moduleHandler->getModuleList();
  659. $config_collection = $this->getConfigCollection();
  660. foreach ($packages as $package) {
  661. $module_dependencies = [];
  662. foreach ($package->getConfig() as $item_name) {
  663. if (isset($config_collection[$item_name])) {
  664. $dependencies = $this->getConfigDependency($config_collection[$item_name], $module_list);
  665. $module_dependencies = array_merge($module_dependencies, $dependencies);
  666. }
  667. }
  668. $package->setDependencies($this->mergeUniqueItems($package->getDependencies(), $module_dependencies));
  669. }
  670. }
  671. /**
  672. * {@inheritdoc}
  673. */
  674. public function assignInterPackageDependencies(FeaturesBundleInterface $bundle, array &$packages) {
  675. if (!$this->packagesPrefixed) {
  676. throw new \Exception($this->t('The packages have not yet been prefixed with a bundle name.'));
  677. }
  678. $config_collection = $this->getConfigCollection();
  679. /** @var \Drupal\features\Package[] $packages */
  680. foreach ($packages as $package) {
  681. foreach ($package->getConfig() as $item_name) {
  682. if (!empty($config_collection[$item_name]->getData()['dependencies']['config'])) {
  683. foreach ($config_collection[$item_name]->getData()['dependencies']['config'] as $dependency_name) {
  684. if (isset($config_collection[$dependency_name])) {
  685. // If the required item is assigned to one of the packages, add
  686. // a dependency on that package.
  687. $dependency_set = FALSE;
  688. if ($dependency_package = $config_collection[$dependency_name]->getPackage()) {
  689. $package_name = $bundle->getFullName($dependency_package);
  690. // Package shouldn't be dependent on itself.
  691. if ($package_name && array_key_exists($package_name, $packages) && $package_name != $package->getMachineName()) {
  692. $package->setDependencies($this->mergeUniqueItems($package->getDependencies(), [$package_name]));
  693. $dependency_set = TRUE;
  694. }
  695. }
  696. // Otherwise, if the dependency is provided by an existing
  697. // feature, add a dependency on that feature.
  698. if (!$dependency_set && $extension_name = $config_collection[$dependency_name]->getProvider()) {
  699. // No extension should depend on the install profile.
  700. $package_name = $bundle->getFullName($package->getMachineName());
  701. if ($extension_name != $package_name && $extension_name != $this->drupalGetProfile()) {
  702. $package->setDependencies($this->mergeUniqueItems($package->getDependencies(), [$extension_name]));
  703. }
  704. }
  705. }
  706. }
  707. }
  708. }
  709. }
  710. // Unset the $package pass by reference.
  711. unset($package);
  712. }
  713. /**
  714. * Gets the name of the currently active installation profile.
  715. *
  716. * @return string|null $profile
  717. * The name of the installation profile or NULL if no installation profile is
  718. * currently active. This is the case for example during the first steps of
  719. * the installer or during unit tests.
  720. */
  721. protected function drupalGetProfile() {
  722. return drupal_get_profile();
  723. }
  724. /**
  725. * Merges a set of new item into an array and sorts the result.
  726. *
  727. * Only unique values are retained.
  728. *
  729. * @param array $items
  730. * An array of items.
  731. * @param array $new_items
  732. * An array of new items to be merged in.
  733. *
  734. * @return array
  735. * The merged, sorted and unique items.
  736. */
  737. protected function mergeUniqueItems($items, $new_items) {
  738. $items = array_unique(array_merge($items, $new_items));
  739. sort($items);
  740. return $items;
  741. }
  742. /**
  743. * Initializes and returns a package or profile array.
  744. *
  745. * @param string $machine_name
  746. * Machine name of the package.
  747. * @param string $name
  748. * (optional) Human readable name of the package.
  749. * @param string $description
  750. * (optional) Description of the package.
  751. * @param string $type
  752. * (optional) Type of project.
  753. * @param \Drupal\features\FeaturesBundleInterface $bundle
  754. * (optional) Bundle to use to add profile directories to the scan.
  755. * @param \Drupal\Core\Extension\Extension $extension
  756. * (optional) An Extension object.
  757. *
  758. * @return \Drupal\features\Package
  759. * An array of package properties; see
  760. * FeaturesManagerInterface::getPackages().
  761. */
  762. protected function getPackageObject($machine_name, $name = NULL, $description = '', $type = 'module', FeaturesBundleInterface $bundle = NULL, Extension $extension = NULL) {
  763. if (!isset($bundle)) {
  764. $bundle = $this->getAssigner()->getBundle();
  765. }
  766. $package = new Package($machine_name, [
  767. 'name' => isset($name) ? $name : ucwords(str_replace(['_', '-'], ' ', $machine_name)),
  768. 'description' => $description,
  769. 'type' => $type,
  770. 'core' => Drupal::CORE_COMPATIBILITY,
  771. 'dependencies' => [],
  772. 'themes' => [],
  773. 'config' => [],
  774. 'status' => FeaturesManagerInterface::STATUS_DEFAULT,
  775. 'version' => '',
  776. 'state' => FeaturesManagerInterface::STATE_DEFAULT,
  777. 'files' => [],
  778. 'bundle' => $bundle->isDefault() ? '' : $bundle->getMachineName(),
  779. 'extension' => NULL,
  780. 'info' => [],
  781. 'configOrig' => [],
  782. ]);
  783. // If no extension was passed in, look for a match.
  784. if (!isset($extension)) {
  785. $module_list = $this->getFeaturesModules($bundle);
  786. $module_name = $package->getMachineName();
  787. if (isset($module_list[$module_name])) {
  788. $extension = $module_list[$module_name];
  789. }
  790. }
  791. // If there is an extension, set extension-specific properties.
  792. if (isset($extension)) {
  793. $info = $this->getExtensionInfo($extension);
  794. $features_info = $this->getFeaturesInfo($extension);
  795. $package->setExtension($extension);
  796. $package->setInfo($info);
  797. $package->setFeaturesInfo($features_info);
  798. $package->setConfigOrig($this->listExtensionConfig($extension));
  799. $package->setStatus($this->extensionEnabled($extension)
  800. ? FeaturesManagerInterface::STATUS_INSTALLED
  801. : FeaturesManagerInterface::STATUS_UNINSTALLED);
  802. $package->setVersion(isset($info['version']) ? $info['version'] : '');
  803. }
  804. return $package;
  805. }
  806. /**
  807. * Generates and adds .info.yml files to a package.
  808. *
  809. * @param \Drupal\features\Package $package
  810. * The package.
  811. */
  812. protected function addInfoFile(Package $package) {
  813. $info = [
  814. 'name' => $package->getName(),
  815. 'description' => $package->getDescription(),
  816. 'type' => $package->getType(),
  817. 'core' => $package->getCore(),
  818. 'dependencies' => $package->getDependencies(),
  819. 'themes' => $package->getThemes(),
  820. 'version' => $package->getVersion(),
  821. ];
  822. $features_info = [];
  823. // Assign to a "package" named for the profile.
  824. if ($package->getBundle()) {
  825. $bundle = $this->getAssigner()->getBundle($package->getBundle());
  826. }
  827. // Save the current bundle in the info file so the package
  828. // can be reloaded later by the AssignmentPackages plugin.
  829. if (isset($bundle) && !$bundle->isDefault()) {
  830. $info['package'] = $bundle->getName();
  831. $features_info['bundle'] = $bundle->getMachineName();
  832. }
  833. else {
  834. unset($features_info['bundle']);
  835. }
  836. if ($package->getConfig()) {
  837. foreach (array('excluded', 'required') as $constraint) {
  838. if (!empty($package->{'get' . $constraint}())) {
  839. $features_info[$constraint] = $package->{'get' . $constraint}();
  840. }
  841. else {
  842. unset($features_info[$constraint]);
  843. }
  844. }
  845. if (empty($features_info)) {
  846. $features_info = TRUE;
  847. }
  848. }
  849. // The name and description need to be cast as strings from the
  850. // TranslatableMarkup objects returned by t() to avoid raising an
  851. // InvalidDataTypeException on Yaml serialization.
  852. foreach (array('name', 'description') as $key) {
  853. $info[$key] = (string) $info[$key];
  854. }
  855. // Add profile-specific info data.
  856. if ($info['type'] == 'profile') {
  857. // Set the distribution name.
  858. $info['distribution'] = [
  859. 'name' => $info['name']
  860. ];
  861. }
  862. $package->appendFile([
  863. 'filename' => $package->getMachineName() . '.info.yml',
  864. 'subdirectory' => NULL,
  865. // Filter to remove any empty keys, e.g., an empty themes array.
  866. 'string' => Yaml::encode(array_filter($info))
  867. ], 'info');
  868. $package->appendFile([
  869. 'filename' => $package->getMachineName() . '.features.yml',
  870. 'subdirectory' => NULL,
  871. 'string' => Yaml::encode($features_info)
  872. ], 'features');
  873. }
  874. /**
  875. * Generates and adds files to a given package or profile.
  876. */
  877. protected function addPackageFiles(Package $package) {
  878. $config_collection = $this->getConfigCollection();
  879. // Always add .info.yml and .features.yml files.
  880. $this->addInfoFile($package);
  881. // Only add files if there is at least one piece of configuration present.
  882. if ($package->getConfig()) {
  883. // Add configuration files.
  884. foreach ($package->getConfig() as $name) {
  885. $config = $config_collection[$name];
  886. $package->appendFile([
  887. 'filename' => $config->getName() . '.yml',
  888. 'subdirectory' => $config->getSubdirectory(),
  889. 'string' => Yaml::encode($config->getData())
  890. ], $name);
  891. }
  892. }
  893. }
  894. /**
  895. * {@inheritdoc}
  896. */
  897. public function mergeInfoArray(array $info1, array $info2, array $keys = array()) {
  898. // If keys were specified, use only those.
  899. if (!empty($keys)) {
  900. $info2 = array_intersect_key($info2, array_fill_keys($keys, NULL));
  901. }
  902. $info = NestedArray::mergeDeep($info1, $info2);
  903. // Process the dependencies and themes keys.
  904. $keys = ['dependencies', 'themes'];
  905. foreach ($keys as $key) {
  906. if (isset($info[$key]) && is_array($info[$key])) {
  907. // NestedArray::mergeDeep() may produce duplicate values.
  908. $info[$key] = array_unique($info[$key]);
  909. sort($info[$key]);
  910. }
  911. }
  912. return $info;
  913. }
  914. /**
  915. * {@inheritdoc}
  916. */
  917. public function listConfigTypes($bundles_only = FALSE) {
  918. $definitions = [];
  919. foreach ($this->entityTypeManager->getDefinitions() as $entity_type => $definition) {
  920. if ($definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
  921. if (!$bundles_only || $definition->getBundleOf()) {
  922. $definitions[$entity_type] = $definition;
  923. }
  924. }
  925. }
  926. $entity_types = array_map(function (EntityTypeInterface $definition) {
  927. return $definition->getLabel();
  928. }, $definitions);
  929. // Sort the entity types by label, then add the simple config to the top.
  930. uasort($entity_types, 'strnatcasecmp');
  931. return $bundles_only ? $entity_types : [
  932. FeaturesManagerInterface::SYSTEM_SIMPLE_CONFIG => $this->t('Simple configuration'),
  933. ] + $entity_types;
  934. }
  935. /**
  936. * {@inheritdoc}
  937. */
  938. public function listExtensionConfig(Extension $extension) {
  939. return $this->extensionStorages->listExtensionConfig($extension);
  940. }
  941. /**
  942. * {@inheritdoc}
  943. */
  944. public function listExistingConfig($installed = FALSE, FeaturesBundleInterface $bundle = NULL) {
  945. $config = array();
  946. $existing = $this->getFeaturesModules($bundle, $installed);
  947. foreach ($existing as $extension) {
  948. // Keys are configuration item names and values are providing extension
  949. // name.
  950. $new_config = array_fill_keys($this->listExtensionConfig($extension), $extension->getName());
  951. $config = array_merge($config, $new_config);
  952. }
  953. return $config;
  954. }
  955. /**
  956. * {@inheritdoc}
  957. */
  958. public function listConfigByType($config_type) {
  959. // For a given entity type, load all entities.
  960. if ($config_type && $config_type !== FeaturesManagerInterface::SYSTEM_SIMPLE_CONFIG) {
  961. $entity_storage = $this->entityTypeManager->getStorage($config_type);
  962. $names = [];
  963. foreach ($entity_storage->loadMultiple() as $entity) {
  964. $entity_id = $entity->id();
  965. $label = $entity->label() ?: $entity_id;
  966. $names[$entity_id] = $label;
  967. }
  968. }
  969. // Handle simple configuration.
  970. else {
  971. $definitions = [];
  972. foreach ($this->entityTypeManager->getDefinitions() as $entity_type => $definition) {
  973. if ($definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
  974. $definitions[$entity_type] = $definition;
  975. }
  976. }
  977. // Gather the config entity prefixes.
  978. $config_prefixes = array_map(function (EntityTypeInterface $definition) {
  979. return $definition->getConfigPrefix() . '.';
  980. }, $definitions);
  981. // Find all config, and then filter our anything matching a config prefix.
  982. $names = $this->configStorage->listAll();
  983. $names = array_combine($names, $names);
  984. foreach ($names as $item_name) {
  985. foreach ($config_prefixes as $config_prefix) {
  986. if (strpos($item_name, $config_prefix) === 0) {
  987. unset($names[$item_name]);
  988. }
  989. }
  990. }
  991. }
  992. return $names;
  993. }
  994. /**
  995. * Creates a high performant version of the ConfigDependencyManager.
  996. *
  997. * @return \Drupal\features\FeaturesConfigDependencyManager
  998. * A high performant version of the ConfigDependencyManager.
  999. *
  1000. * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
  1001. */
  1002. protected function getFeaturesConfigDependencyManager() {
  1003. $dependency_manager = new FeaturesConfigDependencyManager();
  1004. // Read all configuration using the factory. This ensures that multiple
  1005. // deletes during the same request benefit from the static cache. Using the
  1006. // factory also ensures configuration entity dependency discovery has no
  1007. // dependencies on the config entity classes. Assume data with UUID is a
  1008. // config entity. Only configuration entities can be depended on so we can
  1009. // ignore everything else.
  1010. $data = array_map(function(Drupal\Core\Config\ImmutableConfig $config) {
  1011. $data = $config->get();
  1012. if (isset($data['uuid'])) {
  1013. return $data;
  1014. }
  1015. return FALSE;
  1016. }, $this->configFactory->loadMultiple($this->configStorage->listAll()));
  1017. $dependency_manager->setData(array_filter($data));
  1018. return $dependency_manager;
  1019. }
  1020. /**
  1021. * Loads configuration from storage into a property.
  1022. */
  1023. protected function initConfigCollection($reset = FALSE) {
  1024. if ($reset || empty($this->configCollection)) {
  1025. $config_collection = [];
  1026. $config_types = $this->listConfigTypes();
  1027. $dependency_manager = $this->getFeaturesConfigDependencyManager();
  1028. // List configuration provided by installed features.
  1029. $existing_config = $this->listExistingConfig(NULL);
  1030. foreach (array_keys($config_types) as $config_type) {
  1031. $config = $this->listConfigByType($config_type);
  1032. foreach ($config as $item_name => $label) {
  1033. $name = $this->getFullName($config_type, $item_name);
  1034. $data = $this->configStorage->read($name);
  1035. $config_collection[$name] = (new ConfigurationItem($name, $data, [
  1036. 'shortName' => $item_name,
  1037. 'label' => $label,
  1038. 'type' => $config_type,
  1039. 'dependents' => array_keys($dependency_manager->getDependentEntities('config', $name)),
  1040. // Default to the install directory.
  1041. 'subdirectory' => InstallStorage::CONFIG_INSTALL_DIRECTORY,
  1042. 'package' => '',
  1043. 'providerExcluded' => NULL,
  1044. 'provider' => isset($existing_config[$name]) ? $existing_config[$name] : NULL,
  1045. 'packageExcluded' => [],
  1046. ]));
  1047. }
  1048. }
  1049. $this->setConfigCollection($config_collection);
  1050. }
  1051. }
  1052. /**
  1053. * {@inheritdoc}
  1054. */
  1055. public function prepareFiles(array $packages) {
  1056. foreach ($packages as $package) {
  1057. $this->addPackageFiles($package);
  1058. }
  1059. }
  1060. /**
  1061. * {@inheritdoc}
  1062. */
  1063. public function getExportInfo(Package $package, FeaturesBundleInterface $bundle = NULL) {
  1064. $full_name = isset($bundle) ? $bundle->getFullName($package->getMachineName()) : $package->getMachineName();
  1065. $path = '';
  1066. // Adjust export directory to be in profile.
  1067. if (isset($bundle) && $bundle->isProfile()) {
  1068. $path .= 'profiles/' . $bundle->getProfileName();
  1069. }
  1070. // If this is not the profile package, nest the directory.
  1071. if (!isset($bundle) || !$bundle->isProfilePackage($package->getMachineName())) {
  1072. $path .= empty($path) ? 'modules' : '/modules';
  1073. $export_settings = $this->getExportSettings();
  1074. if (!empty($export_settings['folder'])) {
  1075. $path .= '/' . $export_settings['folder'];
  1076. }
  1077. }
  1078. // Use the same path of a package to override it.
  1079. if ($extension = $package->getExtension()) {
  1080. $extension_path = $extension->getPath();
  1081. $path = dirname($extension_path);
  1082. }
  1083. return array($full_name, $path);
  1084. }
  1085. /**
  1086. * {@inheritdoc}
  1087. */
  1088. public function detectOverrides(Package $feature, $include_new = FALSE) {
  1089. /** @var \Drupal\config_update\ConfigDiffInterface $config_diff */
  1090. $config_diff = \Drupal::service('config_update.config_diff');
  1091. $different = array();
  1092. foreach ($feature->getConfig() as $name) {
  1093. $active = $this->configStorage->read($name);
  1094. $extension = $this->extensionStorages->read($name);
  1095. $extension = !empty($extension) ? $extension : array();
  1096. if (($include_new || !empty($extension)) && !$config_diff->same($extension, $active)) {
  1097. $different[] = $name;
  1098. }
  1099. }
  1100. if (!empty($different)) {
  1101. $feature->setState(FeaturesManagerInterface::STATE_OVERRIDDEN);
  1102. }
  1103. return $different;
  1104. }
  1105. /**
  1106. * {@inheritdoc}
  1107. */
  1108. public function detectNew(Package $feature) {
  1109. $result = array();
  1110. foreach ($feature->getConfig() as $name) {
  1111. $extension = $this->extensionStorages->read($name);
  1112. if (empty($extension)) {
  1113. $result[] = $name;
  1114. }
  1115. }
  1116. return $result;
  1117. }
  1118. /**
  1119. * {@inheritdoc}
  1120. */
  1121. public function detectMissing(Package $feature) {
  1122. $config = $this->getConfigCollection();
  1123. $result = array();
  1124. foreach ($feature->getConfigOrig() as $name) {
  1125. if (!isset($config[$name])) {
  1126. $result[] = $name;
  1127. }
  1128. }
  1129. return $result;
  1130. }
  1131. /**
  1132. * {@inheritdoc}
  1133. */
  1134. public function reorderMissing(array $missing) {
  1135. $list = array();
  1136. $result = array();
  1137. foreach ($missing as $full_name) {
  1138. $this->addConfigList($full_name, $list);
  1139. }
  1140. foreach ($list as $full_name) {
  1141. if (in_array($full_name, $missing)) {
  1142. $result[] = $full_name;
  1143. }
  1144. }
  1145. return $result;
  1146. }
  1147. protected function addConfigList($full_name, &$list) {
  1148. $index = array_search($full_name, $list);
  1149. if ($index !== FALSE) {
  1150. unset($list[$index]);
  1151. }
  1152. array_unshift($list, $full_name);
  1153. $value = $this->extensionStorages->read($full_name);
  1154. if (isset($value['dependencies']['config'])) {
  1155. foreach ($value['dependencies']['config'] as $config_name) {
  1156. $this->addConfigList($config_name, $list);
  1157. }
  1158. }
  1159. }
  1160. /**
  1161. * {@inheritdoc}
  1162. */
  1163. public function statusLabel($status) {
  1164. switch ($status) {
  1165. case FeaturesManagerInterface::STATUS_NO_EXPORT:
  1166. return $this->t('Not exported');
  1167. case FeaturesManagerInterface::STATUS_UNINSTALLED:
  1168. return $this->t('Uninstalled');
  1169. case FeaturesManagerInterface::STATUS_INSTALLED:
  1170. return $this->t('Installed');
  1171. }
  1172. }
  1173. /**
  1174. * {@inheritdoc}
  1175. */
  1176. public function stateLabel($state) {
  1177. switch ($state) {
  1178. case FeaturesManagerInterface::STATE_DEFAULT:
  1179. return $this->t('Default');
  1180. case FeaturesManagerInterface::STATE_OVERRIDDEN:
  1181. return $this->t('Changed');
  1182. }
  1183. }
  1184. /**
  1185. * {@inheritdoc}
  1186. */
  1187. public function getFeaturesInfo(Extension $extension) {
  1188. $module_name = $extension->getName();
  1189. if (isset($this->featureInfoCache[$module_name])) {
  1190. return $this->featureInfoCache[$module_name];
  1191. }
  1192. $features_info = NULL;
  1193. $filename = $this->root . '/' . $extension->getPath() . '/' . $module_name . '.features.yml';
  1194. if (file_exists($filename)) {
  1195. $features_info = Yaml::decode(file_get_contents($filename));
  1196. }
  1197. $this->featureInfoCache[$module_name] = $features_info;
  1198. return $features_info;
  1199. }
  1200. /**
  1201. * {@inheritdoc}
  1202. */
  1203. public function createConfiguration(array $config_to_create) {
  1204. $existing_config = $this->getConfigCollection();
  1205. // If config data is not specified, load it from the extension storage.
  1206. foreach ($config_to_create as $name => $item) {
  1207. if (empty($item)) {
  1208. $config = $this->configReverter->getFromExtension('', $name);
  1209. // For testing purposes, if it couldn't load from a module, get config
  1210. // from the cached Config Collection
  1211. if (empty($config) && isset($existing_config[$name])) {
  1212. $config = $existing_config[$name]->getData();
  1213. }
  1214. $config_to_create[$name] = $config;
  1215. }
  1216. }
  1217. // Determine which config is new vs existing.
  1218. $existing = array_intersect_key($config_to_create, $existing_config);
  1219. $new = array_diff_key($config_to_create, $existing);
  1220. // The FeaturesConfigInstaller exposes the normally protected createConfiguration
  1221. // function from Core ConfigInstaller than handles the creation of new
  1222. // config or the changing of existing config.
  1223. /** @var \Drupal\features\FeaturesConfigInstaller $config_installer */
  1224. $config_installer = \Drupal::service('features.config.installer');
  1225. $config_installer->createConfiguration(StorageInterface::DEFAULT_COLLECTION, $config_to_create);
  1226. // Collect results for new and updated config.
  1227. $new_config = $this->getConfigCollection(TRUE);
  1228. $result['updated'] = array_intersect_key($new_config, $existing);
  1229. $result['new'] = array_intersect_key($new_config, $new);
  1230. return $result;
  1231. }
  1232. /**
  1233. * {@inheritdoc}
  1234. */
  1235. public function import($modules, $any = FALSE) {
  1236. $result = [];
  1237. foreach ($modules as $module_name) {
  1238. $package = $this->loadPackage($module_name, $any);
  1239. $components = isset($package) ? $package->getConfigOrig() : [];
  1240. if (empty($components)) {
  1241. continue;
  1242. }
  1243. $result[$module_name] = $this->createConfiguration(array_fill_keys($components, []));
  1244. }
  1245. return $result;
  1246. }
  1247. }