Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CliCommand.php
Go to the documentation of this file.
1 <?php
7 
12 
17 {
21  private $shell;
22 
26  private $testEnv;
27 
31  private $parametersHolder;
32 
36  private $deploymentConfig;
37 
45  public function __construct(
46  \Magento\TestFramework\Deploy\TestModuleManager $testEnv
47  ) {
48  $this->shell = new Shell(new CommandRenderer());
49  $this->testEnv = $testEnv;
50  $this->parametersHolder = new ParametersHolder();
51  }
52 
59  public function introduceModule($moduleName)
60  {
61  $this->testEnv->addModuleFiles($moduleName);
62  return $this->enableModule($moduleName);
63  }
64 
71  public function enableModule($moduleName)
72  {
73  $initParams = $this->parametersHolder->getInitParams();
74  $enableModuleCommand = 'php -f ' . BP . '/bin/magento module:enable ' . $moduleName
75  . ' -n -vvv --magento-init-params="' . $initParams['magento-init-params'] . '"';
76  return $this->shell->execute($enableModuleCommand);
77  }
78 
85  public function upgrade($installParams = [])
86  {
87  $initParams = $this->parametersHolder->getInitParams();
88  $upgradeCommand = 'php -f ' . BP . '/bin/magento setup:upgrade -vvv -n --magento-init-params="'
89  . $initParams['magento-init-params'] . '"';
90  $installParams = $this->toCliArguments($installParams);
91  $upgradeCommand .= ' ' . implode(" ", array_keys($installParams));
92 
93  return $this->shell->execute($upgradeCommand, array_values($installParams));
94  }
95 
102  public function disableModule($moduleName)
103  {
104  $initParams = $this->parametersHolder->getInitParams();
105  $disableModuleCommand = 'php -f ' . BP . '/bin/magento module:disable '. $moduleName
106  . ' -vvv --magento-init-params="' . $initParams['magento-init-params'] . '"';
107  return $this->shell->execute($disableModuleCommand);
108  }
109 
115  public function splitQuote()
116  {
117  $initParams = $this->parametersHolder->getInitParams();
118  $installParams = $this->toCliArguments(
119  $this->parametersHolder->getDbData('checkout')
120  );
121  $command = 'php -f ' . BP . '/bin/magento setup:db-schema:split-quote ' .
122  implode(" ", array_keys($installParams)) .
123  ' -vvv --magento-init-params="' .
124  $initParams['magento-init-params'] . '"';
125 
126  $this->shell->execute($command, array_values($installParams));
127  }
128 
134  public function splitSales()
135  {
136  $initParams = $this->parametersHolder->getInitParams();
137  $installParams = $this->toCliArguments(
138  $this->parametersHolder->getDbData('sales')
139  );
140  $command = 'php -f ' . BP . '/bin/magento setup:db-schema:split-sales ' .
141  implode(" ", array_keys($installParams)) .
142  ' -vvv --magento-init-params="' .
143  $initParams['magento-init-params'] . '"';
144 
145  $this->shell->execute($command, array_values($installParams));
146  }
147 
151  public function cacheClean()
152  {
153  $initParams = $this->parametersHolder->getInitParams();
154  $command = 'php -f ' . BP . '/bin/magento cache:clean ' .
155  ' -vvv --magento-init-params=' .
156  $initParams['magento-init-params'];
157 
158  $this->shell->execute($command);
159  }
160 
166  public function uninstallModule($moduleName)
167  {
168  $initParams = $this->parametersHolder->getInitParams();
169  $command = 'php -f ' . BP . '/bin/magento module:uninstall ' . $moduleName . ' --remove-data ' .
170  ' -vvv --non-composer --magento-init-params="' .
171  $initParams['magento-init-params'] . '"';
172 
173  $this->shell->execute($command);
174  }
175 
182  private function toCliArguments(array $params)
183  {
184  $result = [];
185 
186  foreach ($params as $key => $value) {
187  if (!empty($value)) {
188  $result["--{$key}=%s"] = $value;
189  }
190  }
191 
192  return $result;
193  }
194 
203  public function install(array $modules, array $installParams = [])
204  {
205  if (empty($modules)) {
206  throw new \Exception("Cannot install Magento without modules");
207  }
208 
209  $params = $this->parametersHolder->getInitParams();
210  $installParams += [
211  InstallCommand::INPUT_KEY_ENABLE_MODULES => implode(",", $modules),
213  ];
214  $installParams = $this->toCliArguments(
215  array_merge(
216  $params,
217  $this->parametersHolder->getDbData('default'),
218  $installParams
219  )
220  );
221  // run install script
222  $exitCode = $this->shell->execute(
223  PHP_BINARY . ' -f %s setup:install -vvv ' . implode(' ', array_keys($installParams)),
224  array_merge([BP . '/bin/magento'], array_values($installParams))
225  );
226  $this->afterInstall();
227  return $exitCode;
228  }
229 
235  public function afterInstall()
236  {
237  //Take current deployment config in order to flush it cache after installation
238  //Before installation usually we do not have any connections - so we need to add them
240  ->get(DeploymentConfig::class);
241  $this->deploymentConfig->resetData();
242  }
243 }
install(array $modules, array $installParams=[])
Definition: CliCommand.php:203
$value
Definition: gender.phtml:16
__construct(\Magento\TestFramework\Deploy\TestModuleManager $testEnv)
Definition: CliCommand.php:45
const BP
Definition: autoload.php:14
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18