Skip to content

Commit a8e469d

Browse files
authored
ENGCOM-7219: Fixed issue #19481: After composer installation sample data can't be installed from command line #27481
2 parents 526d6a0 + bec39b9 commit a8e469d

File tree

5 files changed

+374
-111
lines changed

5 files changed

+374
-111
lines changed

app/code/Magento/SampleData/Console/Command/SampleDataDeployCommand.php

Lines changed: 107 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,83 @@
77
namespace Magento\SampleData\Console\Command;
88

99
use Composer\Console\Application;
10+
use Composer\Console\ApplicationFactory;
11+
use Exception;
1012
use Magento\Framework\App\Filesystem\DirectoryList;
13+
use Magento\Framework\Console\Cli;
14+
use Magento\Framework\Exception\FileSystemException;
15+
use Magento\Framework\Exception\InvalidArgumentException;
16+
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\Framework\Filesystem;
18+
use Magento\Framework\Serialize\Serializer\Json;
19+
use Magento\SampleData\Model\Dependency;
1120
use Magento\Setup\Model\PackagesAuth;
1221
use Symfony\Component\Console\Command\Command;
1322
use Symfony\Component\Console\Input\ArrayInput;
23+
use Symfony\Component\Console\Input\ArrayInputFactory;
1424
use Symfony\Component\Console\Input\InputInterface;
1525
use Symfony\Component\Console\Input\InputOption;
1626
use Symfony\Component\Console\Output\OutputInterface;
1727

1828
/**
1929
* Command for deployment of Sample Data
30+
*
31+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2032
*/
2133
class SampleDataDeployCommand extends Command
2234
{
2335
const OPTION_NO_UPDATE = 'no-update';
2436

2537
/**
26-
* @var \Magento\Framework\Filesystem
38+
* @var Filesystem
2739
*/
2840
private $filesystem;
2941

3042
/**
31-
* @var \Magento\SampleData\Model\Dependency
43+
* @var Dependency
3244
*/
3345
private $sampleDataDependency;
3446

3547
/**
36-
* @var \Symfony\Component\Console\Input\ArrayInputFactory
48+
* @var ArrayInputFactory
3749
* @deprecated 100.1.0
3850
*/
3951
private $arrayInputFactory;
4052

4153
/**
42-
* @var \Composer\Console\ApplicationFactory
54+
* @var ApplicationFactory
4355
*/
4456
private $applicationFactory;
4557

4658
/**
47-
* @param \Magento\Framework\Filesystem $filesystem
48-
* @param \Magento\SampleData\Model\Dependency $sampleDataDependency
49-
* @param \Symfony\Component\Console\Input\ArrayInputFactory $arrayInputFactory
50-
* @param \Composer\Console\ApplicationFactory $applicationFactory
59+
* @var Json
60+
*/
61+
private $serializer;
62+
63+
/**
64+
* @param Filesystem $filesystem
65+
* @param Dependency $sampleDataDependency
66+
* @param ArrayInputFactory $arrayInputFactory
67+
* @param ApplicationFactory $applicationFactory
68+
* @param Json $serializer
5169
*/
5270
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
71+
Filesystem $filesystem,
72+
Dependency $sampleDataDependency,
73+
ArrayInputFactory $arrayInputFactory,
74+
ApplicationFactory $applicationFactory,
75+
Json $serializer
5776
) {
5877
$this->filesystem = $filesystem;
5978
$this->sampleDataDependency = $sampleDataDependency;
6079
$this->arrayInputFactory = $arrayInputFactory;
6180
$this->applicationFactory = $applicationFactory;
81+
$this->serializer = $serializer;
6282
parent::__construct();
6383
}
6484

6585
/**
66-
* {@inheritdoc}
86+
* @inheritdoc
6787
*/
6888
protected function configure()
6989
{
@@ -79,15 +99,42 @@ protected function configure()
7999
}
80100

81101
/**
82-
* {@inheritdoc}
102+
* @inheritdoc
103+
*
104+
* @param InputInterface $input
105+
* @param OutputInterface $output
106+
* @return int
107+
* @throws FileSystemException
108+
* @throws LocalizedException
83109
*/
84110
protected function execute(InputInterface $input, OutputInterface $output)
85111
{
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;
112+
$rootJson = $this->serializer->unserialize(
113+
$this->filesystem->getDirectoryRead(
114+
DirectoryList::ROOT
115+
)->readFile("composer.json")
116+
);
117+
if (!isset($rootJson['version'])) {
118+
$magentoProductPackage = array_filter(
119+
$rootJson['require'],
120+
function ($package) {
121+
return false !== strpos($package, 'magento/product-');
122+
},
123+
ARRAY_FILTER_USE_KEY
124+
);
125+
$version = reset($magentoProductPackage);
126+
$output->writeln(
127+
'<info>' .
128+
// @codingStandardsIgnoreLine
129+
'We don\'t recommend to remove the "version" field from your composer.json; see https://getcomposer.org/doc/02-libraries.md#library-versioning for more information.' .
130+
'</info>'
131+
);
132+
$restoreVersion = new ArrayInput([
133+
'command' => 'config',
134+
'setting-key' => 'version',
135+
'setting-value' => [$version],
136+
'--quiet' => 1
137+
]);
91138
}
92139
$this->updateMemoryLimit();
93140
$this->createAuthFile();
@@ -109,16 +156,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
109156
/** @var Application $application */
110157
$application = $this->applicationFactory->create();
111158
$application->setAutoExit(false);
159+
if (!empty($restoreVersion)) {
160+
$result = $application->run($restoreVersion, clone $output);
161+
if ($result === 0) {
162+
$output->writeln('<info>The field "version" has been restored.</info>');
163+
}
164+
}
112165
$result = $application->run($commandInput, $output);
113166
if ($result !== 0) {
114167
$output->writeln(
115168
'<info>' . 'There is an error during sample data deployment. Composer file will be reverted.'
116169
. '</info>'
117170
);
118171
$application->resetComposer();
172+
173+
return Cli::RETURN_FAILURE;
119174
}
175+
176+
return Cli::RETURN_SUCCESS;
120177
} else {
121178
$output->writeln('<info>' . 'There is no sample data for current set of modules.' . '</info>');
179+
180+
return Cli::RETURN_FAILURE;
122181
}
123182
}
124183

@@ -128,7 +187,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
128187
* We create auth.json with correct permissions instead of relying on Composer.
129188
*
130189
* @return void
131-
* @throws \Exception
190+
* @throws LocalizedException
132191
*/
133192
private function createAuthFile()
134193
{
@@ -137,30 +196,51 @@ private function createAuthFile()
137196
if (!$directory->isExist(PackagesAuth::PATH_TO_AUTH_FILE)) {
138197
try {
139198
$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);
199+
} catch (Exception $e) {
200+
throw new LocalizedException(__(
201+
'Error in writing Auth file %1. Please check permissions for writing.',
202+
$directory->getAbsolutePath(PackagesAuth::PATH_TO_AUTH_FILE)
203+
));
145204
}
146205
}
147206
}
148207

149208
/**
209+
* Updates PHP memory limit
210+
*
211+
* @throws InvalidArgumentException
150212
* @return void
151213
*/
152214
private function updateMemoryLimit()
153215
{
154216
if (function_exists('ini_set')) {
155-
@ini_set('display_errors', 1);
217+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
218+
$result = ini_set('display_errors', 1);
219+
if ($result === false) {
220+
$error = error_get_last();
221+
throw new InvalidArgumentException(__(
222+
'Failed to set ini option display_errors to value 1. %1',
223+
$error['message']
224+
));
225+
}
156226
$memoryLimit = trim(ini_get('memory_limit'));
157227
if ($memoryLimit != -1 && $this->getMemoryInBytes($memoryLimit) < 756 * 1024 * 1024) {
158-
@ini_set('memory_limit', '756M');
228+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
229+
$result = ini_set('memory_limit', '756M');
230+
if ($result === false) {
231+
$error = error_get_last();
232+
throw new InvalidArgumentException(__(
233+
'Failed to set ini option memory_limit to 756M. %1',
234+
$error['message']
235+
));
236+
}
159237
}
160238
}
161239
}
162240

163241
/**
242+
* Retrieves the memory size in bytes
243+
*
164244
* @param string $value
165245
* @return int
166246
*/

0 commit comments

Comments
 (0)