Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RequireUpdateDryRunCommand.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Composer;
8 
13 {
18 
22  protected $infoCommand;
23 
30  public function __construct(
33  ) {
34  $this->magentoComposerApplication = $magentoComposerApplication;
35  $this->infoCommand = $infoCommand;
36  }
37 
46  public function run($packages, $workingDir = null)
47  {
48  try {
49  // run require
50  $this->magentoComposerApplication->runComposerCommand(
51  ['command' => 'require', 'packages' => $packages, '--no-update' => true],
52  $workingDir
53  );
54 
55  $output = $this->magentoComposerApplication->runComposerCommand(
56  ['command' => 'update', '--dry-run' => true],
57  $workingDir
58  );
59  } catch (\RuntimeException $e) {
60  $errorMessage = $this->generateAdditionalErrorMessage($e->getMessage(), $packages);
61  if ($errorMessage) {
62  throw new \RuntimeException($errorMessage, $e->getCode(), $e);
63  } else {
64  throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
65  }
66 
67  }
68 
69  return $output;
70  }
71 
79  protected function generateAdditionalErrorMessage($message, $inputPackages)
80  {
81  $matches = [];
82  $errorMessage = '';
83  $packages = [];
84  $rawLines = explode(PHP_EOL, $message);
85 
86  foreach ($rawLines as $line) {
87  if (preg_match('/- (.*) requires (.*) -> no matching package/', $line, $matches)) {
88  $packages[] = $matches[1];
89  $packages[] = $matches[2];
90  }
91  }
92 
93  if (!empty($packages)) {
94  $packages = array_unique($packages);
95  $packages = $this->explodePackagesAndVersions($packages);
96  $inputPackages = $this->explodePackagesAndVersions($inputPackages);
97 
98  $update = [];
99  $conflicts = [];
100 
101  foreach ($inputPackages as $package => $version) {
102  if (isset($packages[$package])) {
103  $update[] = $package . ' to ' . $version;
104  }
105  }
106 
107  foreach (array_diff_key($packages, $inputPackages) as $package => $version) {
108 
109  if (!$packageInfo = $this->infoCommand->run($package, true)) {
110  return false;
111  }
112 
113  $currentVersion = $packageInfo[InfoCommand::CURRENT_VERSION];
114 
115  if (empty($packageInfo[InfoCommand::AVAILABLE_VERSIONS])) {
116  $packageInfo = $this->infoCommand->run($package);
117  if (empty($packageInfo[InfoCommand::AVAILABLE_VERSIONS])) {
118  return false;
119  }
120  }
121 
122  $conflicts[] = ' - ' . $package . ' version ' . $currentVersion . '. '
123  . 'Please try to update it to one of the following package versions: '
124  . implode(', ', $packageInfo['available_versions']);
125  }
126 
127  $errorMessage = 'You are trying to update package(s) '
128  . implode(', ', $update) . PHP_EOL
129  . "We've detected conflicts with the following packages:" . PHP_EOL
130  . implode(PHP_EOL, $conflicts)
131  . PHP_EOL;
132  }
133 
134  return $errorMessage;
135  }
136 
143  protected function explodePackagesAndVersions($packages)
144  {
145  $packagesAndVersions = [];
146  foreach ($packages as $package) {
147  $package = explode(' ', $package);
148  $packagesAndVersions[$package[0]] = $package[1];
149  }
150 
151  return $packagesAndVersions;
152  }
153 }
$message
__construct(MagentoComposerApplication $magentoComposerApplication, InfoCommand $infoCommand)