Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConflictChecker.php
Go to the documentation of this file.
1 <?php
7 
8 use Composer\Package\Version\VersionParser;
9 
14 {
20  private $list;
21 
27  private $packageInfo;
28 
35  public function __construct(ModuleList $list, PackageInfoFactory $packageInfoFactory)
36  {
37  $this->list = $list;
38  $this->packageInfo = $packageInfoFactory->create();
39  }
40 
49  public function checkConflictsWhenEnableModules($moduleNames, $currentlyEnabledModules = null)
50  {
51  $masterList = isset($currentlyEnabledModules) ? $currentlyEnabledModules: $this->list->getNames();
52  // union of currently enabled modules and to-be-enabled modules
53  $enabledModules = array_unique(array_merge($masterList, $moduleNames));
54  $conflictsAll = [];
55  foreach ($moduleNames as $moduleName) {
56  $conflicts = [];
57  foreach ($enabledModules as $enabledModule) {
58  $messages = $this->getConflictMessages($enabledModule, $moduleName);
59  if (!empty($messages)) {
60  $conflicts[] = implode("\n", $messages);
61  }
62  }
63  $conflictsAll[$moduleName] = $conflicts;
64  }
65  return $conflictsAll;
66  }
67 
75  private function getConflictMessages($moduleA, $moduleB)
76  {
77  $messages = [];
78  $versionParser = new VersionParser();
79  if (isset($this->packageInfo->getConflict($moduleB)[$moduleA]) &&
80  $this->packageInfo->getConflict($moduleB)[$moduleA] &&
81  $this->packageInfo->getVersion($moduleA)
82  ) {
83  $constraintA = $versionParser->parseConstraints($this->packageInfo->getConflict($moduleB)[$moduleA]);
84  $constraintB = $versionParser->parseConstraints($this->packageInfo->getVersion($moduleA));
85  if ($constraintA->matches($constraintB)) {
86  $messages[] = "$moduleB conflicts with current $moduleA version " .
87  $this->packageInfo->getVersion($moduleA) .
88  ' (version should not be ' . $this->packageInfo->getConflict($moduleB)[$moduleA] . ')';
89  }
90  }
91  if (isset($this->packageInfo->getConflict($moduleA)[$moduleB]) &&
92  $this->packageInfo->getConflict($moduleA)[$moduleB] &&
93  $this->packageInfo->getVersion($moduleB)
94  ) {
95  $constraintA = $versionParser->parseConstraints($this->packageInfo->getConflict($moduleA)[$moduleB]);
96  $constraintB = $versionParser->parseConstraints($this->packageInfo->getVersion($moduleB));
97  if ($constraintA->matches($constraintB)) {
98  $messages[] = "$moduleA conflicts with current $moduleB version " .
99  $this->packageInfo->getVersion($moduleA) .
100  ' (version should not be ' . $this->packageInfo->getConflict($moduleA)[$moduleB] . ')';
101  }
102  }
103  return $messages;
104  }
105 }
__construct(ModuleList $list, PackageInfoFactory $packageInfoFactory)
checkConflictsWhenEnableModules($moduleNames, $currentlyEnabledModules=null)