/vendor/magento/magento2-base/setup/src/Magento/Setup/Controller/OtherComponentsGrid.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 100 lines · 69 code · 8 blank · 23 comment · 4 complexity · f9c913a0fcbd44c094a30d5910b7e536 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\Composer\InfoCommand;
  8. use Magento\Framework\Composer\ComposerInformation;
  9. use Magento\Framework\Composer\MagentoComposerApplicationFactory;
  10. use Zend\Mvc\Controller\AbstractActionController;
  11. use Zend\View\Model\JsonModel;
  12. /**
  13. * Controller for other components grid on select version page
  14. */
  15. class OtherComponentsGrid extends AbstractActionController
  16. {
  17. /**
  18. * @var ComposerInformation
  19. */
  20. private $composerInformation;
  21. /**
  22. * @var InfoCommand
  23. */
  24. private $infoCommand;
  25. /**
  26. * @param ComposerInformation $composerInformation
  27. * @param MagentoComposerApplicationFactory $magentoComposerApplicationFactory
  28. */
  29. public function __construct(
  30. ComposerInformation $composerInformation,
  31. MagentoComposerApplicationFactory $magentoComposerApplicationFactory
  32. ) {
  33. $this->composerInformation = $composerInformation;
  34. $this->infoCommand = $magentoComposerApplicationFactory->createInfoCommand();
  35. }
  36. /**
  37. * Get Components from composer info command
  38. *
  39. * @return JsonModel
  40. * @throws \RuntimeException
  41. */
  42. public function componentsAction()
  43. {
  44. try {
  45. $components = $this->composerInformation->getInstalledMagentoPackages();
  46. foreach ($components as $component) {
  47. if (!$this->composerInformation->isPackageInComposerJson($component['name'])) {
  48. unset($components[$component['name']]);
  49. continue;
  50. }
  51. $componentNameParts = explode('/', $component['name']);
  52. $packageInfo = $this->infoCommand->run($component['name']);
  53. if (!$packageInfo) {
  54. throw new \RuntimeException('Package info not found for ' . $component['name']);
  55. }
  56. if ($packageInfo[InfoCommand::NEW_VERSIONS]) {
  57. $currentVersion = $packageInfo[InfoCommand::CURRENT_VERSION];
  58. $components[$component['name']]['version'] = $currentVersion;
  59. $versions = [];
  60. foreach ($packageInfo[InfoCommand::NEW_VERSIONS] as $version) {
  61. $versions[] = ['id' => $version, 'name' => $version];
  62. }
  63. $versions[] = [
  64. 'id' => $packageInfo[InfoCommand::CURRENT_VERSION],
  65. 'name' => $packageInfo[InfoCommand::CURRENT_VERSION]
  66. ];
  67. $versions[0]['name'] .= ' (latest)';
  68. $versions[count($versions) - 1]['name'] .= ' (current)';
  69. $components[$component['name']]['vendor'] = $componentNameParts[0];
  70. $components[$component['name']]['updates'] = $versions;
  71. $components[$component['name']]['dropdownId'] = 'dd_' . $component['name'];
  72. $components[$component['name']]['checkboxId'] = 'cb_' . $component['name'];
  73. } else {
  74. unset($components[$component['name']]);
  75. }
  76. }
  77. return new JsonModel(
  78. [
  79. 'components' => array_values($components),
  80. 'total' => count($components),
  81. 'responseType' => ResponseTypeInterface::RESPONSE_TYPE_SUCCESS
  82. ]
  83. );
  84. } catch (\Exception $e) {
  85. return new JsonModel(
  86. [
  87. 'responseType' => ResponseTypeInterface::RESPONSE_TYPE_ERROR
  88. ]
  89. );
  90. }
  91. }
  92. }