Cli.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Console;
  7. use Magento\Framework\App\Bootstrap;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\App\ProductMetadata;
  11. use Magento\Framework\Composer\ComposerJsonFinder;
  12. use Magento\Framework\Console\Exception\GenerationDirectoryAccessException;
  13. use Magento\Framework\Filesystem\Driver\File;
  14. use Magento\Framework\ObjectManagerInterface;
  15. use Magento\Framework\Shell\ComplexParameter;
  16. use Magento\Setup\Application;
  17. use Magento\Setup\Console\CompilerPreparation;
  18. use Magento\Setup\Model\ObjectManagerProvider;
  19. use Symfony\Component\Console;
  20. use Magento\Framework\Config\ConfigOptionsListConstants;
  21. /**
  22. * Magento 2 CLI Application.
  23. * This is the hood for all command line tools supported by Magento.
  24. *
  25. * {@inheritdoc}
  26. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  27. */
  28. class Cli extends Console\Application
  29. {
  30. /**
  31. * Name of input option.
  32. */
  33. const INPUT_KEY_BOOTSTRAP = 'bootstrap';
  34. /**#@+
  35. * Cli exit codes.
  36. */
  37. const RETURN_SUCCESS = 0;
  38. const RETURN_FAILURE = 1;
  39. /**#@-*/
  40. /**#@-*/
  41. private $serviceManager;
  42. /**
  43. * Initialization exception.
  44. *
  45. * @var \Exception
  46. */
  47. private $initException;
  48. /**
  49. * Object Manager.
  50. *
  51. * @var ObjectManagerInterface
  52. */
  53. private $objectManager;
  54. /**
  55. * @param string $name the application name
  56. * @param string $version the application version
  57. * @SuppressWarnings(PHPMD.ExitExpression)
  58. */
  59. public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
  60. {
  61. try {
  62. $configuration = require BP . '/setup/config/application.config.php';
  63. $bootstrapApplication = new Application();
  64. $application = $bootstrapApplication->bootstrap($configuration);
  65. $this->serviceManager = $application->getServiceManager();
  66. $this->assertCompilerPreparation();
  67. $this->initObjectManager();
  68. } catch (\Exception $exception) {
  69. $output = new \Symfony\Component\Console\Output\ConsoleOutput();
  70. $output->writeln(
  71. '<error>' . $exception->getMessage() . '</error>'
  72. );
  73. exit(static::RETURN_FAILURE);
  74. }
  75. if ($version == 'UNKNOWN') {
  76. $directoryList = new DirectoryList(BP);
  77. $composerJsonFinder = new ComposerJsonFinder($directoryList);
  78. $productMetadata = new ProductMetadata($composerJsonFinder);
  79. $version = $productMetadata->getVersion();
  80. }
  81. parent::__construct($name, $version);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. *
  86. * @throws \Exception The exception in case of unexpected error
  87. */
  88. public function doRun(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
  89. {
  90. $exitCode = parent::doRun($input, $output);
  91. if ($this->initException) {
  92. throw $this->initException;
  93. }
  94. return $exitCode;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. protected function getDefaultCommands()
  100. {
  101. return array_merge(parent::getDefaultCommands(), $this->getApplicationCommands());
  102. }
  103. /**
  104. * Gets application commands.
  105. *
  106. * @return array a list of available application commands
  107. */
  108. protected function getApplicationCommands()
  109. {
  110. $commands = [];
  111. try {
  112. if (class_exists(\Magento\Setup\Console\CommandList::class)) {
  113. $setupCommandList = new \Magento\Setup\Console\CommandList($this->serviceManager);
  114. $commands = array_merge($commands, $setupCommandList->getCommands());
  115. }
  116. if ($this->objectManager->get(DeploymentConfig::class)->isAvailable()) {
  117. /** @var CommandListInterface */
  118. $commandList = $this->objectManager->create(CommandListInterface::class);
  119. $commands = array_merge($commands, $commandList->getCommands());
  120. }
  121. $commands = array_merge(
  122. $commands,
  123. $this->getVendorCommands($this->objectManager)
  124. );
  125. } catch (\Exception $e) {
  126. $this->initException = $e;
  127. }
  128. return $commands;
  129. }
  130. /**
  131. * Object Manager initialization.
  132. *
  133. * @return void
  134. */
  135. private function initObjectManager()
  136. {
  137. $params = (new ComplexParameter(self::INPUT_KEY_BOOTSTRAP))->mergeFromArgv($_SERVER, $_SERVER);
  138. $params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null;
  139. $params = $this->documentRootResolver($params);
  140. $requestParams = $this->serviceManager->get('magento-init-params');
  141. $appBootstrapKey = Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS;
  142. if (isset($requestParams[$appBootstrapKey]) && !isset($params[$appBootstrapKey])) {
  143. $params[$appBootstrapKey] = $requestParams[$appBootstrapKey];
  144. }
  145. $this->objectManager = Bootstrap::create(BP, $params)->getObjectManager();
  146. /** @var ObjectManagerProvider $omProvider */
  147. $omProvider = $this->serviceManager->get(ObjectManagerProvider::class);
  148. $omProvider->setObjectManager($this->objectManager);
  149. }
  150. /**
  151. * Checks whether compiler is being prepared.
  152. *
  153. * @return void
  154. * @throws GenerationDirectoryAccessException If generation directory is read-only
  155. */
  156. private function assertCompilerPreparation()
  157. {
  158. /**
  159. * Temporary workaround until the compiler is able to clear the generation directory
  160. * @todo remove after MAGETWO-44493 resolved
  161. */
  162. if (class_exists(CompilerPreparation::class)) {
  163. $compilerPreparation = new CompilerPreparation(
  164. $this->serviceManager,
  165. new Console\Input\ArgvInput(),
  166. new File()
  167. );
  168. $compilerPreparation->handleCompilerEnvironment();
  169. }
  170. }
  171. /**
  172. * Retrieves vendor commands.
  173. *
  174. * @param ObjectManagerInterface $objectManager the object manager
  175. *
  176. * @return array an array with external commands
  177. */
  178. protected function getVendorCommands($objectManager)
  179. {
  180. $commands = [];
  181. foreach (CommandLocator::getCommands() as $commandListClass) {
  182. if (class_exists($commandListClass)) {
  183. $commands = array_merge(
  184. $commands,
  185. $objectManager->create($commandListClass)->getCommands()
  186. );
  187. }
  188. }
  189. return $commands;
  190. }
  191. /**
  192. * Provides updated configuration in
  193. * accordance to document root settings.
  194. *
  195. * @param array $config
  196. * @return array
  197. */
  198. private function documentRootResolver(array $config = []): array
  199. {
  200. $params = [];
  201. $deploymentConfig = $this->serviceManager->get(DeploymentConfig::class);
  202. if ((bool)$deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_DOCUMENT_ROOT_IS_PUB)) {
  203. $params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] = [
  204. DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
  205. DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'media'],
  206. DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'static'],
  207. DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'media/upload'],
  208. ];
  209. }
  210. return array_merge_recursive($config, $params);
  211. }
  212. }