Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cli.php
Go to the documentation of this file.
1 <?php
7 
20 use Symfony\Component\Console;
22 
30 class Cli extends Console\Application
31 {
35  const INPUT_KEY_BOOTSTRAP = 'bootstrap';
36 
40  const RETURN_SUCCESS = 0;
41  const RETURN_FAILURE = 1;
45  private $serviceManager;
46 
52  private $initException;
53 
59  private $objectManager;
60 
66  public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
67  {
68  try {
69  $configuration = require BP . '/setup/config/application.config.php';
70  $bootstrapApplication = new Application();
71  $application = $bootstrapApplication->bootstrap($configuration);
72  $this->serviceManager = $application->getServiceManager();
73 
74  $this->assertCompilerPreparation();
75  $this->initObjectManager();
76  } catch (\Exception $exception) {
77  $output = new \Symfony\Component\Console\Output\ConsoleOutput();
78  $output->writeln(
79  '<error>' . $exception->getMessage() . '</error>'
80  );
81 
82  exit(static::RETURN_FAILURE);
83  }
84 
85  if ($version == 'UNKNOWN') {
86  $directoryList = new DirectoryList(BP);
87  $composerJsonFinder = new ComposerJsonFinder($directoryList);
88  $productMetadata = new ProductMetadata($composerJsonFinder);
89  $version = $productMetadata->getVersion();
90  }
91 
92  parent::__construct($name, $version);
93  }
94 
100  public function doRun(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
101  {
102  $exitCode = parent::doRun($input, $output);
103 
104  if ($this->initException) {
105  throw $this->initException;
106  }
107 
108  return $exitCode;
109  }
110 
114  protected function getDefaultCommands()
115  {
116  return array_merge(parent::getDefaultCommands(), $this->getApplicationCommands());
117  }
118 
124  protected function getApplicationCommands()
125  {
126  $commands = [];
127  try {
128  if (class_exists(\Magento\Setup\Console\CommandList::class)) {
129  $setupCommandList = new \Magento\Setup\Console\CommandList($this->serviceManager);
130  $commands = array_merge($commands, $setupCommandList->getCommands());
131  }
132 
133  if ($this->objectManager->get(DeploymentConfig::class)->isAvailable()) {
135  $commandList = $this->objectManager->create(CommandListInterface::class);
136  $commands = array_merge($commands, $commandList->getCommands());
137  }
138 
139  $commands = array_merge(
140  $commands,
141  $this->getVendorCommands($this->objectManager)
142  );
143  } catch (\Exception $e) {
144  $this->initException = $e;
145  }
146 
147  return $commands;
148  }
149 
155  private function initObjectManager()
156  {
157  $params = (new ComplexParameter(self::INPUT_KEY_BOOTSTRAP))->mergeFromArgv($_SERVER, $_SERVER);
159  $params = $this->documentRootResolver($params);
160  $requestParams = $this->serviceManager->get('magento-init-params');
162 
163  if (isset($requestParams[$appBootstrapKey]) && !isset($params[$appBootstrapKey])) {
164  $params[$appBootstrapKey] = $requestParams[$appBootstrapKey];
165  }
166 
167  $this->objectManager = Bootstrap::create(BP, $params)->getObjectManager();
168 
170  $omProvider = $this->serviceManager->get(ObjectManagerProvider::class);
171  $omProvider->setObjectManager($this->objectManager);
172  }
173 
180  private function assertCompilerPreparation()
181  {
186  if (class_exists(CompilerPreparation::class)) {
187  $compilerPreparation = new CompilerPreparation(
188  $this->serviceManager,
189  new Console\Input\ArgvInput(),
190  new File()
191  );
192 
193  $compilerPreparation->handleCompilerEnvironment();
194  }
195  }
196 
204  protected function getVendorCommands($objectManager)
205  {
206  $commands = [];
207  foreach (CommandLocator::getCommands() as $commandListClass) {
208  if (class_exists($commandListClass)) {
209  $commands = array_merge(
210  $commands,
211  $objectManager->create($commandListClass)->getCommands()
212  );
213  }
214  }
215 
216  return $commands;
217  }
218 
226  private function documentRootResolver(array $config = []): array
227  {
228  $params = [];
229  $deploymentConfig = $this->serviceManager->get(DeploymentConfig::class);
235  DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'media/upload'],
236  ];
237  }
238 
239  return array_merge_recursive($config, $params);
240  }
241 }
static create($rootDir, array $initParams, ObjectManagerFactory $factory=null)
Definition: Bootstrap.php:119
__construct($name='UNKNOWN', $version='UNKNOWN')
Definition: Cli.php:66
$configuration
Definition: index.php:33
$config
Definition: fraud_order.php:17
$deploymentConfig
$application
Definition: bootstrap.php:58
exit
Definition: redirect.phtml:12
getVendorCommands($objectManager)
Definition: Cli.php:204
doRun(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
Definition: Cli.php:100
const BP
Definition: autoload.php:14
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
if(!isset($_GET['name'])) $name
Definition: log.php:14