Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SampleDataDeployCommand.php
Go to the documentation of this file.
1 <?php
8 
9 use Composer\Console\Application;
12 use Symfony\Component\Console\Command\Command;
13 use Symfony\Component\Console\Input\ArrayInput;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Input\InputOption;
16 use Symfony\Component\Console\Output\OutputInterface;
17 
21 class SampleDataDeployCommand extends Command
22 {
23  const OPTION_NO_UPDATE = 'no-update';
24 
28  private $filesystem;
29 
33  private $sampleDataDependency;
34 
39  private $arrayInputFactory;
40 
44  private $applicationFactory;
45 
52  public function __construct(
53  \Magento\Framework\Filesystem $filesystem,
54  \Magento\SampleData\Model\Dependency $sampleDataDependency,
55  \Symfony\Component\Console\Input\ArrayInputFactory $arrayInputFactory,
56  \Composer\Console\ApplicationFactory $applicationFactory
57  ) {
58  $this->filesystem = $filesystem;
59  $this->sampleDataDependency = $sampleDataDependency;
60  $this->arrayInputFactory = $arrayInputFactory;
61  $this->applicationFactory = $applicationFactory;
62  parent::__construct();
63  }
64 
68  protected function configure()
69  {
70  $this->setName('sampledata:deploy')
71  ->setDescription('Deploy sample data modules for composer-based Magento installations');
72  $this->addOption(
73  self::OPTION_NO_UPDATE,
74  null,
75  InputOption::VALUE_NONE,
76  'Update composer.json without executing composer update'
77  );
78  parent::configure();
79  }
80 
84  protected function execute(InputInterface $input, OutputInterface $output)
85  {
86  $rootJson = json_decode($this->filesystem->getDirectoryRead(DirectoryList::ROOT)->readFile("composer.json"));
87  if (!isset($rootJson->version)) {
88  // @codingStandardsIgnoreLine
89  $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>');
90  return;
91  }
92  $this->updateMemoryLimit();
93  $this->createAuthFile();
94  $sampleDataPackages = $this->sampleDataDependency->getSampleDataPackages();
95  if (!empty($sampleDataPackages)) {
96  $baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
97  $commonArgs = ['--working-dir' => $baseDir, '--no-progress' => 1];
98  if ($input->getOption(self::OPTION_NO_UPDATE)) {
99  $commonArgs['--no-update'] = 1;
100  }
101  $packages = [];
102  foreach ($sampleDataPackages as $name => $version) {
103  $packages[] = "$name:$version";
104  }
105  $commonArgs = array_merge(['packages' => $packages], $commonArgs);
106  $arguments = array_merge(['command' => 'require'], $commonArgs);
107  $commandInput = new ArrayInput($arguments);
108 
110  $application = $this->applicationFactory->create();
111  $application->setAutoExit(false);
112  $result = $application->run($commandInput, $output);
113  if ($result !== 0) {
114  $output->writeln(
115  '<info>' . 'There is an error during sample data deployment. Composer file will be reverted.'
116  . '</info>'
117  );
118  $application->resetComposer();
119  }
120  } else {
121  $output->writeln('<info>' . 'There is no sample data for current set of modules.' . '</info>');
122  }
123  }
124 
133  private function createAuthFile()
134  {
135  $directory = $this->filesystem->getDirectoryWrite(DirectoryList::COMPOSER_HOME);
136 
137  if (!$directory->isExist(PackagesAuth::PATH_TO_AUTH_FILE)) {
138  try {
139  $directory->writeFile(PackagesAuth::PATH_TO_AUTH_FILE, '{}');
140  } catch (\Exception $e) {
141  $message = 'Error in writing Auth file '
142  . $directory->getAbsolutePath(PackagesAuth::PATH_TO_AUTH_FILE)
143  . '. Please check permissions for writing.';
144  throw new \Exception($message);
145  }
146  }
147  }
148 
152  private function updateMemoryLimit()
153  {
154  if (function_exists('ini_set')) {
155  @ini_set('display_errors', 1);
156  $memoryLimit = trim(ini_get('memory_limit'));
157  if ($memoryLimit != -1 && $this->getMemoryInBytes($memoryLimit) < 756 * 1024 * 1024) {
158  @ini_set('memory_limit', '756M');
159  }
160  }
161  }
162 
167  private function getMemoryInBytes($value)
168  {
169  $unit = strtolower(substr($value, -1, 1));
170  $value = (int) $value;
171  switch ($unit) {
172  case 'g':
173  $value *= 1024 * 1024 * 1024;
174  break;
175  case 'm':
176  $value *= 1024 * 1024;
177  break;
178  case 'k':
179  $value *= 1024;
180  }
181  return $value;
182  }
183 }
ini_set($varName, $newValue)
$baseDir
Definition: autoload.php:9
$message
$application
Definition: bootstrap.php:58
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Filesystem $filesystem, \Magento\SampleData\Model\Dependency $sampleDataDependency, \Symfony\Component\Console\Input\ArrayInputFactory $arrayInputFactory, \Composer\Console\ApplicationFactory $applicationFactory)
$arguments
$filesystem
if(!isset($_GET['name'])) $name
Definition: log.php:14