PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/axeltizon/magento-demopoweraccess
PHP | 173 lines | 98 code | 15 blank | 60 comment | 4 complexity | c1e409ecdb77446aef37347325a25346 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\Framework\App\DeploymentConfig;
  8. use Magento\Framework\Config\ConfigOptionsListConstants as SetupConfigOptionsList;
  9. use Magento\SampleData;
  10. use Magento\Setup\Model\Installer;
  11. use Magento\Setup\Model\Installer\ProgressFactory;
  12. use Magento\Setup\Model\InstallerFactory;
  13. use Magento\Setup\Model\RequestDataConverter;
  14. use Magento\Setup\Model\WebLogger;
  15. use Zend\Json\Json;
  16. use Zend\Mvc\Controller\AbstractActionController;
  17. use Zend\View\Model\JsonModel;
  18. use Zend\View\Model\ViewModel;
  19. /**
  20. * Install controller
  21. *
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class Install extends AbstractActionController
  25. {
  26. /**
  27. * @var WebLogger
  28. */
  29. private $log;
  30. /**
  31. * @var Installer
  32. */
  33. private $installer;
  34. /**
  35. * @var ProgressFactory
  36. */
  37. private $progressFactory;
  38. /**
  39. * @var \Magento\Framework\Setup\SampleData\State
  40. */
  41. protected $sampleDataState;
  42. /**
  43. * @var \Magento\Framework\App\DeploymentConfig
  44. */
  45. private $deploymentConfig;
  46. /**
  47. * @var RequestDataConverter
  48. */
  49. private $requestDataConverter;
  50. /**
  51. * Default Constructor
  52. *
  53. * @param WebLogger $logger
  54. * @param InstallerFactory $installerFactory
  55. * @param ProgressFactory $progressFactory
  56. * @param \Magento\Framework\Setup\SampleData\State $sampleDataState
  57. * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
  58. * @param RequestDataConverter $requestDataConverter
  59. */
  60. public function __construct(
  61. WebLogger $logger,
  62. InstallerFactory $installerFactory,
  63. ProgressFactory $progressFactory,
  64. \Magento\Framework\Setup\SampleData\State $sampleDataState,
  65. DeploymentConfig $deploymentConfig,
  66. RequestDataConverter $requestDataConverter
  67. ) {
  68. $this->log = $logger;
  69. $this->installer = $installerFactory->create($logger);
  70. $this->progressFactory = $progressFactory;
  71. $this->sampleDataState = $sampleDataState;
  72. $this->deploymentConfig = $deploymentConfig;
  73. $this->requestDataConverter = $requestDataConverter;
  74. }
  75. /**
  76. * @return ViewModel
  77. */
  78. public function indexAction()
  79. {
  80. $view = new ViewModel;
  81. $view->setTerminal(true);
  82. return $view;
  83. }
  84. /**
  85. * Index Action
  86. *
  87. * @return JsonModel
  88. */
  89. public function startAction()
  90. {
  91. $this->log->clear();
  92. $json = new JsonModel;
  93. try {
  94. $this->checkForPriorInstall();
  95. $content = $this->getRequest()->getContent();
  96. $source = $content ? $source = Json::decode($content, Json::TYPE_ARRAY) : [];
  97. $data = $this->requestDataConverter->convert($source);
  98. $this->installer->install($data);
  99. $json->setVariable(
  100. 'key',
  101. $this->installer->getInstallInfo()[SetupConfigOptionsList::KEY_ENCRYPTION_KEY]
  102. );
  103. $json->setVariable('success', true);
  104. if ($this->sampleDataState->hasError()) {
  105. $json->setVariable('isSampleDataError', true);
  106. }
  107. $json->setVariable('messages', $this->installer->getInstallInfo()[Installer::INFO_MESSAGE]);
  108. } catch (\Exception $e) {
  109. $this->log->logError($e);
  110. $json->setVariable('messages', $e->getMessage());
  111. $json->setVariable('success', false);
  112. }
  113. return $json;
  114. }
  115. /**
  116. * Checks progress of installation
  117. *
  118. * @return JsonModel
  119. */
  120. public function progressAction()
  121. {
  122. $percent = 0;
  123. $success = false;
  124. $contents = [];
  125. $json = new JsonModel();
  126. // Depending upon the install environment and network latency, there is a possibility that
  127. // "progress" check request may arrive before the Install POST request. In that case
  128. // "install.log" file may not be created yet. Check the "install.log" is created before
  129. // trying to read from it.
  130. if (!$this->log->logfileExists()) {
  131. return $json->setVariables(['progress' => $percent, 'success' => true, 'console' => $contents]);
  132. }
  133. try {
  134. $progress = $this->progressFactory->createFromLog($this->log);
  135. $percent = sprintf('%d', $progress->getRatio() * 100);
  136. $success = true;
  137. $contents = $this->log->get();
  138. if ($this->sampleDataState->hasError()) {
  139. $json->setVariable('isSampleDataError', true);
  140. }
  141. } catch (\Exception $e) {
  142. $contents = [(string)$e];
  143. }
  144. return $json->setVariables(['progress' => $percent, 'success' => $success, 'console' => $contents]);
  145. }
  146. /**
  147. * Checks for prior install
  148. *
  149. * @return void
  150. * @throws \Magento\Setup\Exception
  151. */
  152. private function checkForPriorInstall()
  153. {
  154. if ($this->deploymentConfig->isAvailable()) {
  155. throw new \Magento\Setup\Exception('Magento application is already installed.');
  156. }
  157. }
  158. }