/setup/src/Magento/Setup/Controller/ComponentGrid.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 263 lines · 150 code · 24 blank · 89 comment · 9 complexity · 3801c3a3e5100da30658a4af7bfbe0f1 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\Controller;
  7. use Magento\Setup\Model\DateTime\TimezoneProvider;
  8. /**
  9. * Controller for component grid tasks
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class ComponentGrid extends \Zend\Mvc\Controller\AbstractActionController
  13. {
  14. /**
  15. * @var \Magento\Framework\Composer\ComposerInformation
  16. */
  17. private $composerInformation;
  18. /**
  19. * Module package info
  20. *
  21. * @var \Magento\Framework\Module\PackageInfo
  22. */
  23. private $packageInfo;
  24. /**
  25. * @var \Magento\Setup\Model\MarketplaceManager
  26. */
  27. private $marketplaceManager;
  28. /**
  29. * @var \Magento\Framework\Module\ModuleList
  30. */
  31. private $enabledModuleList;
  32. /**
  33. * Full Module info
  34. *
  35. * @var \Magento\Framework\Module\FullModuleList
  36. */
  37. private $fullModuleList;
  38. /**
  39. * @var \Magento\Setup\Model\UpdatePackagesCache
  40. */
  41. private $updatePackagesCache;
  42. /**
  43. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  44. */
  45. private $timezone;
  46. /**
  47. * @var \Magento\Framework\ObjectManagerInterface
  48. */
  49. private $objectManager;
  50. /**
  51. * @param \Magento\Framework\Composer\ComposerInformation $composerInformation
  52. * @param \Magento\Setup\Model\ObjectManagerProvider $objectManagerProvider
  53. * @param \Magento\Setup\Model\UpdatePackagesCache $updatePackagesCache
  54. * @param \Magento\Setup\Model\MarketplaceManager $marketplaceManager
  55. */
  56. public function __construct(
  57. \Magento\Framework\Composer\ComposerInformation $composerInformation,
  58. \Magento\Setup\Model\ObjectManagerProvider $objectManagerProvider,
  59. \Magento\Setup\Model\UpdatePackagesCache $updatePackagesCache,
  60. \Magento\Setup\Model\MarketplaceManager $marketplaceManager
  61. ) {
  62. $this->composerInformation = $composerInformation;
  63. $this->objectManager = $objectManagerProvider->get();
  64. $this->enabledModuleList = $this->objectManager->get('Magento\Framework\Module\ModuleList');
  65. $this->fullModuleList = $this->objectManager->get('Magento\Framework\Module\FullModuleList');
  66. $this->packageInfo = $this->objectManager->get('Magento\Framework\Module\PackageInfoFactory')->create();
  67. $this->marketplaceManager = $marketplaceManager;
  68. $this->updatePackagesCache = $updatePackagesCache;
  69. }
  70. /**
  71. * Get timezone
  72. *
  73. * @return \Magento\Framework\Stdlib\DateTime\TimezoneInterface|null
  74. */
  75. private function getTimezone()
  76. {
  77. if ($this->timezone === null) {
  78. $this->timezone = $this->objectManager->get('Magento\Setup\Model\DateTime\TimezoneProvider')->get();
  79. }
  80. return $this->timezone;
  81. }
  82. /**
  83. * Set timezone
  84. *
  85. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
  86. * @return void
  87. * @throws \Exception
  88. */
  89. public function setTimezone(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone)
  90. {
  91. if ($this->timezone !== null) {
  92. throw new \Exception('timezone is already set');
  93. }
  94. $this->timezone = $timezone;
  95. }
  96. /**
  97. * Index page action
  98. *
  99. * @return \Zend\View\Model\ViewModel
  100. */
  101. public function indexAction()
  102. {
  103. $view = new \Zend\View\Model\ViewModel();
  104. $view->setTerminal(true);
  105. return $view;
  106. }
  107. /**
  108. * Get Components info action
  109. *
  110. * @return \Zend\View\Model\JsonModel
  111. * @throws \RuntimeException
  112. * @SuppressWarnings(PHPMD.NPathComplexity)
  113. */
  114. public function componentsAction()
  115. {
  116. $lastSyncData = $this->updatePackagesCache->getPackagesForUpdate();
  117. $components = $this->composerInformation->getInstalledMagentoPackages();
  118. $allModules = $this->getAllModules();
  119. $components = array_replace_recursive($components, $allModules);
  120. foreach ($components as $component) {
  121. $components[$component['name']]['update'] = false;
  122. $components[$component['name']]['uninstall'] = false;
  123. $components[$component['name']]['moduleName'] = $this->packageInfo->getModuleName($component['name']);
  124. if ($this->composerInformation->isPackageInComposerJson($component['name'])) {
  125. if ($component['type'] !== \Magento\Framework\Composer\ComposerInformation::METAPACKAGE_PACKAGE_TYPE) {
  126. $components[$component['name']]['uninstall'] = true;
  127. }
  128. if (isset($lastSyncData['packages'][$component['name']]['latestVersion'])
  129. && version_compare(
  130. $lastSyncData['packages'][$component['name']]['latestVersion'],
  131. $component['version'],
  132. '>'
  133. )) {
  134. $components[$component['name']]['update'] = true;
  135. }
  136. }
  137. if ($component['type'] === \Magento\Framework\Composer\ComposerInformation::MODULE_PACKAGE_TYPE) {
  138. $components[$component['name']]['enable'] =
  139. $this->enabledModuleList->has($components[$component['name']]['moduleName']);
  140. $components[$component['name']]['disable'] = !$components[$component['name']]['enable'];
  141. } else {
  142. $components[$component['name']]['enable'] = false;
  143. $components[$component['name']]['disable'] = false;
  144. }
  145. $componentNameParts = explode('/', $component['name']);
  146. $components[$component['name']]['vendor'] = $componentNameParts[0];
  147. }
  148. $packagesForInstall = $this->marketplaceManager->getPackagesForInstall();
  149. $lastSyncData = $this->formatLastSyncData($packagesForInstall, $lastSyncData);
  150. return new \Zend\View\Model\JsonModel(
  151. [
  152. 'success' => true,
  153. 'components' => array_values($components),
  154. 'total' => count($components),
  155. 'lastSyncData' => $lastSyncData
  156. ]
  157. );
  158. }
  159. /**
  160. * Sync action
  161. *
  162. * @return \Zend\View\Model\JsonModel
  163. */
  164. public function syncAction()
  165. {
  166. $error = '';
  167. try {
  168. $this->updatePackagesCache->syncPackagesForUpdate();
  169. $lastSyncData = $this->updatePackagesCache->getPackagesForUpdate();
  170. $this->marketplaceManager->syncPackagesForInstall();
  171. $packagesForInstall = $this->marketplaceManager->getPackagesForInstall();
  172. } catch (\Exception $e) {
  173. $error = $e->getMessage();
  174. }
  175. $lastSyncData = $this->formatLastSyncData($packagesForInstall, $lastSyncData);
  176. return new \Zend\View\Model\JsonModel(
  177. [
  178. 'success' => true,
  179. 'lastSyncData' => $lastSyncData,
  180. 'error' => $error
  181. ]
  182. );
  183. }
  184. /**
  185. * Get full list of modules as an associative array
  186. *
  187. * @return array
  188. */
  189. private function getAllModules()
  190. {
  191. $modules = [];
  192. $allModules = $this->fullModuleList->getNames();
  193. foreach ($allModules as $module) {
  194. $moduleName = $this->packageInfo->getPackageName($module);
  195. $modules[$moduleName]['name'] = $moduleName;
  196. $modules[$moduleName]['type'] = \Magento\Framework\Composer\ComposerInformation::MODULE_PACKAGE_TYPE;
  197. $modules[$moduleName]['version'] = $this->packageInfo->getVersion($module);
  198. }
  199. return $modules;
  200. }
  201. /**
  202. * Format the lastSyncData for use on frontend
  203. *
  204. * @param array $packagesForInstall
  205. * @param array $lastSyncData
  206. * @return mixed
  207. */
  208. private function formatLastSyncData($packagesForInstall, $lastSyncData)
  209. {
  210. $lastSyncData['countOfInstall']
  211. = isset($packagesForInstall['packages']) ? count($packagesForInstall['packages']) : 0;
  212. $lastSyncData['countOfUpdate'] = isset($lastSyncData['packages']) ? count($lastSyncData['packages']) : 0;
  213. if (isset($lastSyncData['lastSyncDate'])) {
  214. $lastSyncData['lastSyncDate'] = $this->formatSyncDate($lastSyncData['lastSyncDate']);
  215. }
  216. return $lastSyncData;
  217. }
  218. /**
  219. * Format a UTC timestamp (seconds since epoch) to structure expected by frontend
  220. *
  221. * @param string $syncDate seconds since epoch
  222. * @return array
  223. */
  224. private function formatSyncDate($syncDate)
  225. {
  226. return [
  227. 'date' => $this->getTimezone()->formatDateTime(
  228. new \DateTime('@'.$syncDate),
  229. \IntlDateFormatter::MEDIUM,
  230. \IntlDateFormatter::NONE
  231. ),
  232. 'time' => $this->getTimezone()->formatDateTime(
  233. new \DateTime('@'.$syncDate),
  234. \IntlDateFormatter::NONE,
  235. \IntlDateFormatter::MEDIUM
  236. ),
  237. ];
  238. }
  239. }