Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InfoCommand.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Composer;
8 
13 {
17  const CURRENT_VERSION = 'current_version';
18 
19  const VERSIONS = 'versions';
20 
24  const AVAILABLE_VERSIONS = 'available_versions';
25 
29  const NAME = 'name';
30 
34  const NEW_VERSIONS = 'new_versions';
35 
36 
41 
48  {
49  $this->magentoComposerApplication = $magentoComposerApplication;
50  }
51 
59  public function run($package, $installed = false)
60  {
61  $showAllPackages = !$installed;
62  $commandParameters = [
63  'command' => 'info',
64  'package' => $package,
65  '-i' => $installed,
66  '--all' => $showAllPackages,
67  ];
68 
69  try {
70  $output = $this->magentoComposerApplication->runComposerCommand($commandParameters);
71  } catch (\RuntimeException $e) {
72  return false;
73  }
74 
75  $rawLines = explode("\n", str_replace("\r\n", "\n", $output));
76  $result = [];
77 
78  foreach ($rawLines as $line) {
79  $chunk = explode(':', $line);
80  if (count($chunk) === 2) {
81  $result[trim($chunk[0])] = trim($chunk[1]);
82  }
83  }
84 
85  $result = $this->extractVersions($result);
86 
87  if (!isset($result[self::NAME]) && isset($result[self::CURRENT_VERSION])) {
88  $result[self::NAME] = $package;
89  }
90 
91  return $result;
92  }
93 
100  private function extractVersions($packageInfo)
101  {
102  $versions = explode(', ', $packageInfo[self::VERSIONS]);
103  $packageInfo[self::NEW_VERSIONS] = [];
104  $packageInfo[self::AVAILABLE_VERSIONS] = [];
105 
106  if (count($versions) === 1) {
107  $packageInfo[self::CURRENT_VERSION] = str_replace('* ', '', $packageInfo[self::VERSIONS]);
108  } else {
109  $currentVersion = array_values(preg_grep("/^\*.*/", $versions));
110  if ($currentVersion) {
111  $packageInfo[self::CURRENT_VERSION] = str_replace('* ', '', $currentVersion[0]);
112  } else {
113  $packageInfo[self::CURRENT_VERSION] = '';
114  }
115 
116  $packageInfo[self::AVAILABLE_VERSIONS] = array_values(preg_grep("/^\*.*/", $versions, PREG_GREP_INVERT));
117  }
118 
119  if (count($packageInfo[self::AVAILABLE_VERSIONS]) > 0) {
120  if ($packageInfo[self::CURRENT_VERSION]) {
121  foreach ($packageInfo[self::AVAILABLE_VERSIONS] as $version) {
122  if (version_compare($packageInfo[self::CURRENT_VERSION], $version, '<')) {
123  $packageInfo[self::NEW_VERSIONS][] = $version;
124  }
125  }
126  } else {
127  $packageInfo[self::NEW_VERSIONS] = $packageInfo[self::AVAILABLE_VERSIONS];
128  }
129  }
130 
131  return $packageInfo;
132  }
133 }
run($package, $installed=false)
Definition: InfoCommand.php:59
__construct(MagentoComposerApplication $magentoComposerApplication)
Definition: InfoCommand.php:47