SampleDataDeployCommand.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SampleData\Console\Command;
  7. use Composer\Console\Application;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Setup\Model\PackagesAuth;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\ArrayInput;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. /**
  16. * Command for deployment of Sample Data
  17. */
  18. class SampleDataDeployCommand extends Command
  19. {
  20. const OPTION_NO_UPDATE = 'no-update';
  21. /**
  22. * @var \Magento\Framework\Filesystem
  23. */
  24. private $filesystem;
  25. /**
  26. * @var \Magento\SampleData\Model\Dependency
  27. */
  28. private $sampleDataDependency;
  29. /**
  30. * @var \Symfony\Component\Console\Input\ArrayInputFactory
  31. * @deprecated 100.1.0
  32. */
  33. private $arrayInputFactory;
  34. /**
  35. * @var \Composer\Console\ApplicationFactory
  36. */
  37. private $applicationFactory;
  38. /**
  39. * @param \Magento\Framework\Filesystem $filesystem
  40. * @param \Magento\SampleData\Model\Dependency $sampleDataDependency
  41. * @param \Symfony\Component\Console\Input\ArrayInputFactory $arrayInputFactory
  42. * @param \Composer\Console\ApplicationFactory $applicationFactory
  43. */
  44. public function __construct(
  45. \Magento\Framework\Filesystem $filesystem,
  46. \Magento\SampleData\Model\Dependency $sampleDataDependency,
  47. \Symfony\Component\Console\Input\ArrayInputFactory $arrayInputFactory,
  48. \Composer\Console\ApplicationFactory $applicationFactory
  49. ) {
  50. $this->filesystem = $filesystem;
  51. $this->sampleDataDependency = $sampleDataDependency;
  52. $this->arrayInputFactory = $arrayInputFactory;
  53. $this->applicationFactory = $applicationFactory;
  54. parent::__construct();
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function configure()
  60. {
  61. $this->setName('sampledata:deploy')
  62. ->setDescription('Deploy sample data modules for composer-based Magento installations');
  63. $this->addOption(
  64. self::OPTION_NO_UPDATE,
  65. null,
  66. InputOption::VALUE_NONE,
  67. 'Update composer.json without executing composer update'
  68. );
  69. parent::configure();
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. protected function execute(InputInterface $input, OutputInterface $output)
  75. {
  76. $rootJson = json_decode($this->filesystem->getDirectoryRead(DirectoryList::ROOT)->readFile("composer.json"));
  77. if (!isset($rootJson->version)) {
  78. // @codingStandardsIgnoreLine
  79. $output->writeln('<info>' . 'Git installations must deploy sample data from GitHub; see https://devdocs.magento.com/guides/v2.3/install-gde/install/sample-data-after-clone.html for more information.' . '</info>');
  80. return;
  81. }
  82. $this->updateMemoryLimit();
  83. $this->createAuthFile();
  84. $sampleDataPackages = $this->sampleDataDependency->getSampleDataPackages();
  85. if (!empty($sampleDataPackages)) {
  86. $baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
  87. $commonArgs = ['--working-dir' => $baseDir, '--no-progress' => 1];
  88. if ($input->getOption(self::OPTION_NO_UPDATE)) {
  89. $commonArgs['--no-update'] = 1;
  90. }
  91. $packages = [];
  92. foreach ($sampleDataPackages as $name => $version) {
  93. $packages[] = "$name:$version";
  94. }
  95. $commonArgs = array_merge(['packages' => $packages], $commonArgs);
  96. $arguments = array_merge(['command' => 'require'], $commonArgs);
  97. $commandInput = new ArrayInput($arguments);
  98. /** @var Application $application */
  99. $application = $this->applicationFactory->create();
  100. $application->setAutoExit(false);
  101. $result = $application->run($commandInput, $output);
  102. if ($result !== 0) {
  103. $output->writeln(
  104. '<info>' . 'There is an error during sample data deployment. Composer file will be reverted.'
  105. . '</info>'
  106. );
  107. $application->resetComposer();
  108. }
  109. } else {
  110. $output->writeln('<info>' . 'There is no sample data for current set of modules.' . '</info>');
  111. }
  112. }
  113. /**
  114. * Create new auth.json file if it doesn't exist.
  115. *
  116. * We create auth.json with correct permissions instead of relying on Composer.
  117. *
  118. * @return void
  119. * @throws \Exception
  120. */
  121. private function createAuthFile()
  122. {
  123. $directory = $this->filesystem->getDirectoryWrite(DirectoryList::COMPOSER_HOME);
  124. if (!$directory->isExist(PackagesAuth::PATH_TO_AUTH_FILE)) {
  125. try {
  126. $directory->writeFile(PackagesAuth::PATH_TO_AUTH_FILE, '{}');
  127. } catch (\Exception $e) {
  128. $message = 'Error in writing Auth file '
  129. . $directory->getAbsolutePath(PackagesAuth::PATH_TO_AUTH_FILE)
  130. . '. Please check permissions for writing.';
  131. throw new \Exception($message);
  132. }
  133. }
  134. }
  135. /**
  136. * @return void
  137. */
  138. private function updateMemoryLimit()
  139. {
  140. if (function_exists('ini_set')) {
  141. @ini_set('display_errors', 1);
  142. $memoryLimit = trim(ini_get('memory_limit'));
  143. if ($memoryLimit != -1 && $this->getMemoryInBytes($memoryLimit) < 756 * 1024 * 1024) {
  144. @ini_set('memory_limit', '756M');
  145. }
  146. }
  147. }
  148. /**
  149. * @param string $value
  150. * @return int
  151. */
  152. private function getMemoryInBytes($value)
  153. {
  154. $unit = strtolower(substr($value, -1, 1));
  155. $value = (int) $value;
  156. switch ($unit) {
  157. case 'g':
  158. $value *= 1024 * 1024 * 1024;
  159. break;
  160. case 'm':
  161. $value *= 1024 * 1024;
  162. break;
  163. case 'k':
  164. $value *= 1024;
  165. }
  166. return $value;
  167. }
  168. }