Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Install.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Framework\Config\ConfigOptionsListConstants as SetupConfigOptionsList;
17 use Zend\Json\Json;
18 use Zend\Mvc\Controller\AbstractActionController;
19 use Zend\View\Model\JsonModel;
20 use Zend\View\Model\ViewModel;
21 
27 class Install extends AbstractActionController
28 {
32  private $log;
33 
37  private $installer;
38 
42  private $progressFactory;
43 
47  protected $sampleDataState;
48 
52  private $deploymentConfig;
53 
57  private $requestDataConverter;
58 
69  public function __construct(
71  InstallerFactory $installerFactory,
72  ProgressFactory $progressFactory,
73  \Magento\Framework\Setup\SampleData\State $sampleDataState,
74  DeploymentConfig $deploymentConfig,
75  RequestDataConverter $requestDataConverter
76  ) {
77  $this->log = $logger;
78  $this->installer = $installerFactory->create($logger);
79  $this->progressFactory = $progressFactory;
80  $this->sampleDataState = $sampleDataState;
81  $this->deploymentConfig = $deploymentConfig;
82  $this->requestDataConverter = $requestDataConverter;
83  }
84 
88  public function indexAction()
89  {
90  $view = new ViewModel;
91  $view->setTerminal(true);
92  return $view;
93  }
94 
100  public function startAction()
101  {
102  $this->log->clear();
103  $json = new JsonModel;
104  try {
105  $this->checkForPriorInstall();
106  $content = $this->getRequest()->getContent();
107  $source = $content ? $source = Json::decode($content, Json::TYPE_ARRAY) : [];
108  $data = $this->requestDataConverter->convert($source);
109  $this->installer->install($data);
110  $json->setVariable(
111  'key',
112  $this->installer->getInstallInfo()[SetupConfigOptionsList::KEY_ENCRYPTION_KEY]
113  );
114  $json->setVariable('success', true);
115  if ($this->sampleDataState->hasError()) {
116  $json->setVariable('isSampleDataError', true);
117  }
118  $json->setVariable('messages', $this->installer->getInstallInfo()[Installer::INFO_MESSAGE]);
119  } catch (\Exception $e) {
120  $this->log->logError($e);
121  $json->setVariable('messages', $e->getMessage());
122  $json->setVariable('success', false);
123  }
124  return $json;
125  }
126 
132  public function progressAction()
133  {
134  $percent = 0;
135  $success = false;
136  $contents = [];
137  $json = new JsonModel();
138 
139  // Depending upon the install environment and network latency, there is a possibility that
140  // "progress" check request may arrive before the Install POST request. In that case
141  // "install.log" file may not be created yet. Check the "install.log" is created before
142  // trying to read from it.
143  if (!$this->log->logfileExists()) {
144  return $json->setVariables(['progress' => $percent, 'success' => true, 'console' => $contents]);
145  }
146 
147  try {
148  $progress = $this->progressFactory->createFromLog($this->log);
149  $percent = sprintf('%d', $progress->getRatio() * 100);
150  $success = true;
151  $contents = $this->log->get();
152  if ($this->sampleDataState->hasError()) {
153  $json->setVariable('isSampleDataError', true);
154  }
155  } catch (\Exception $e) {
156  $contents = [(string)$e];
157  }
158  return $json->setVariables(['progress' => $percent, 'success' => $success, 'console' => $contents]);
159  }
160 
167  private function checkForPriorInstall()
168  {
169  if ($this->deploymentConfig->isAvailable()) {
170  throw new \Magento\Setup\Exception('Magento application is already installed.');
171  }
172  }
173 }
$contents
Definition: website.php:14
if($this->helper('Magento\Tax\Helper\Data') ->displayFullSummary()) foreach( $block->getTotal() ->getFullInfo() as $info)(isset($info['hidden']) && $info['hidden']) $percent
Definition: tax.phtml:33
$source
Definition: source.php:23
$logger
$deploymentConfig
__construct(WebLogger $logger, InstallerFactory $installerFactory, ProgressFactory $progressFactory, \Magento\Framework\Setup\SampleData\State $sampleDataState, DeploymentConfig $deploymentConfig, RequestDataConverter $requestDataConverter)
Definition: Install.php:69