PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/setup/src/Magento/Setup/Model/SystemPackage.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 263 lines | 175 code | 28 blank | 60 comment | 29 complexity | 205003448114d64d6f5b1915314bfc6c MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Setup\Model;
  7. use Magento\Composer\InfoCommand;
  8. use Magento\Composer\MagentoComposerApplication;
  9. use Magento\Framework\Composer\ComposerInformation;
  10. use Magento\Framework\Composer\MagentoComposerApplicationFactory;
  11. /**
  12. * Class SystemPackage returns system package and available for update versions
  13. */
  14. class SystemPackage
  15. {
  16. /**
  17. * @var InfoCommand
  18. */
  19. private $infoCommand;
  20. /**
  21. * @var MagentoComposerApplication
  22. */
  23. private $magentoComposerApplication;
  24. /**
  25. * @var ComposerInformation
  26. */
  27. private $composerInfo;
  28. /**
  29. * Constructor
  30. *
  31. * @param MagentoComposerApplicationFactory $composerAppFactory
  32. * @param ComposerInformation $composerInfo
  33. */
  34. public function __construct(
  35. MagentoComposerApplicationFactory $composerAppFactory,
  36. ComposerInformation $composerInfo
  37. ) {
  38. $this->infoCommand = $composerAppFactory->createInfoCommand();
  39. $this->magentoComposerApplication = $composerAppFactory->create();
  40. $this->composerInfo = $composerInfo;
  41. }
  42. /**
  43. * Returns system package and available versions
  44. *
  45. * @throws \RuntimeException
  46. * @return array
  47. */
  48. public function getPackageVersions()
  49. {
  50. $currentCE = '0';
  51. $result = [];
  52. $systemPackages = [];
  53. $systemPackages = $this->getInstalledSystemPackages($systemPackages);
  54. foreach ($systemPackages as $systemPackage) {
  55. $versions = [];
  56. $systemPackageInfo = $this->infoCommand->run($systemPackage);
  57. if (!$systemPackageInfo) {
  58. throw new \RuntimeException("We cannot retrieve information on $systemPackage.");
  59. }
  60. $versions = $this->getSystemPackageVersions($systemPackageInfo, $versions);
  61. if ($systemPackageInfo['name'] == 'magento/product-community-edition') {
  62. $currentCE = $systemPackageInfo[InfoCommand::CURRENT_VERSION];
  63. }
  64. if (count($versions) > 1) {
  65. $versions[0]['name'] .= ' (latest)';
  66. }
  67. $result[] = [
  68. 'package' => $systemPackageInfo['name'],
  69. 'versions' => $versions,
  70. ];
  71. }
  72. if (!in_array('magento/product-enterprise-edition', $systemPackages)) {
  73. $result = array_merge($this->getAllowedEnterpriseVersions($currentCE), $result);
  74. }
  75. $result = $this->formatPackages($result);
  76. return $result;
  77. }
  78. /**
  79. * @param string $currentCE
  80. * @return array
  81. */
  82. public function getAllowedEnterpriseVersions($currentCE)
  83. {
  84. $result = [];
  85. $enterpriseVersions = $this->infoCommand->run('magento/product-enterprise-edition');
  86. $eeVersions = [];
  87. $maxVersion = '';
  88. if (is_array($enterpriseVersions) && array_key_exists('available_versions', $enterpriseVersions)) {
  89. $enterpriseVersions = $this->sortVersions($enterpriseVersions);
  90. if (isset($enterpriseVersions['available_versions'][0])) {
  91. $maxVersion = $enterpriseVersions['available_versions'][0];
  92. }
  93. $eeVersions = $this->filterEeVersions($currentCE, $enterpriseVersions, $maxVersion);
  94. }
  95. if (!empty($eeVersions)) {
  96. $result[] = [
  97. 'package' => 'magento/product-enterprise-edition',
  98. 'versions' => $eeVersions,
  99. ];
  100. }
  101. return $result;
  102. }
  103. /**
  104. * @param array $systemPackageInfo
  105. * @param array $versions
  106. * @return array
  107. */
  108. public function getSystemPackageVersions($systemPackageInfo, $versions)
  109. {
  110. $editionType = '';
  111. if ($systemPackageInfo['name'] == 'magento/product-community-edition') {
  112. $editionType .= 'CE';
  113. } elseif ($systemPackageInfo['name'] == 'magento/product-enterprise-edition') {
  114. $editionType .= 'EE';
  115. }
  116. foreach ($systemPackageInfo[InfoCommand::NEW_VERSIONS] as $version) {
  117. $versions[] = ['id' => $version, 'name' => 'Version ' . $version . ' ' . $editionType, 'current' => false];
  118. }
  119. if ($systemPackageInfo[InfoCommand::CURRENT_VERSION]) {
  120. $versions[] = [
  121. 'id' => $systemPackageInfo[InfoCommand::CURRENT_VERSION],
  122. 'name' => 'Version ' . $systemPackageInfo[InfoCommand::CURRENT_VERSION] . ' ' . $editionType,
  123. 'current' => true,
  124. ];
  125. }
  126. return $versions;
  127. }
  128. /**
  129. * @param array $systemPackages
  130. * @return array
  131. * @throws \RuntimeException
  132. */
  133. public function getInstalledSystemPackages($systemPackages)
  134. {
  135. $systemPackages = [];
  136. $locker = $this->magentoComposerApplication->createComposer()->getLocker();
  137. /** @var \Composer\Package\CompletePackage $package */
  138. foreach ($locker->getLockedRepository()->getPackages() as $package) {
  139. $packageName = $package->getName();
  140. if ($this->composerInfo->isSystemPackage($packageName)) {
  141. if ($packageName == 'magento/product-community-edition') {
  142. if ($this->composerInfo->isPackageInComposerJson($packageName)) {
  143. $systemPackages[] = $packageName;
  144. }
  145. } else {
  146. $systemPackages[] = $packageName;
  147. }
  148. }
  149. }
  150. if (empty($systemPackages)) {
  151. throw new \RuntimeException(
  152. 'We\'re sorry, no components are available because you cloned the Magento 2 GitHub repository. ' .
  153. 'You must manually update components as discussed in the ' .
  154. '<a href="http://devdocs.magento.com/guides/v2.0/install-gde/install/cli/dev_options.html">' .
  155. 'Installation Guide</a>.'
  156. );
  157. }
  158. return $systemPackages;
  159. }
  160. /**
  161. * @param array $enterpriseVersions
  162. * @return array
  163. */
  164. public function sortVersions($enterpriseVersions)
  165. {
  166. usort($enterpriseVersions['available_versions'], function ($versionOne, $versionTwo) {
  167. if (version_compare($versionOne, $versionTwo, '==')) {
  168. return 0;
  169. }
  170. return (version_compare($versionOne, $versionTwo, '<')) ? 1 : -1;
  171. });
  172. return $enterpriseVersions;
  173. }
  174. /**
  175. * Re-formats packages array to merge packages, sort versions and add technical data
  176. *
  177. * @param array $packages
  178. * @return array
  179. */
  180. private function formatPackages($packages)
  181. {
  182. $versions = [];
  183. foreach ($packages as $package) {
  184. foreach ($package['versions'] as $version) {
  185. $version['package'] = $package['package'];
  186. if (preg_match('/^[0-9].[0-9].[0-9]$/', $version['id']) || $version['current']) {
  187. $version['stable'] = true;
  188. } else {
  189. $version['name'] = $version['name'] . ' (unstable version)';
  190. $version['stable'] = false;
  191. }
  192. $versions[] = $version;
  193. }
  194. }
  195. usort($versions, function ($versionOne, $versionTwo) {
  196. if (version_compare($versionOne['id'], $versionTwo['id'], '==')) {
  197. if ($versionOne['package'] === 'magento/product-community-edition') {
  198. return 1;
  199. }
  200. return 0;
  201. }
  202. return (version_compare($versionOne['id'], $versionTwo['id'], '<')) ? 1 : -1;
  203. });
  204. return $versions;
  205. }
  206. /**
  207. * @param string $currentCE
  208. * @param array $enterpriseVersions
  209. * @param string $maxVersion
  210. * @return array
  211. */
  212. public function filterEeVersions($currentCE, $enterpriseVersions, $maxVersion)
  213. {
  214. $eeVersions = [];
  215. foreach ($enterpriseVersions['available_versions'] as $version) {
  216. $requires = $this->composerInfo->getPackageRequirements('magento/product-enterprise-edition', $version);
  217. if (array_key_exists('magento/product-community-edition', $requires)) {
  218. /** @var \Composer\Package\Link $ceRequire */
  219. $ceRequire = $requires['magento/product-community-edition'];
  220. if (version_compare(
  221. $ceRequire->getConstraint()->getPrettyString(),
  222. $currentCE,
  223. '>='
  224. )) {
  225. $name = 'Version ' . $version . ' EE';
  226. if ($maxVersion == $version) {
  227. $name .= ' (latest)';
  228. }
  229. $eeVersions[] = ['id' => $version, 'name' => $name, 'current' => false];
  230. }
  231. }
  232. }
  233. return $eeVersions;
  234. }
  235. }