ConflictChecker.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Module;
  7. use Composer\Package\Version\VersionParser;
  8. /**
  9. * Checks for conflicts between modules
  10. */
  11. class ConflictChecker
  12. {
  13. /**
  14. * Enabled module list
  15. *
  16. * @var ModuleList
  17. */
  18. private $list;
  19. /**
  20. * Composer package info
  21. *
  22. * @var PackageInfo
  23. */
  24. private $packageInfo;
  25. /**
  26. * Constructor
  27. *
  28. * @param ModuleList $list
  29. * @param PackageInfoFactory $packageInfoFactory
  30. */
  31. public function __construct(ModuleList $list, PackageInfoFactory $packageInfoFactory)
  32. {
  33. $this->list = $list;
  34. $this->packageInfo = $packageInfoFactory->create();
  35. }
  36. /**
  37. * Check if enabling module will conflict any modules
  38. *
  39. * @param string[] $moduleNames
  40. * @param string[] $currentlyEnabledModules
  41. *
  42. * @return array
  43. */
  44. public function checkConflictsWhenEnableModules($moduleNames, $currentlyEnabledModules = null)
  45. {
  46. $masterList = isset($currentlyEnabledModules) ? $currentlyEnabledModules: $this->list->getNames();
  47. // union of currently enabled modules and to-be-enabled modules
  48. $enabledModules = array_unique(array_merge($masterList, $moduleNames));
  49. $conflictsAll = [];
  50. foreach ($moduleNames as $moduleName) {
  51. $conflicts = [];
  52. foreach ($enabledModules as $enabledModule) {
  53. $messages = $this->getConflictMessages($enabledModule, $moduleName);
  54. if (!empty($messages)) {
  55. $conflicts[] = implode("\n", $messages);
  56. }
  57. }
  58. $conflictsAll[$moduleName] = $conflicts;
  59. }
  60. return $conflictsAll;
  61. }
  62. /**
  63. * Check if two modules are conflicted and get the message for display
  64. *
  65. * @param string $moduleA
  66. * @param string $moduleB
  67. * @return string[]
  68. */
  69. private function getConflictMessages($moduleA, $moduleB)
  70. {
  71. $messages = [];
  72. $versionParser = new VersionParser();
  73. if (isset($this->packageInfo->getConflict($moduleB)[$moduleA]) &&
  74. $this->packageInfo->getConflict($moduleB)[$moduleA] &&
  75. $this->packageInfo->getVersion($moduleA)
  76. ) {
  77. $constraintA = $versionParser->parseConstraints($this->packageInfo->getConflict($moduleB)[$moduleA]);
  78. $constraintB = $versionParser->parseConstraints($this->packageInfo->getVersion($moduleA));
  79. if ($constraintA->matches($constraintB)) {
  80. $messages[] = "$moduleB conflicts with current $moduleA version " .
  81. $this->packageInfo->getVersion($moduleA) .
  82. ' (version should not be ' . $this->packageInfo->getConflict($moduleB)[$moduleA] . ')';
  83. }
  84. }
  85. if (isset($this->packageInfo->getConflict($moduleA)[$moduleB]) &&
  86. $this->packageInfo->getConflict($moduleA)[$moduleB] &&
  87. $this->packageInfo->getVersion($moduleB)
  88. ) {
  89. $constraintA = $versionParser->parseConstraints($this->packageInfo->getConflict($moduleA)[$moduleB]);
  90. $constraintB = $versionParser->parseConstraints($this->packageInfo->getVersion($moduleB));
  91. if ($constraintA->matches($constraintB)) {
  92. $messages[] = "$moduleA conflicts with current $moduleB version " .
  93. $this->packageInfo->getVersion($moduleA) .
  94. ' (version should not be ' . $this->packageInfo->getConflict($moduleA)[$moduleB] . ')';
  95. }
  96. }
  97. return $messages;
  98. }
  99. }