7
7
namespace Magento \SampleData \Console \Command ;
8
8
9
9
use Composer \Console \Application ;
10
+ use Composer \Console \ApplicationFactory ;
11
+ use Exception ;
10
12
use Magento \Framework \App \Filesystem \DirectoryList ;
13
+ use Magento \Framework \Exception \FileSystemException ;
14
+ use Magento \Framework \Exception \LocalizedException ;
15
+ use Magento \Framework \Filesystem ;
16
+ use Magento \SampleData \Model \Dependency ;
11
17
use Magento \Setup \Model \PackagesAuth ;
12
18
use Symfony \Component \Console \Command \Command ;
13
19
use Symfony \Component \Console \Input \ArrayInput ;
20
+ use Symfony \Component \Console \Input \ArrayInputFactory ;
14
21
use Symfony \Component \Console \Input \InputInterface ;
15
22
use Symfony \Component \Console \Input \InputOption ;
16
23
use Symfony \Component \Console \Output \OutputInterface ;
@@ -23,37 +30,37 @@ class SampleDataDeployCommand extends Command
23
30
const OPTION_NO_UPDATE = 'no-update ' ;
24
31
25
32
/**
26
- * @var \Magento\Framework\ Filesystem
33
+ * @var Filesystem
27
34
*/
28
35
private $ filesystem ;
29
36
30
37
/**
31
- * @var \Magento\SampleData\Model\ Dependency
38
+ * @var Dependency
32
39
*/
33
40
private $ sampleDataDependency ;
34
41
35
42
/**
36
- * @var \Symfony\Component\Console\Input\ ArrayInputFactory
43
+ * @var ArrayInputFactory
37
44
* @deprecated 100.1.0
38
45
*/
39
46
private $ arrayInputFactory ;
40
47
41
48
/**
42
- * @var \Composer\Console\ ApplicationFactory
49
+ * @var ApplicationFactory
43
50
*/
44
51
private $ applicationFactory ;
45
52
46
53
/**
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
54
+ * @param Filesystem $filesystem
55
+ * @param Dependency $sampleDataDependency
56
+ * @param ArrayInputFactory $arrayInputFactory
57
+ * @param ApplicationFactory $applicationFactory
51
58
*/
52
59
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
60
+ Filesystem $ filesystem ,
61
+ Dependency $ sampleDataDependency ,
62
+ ArrayInputFactory $ arrayInputFactory ,
63
+ ApplicationFactory $ applicationFactory
57
64
) {
58
65
$ this ->filesystem = $ filesystem ;
59
66
$ this ->sampleDataDependency = $ sampleDataDependency ;
@@ -63,7 +70,7 @@ public function __construct(
63
70
}
64
71
65
72
/**
66
- * { @inheritdoc}
73
+ * @inheritdoc
67
74
*/
68
75
protected function configure ()
69
76
{
@@ -79,15 +86,42 @@ protected function configure()
79
86
}
80
87
81
88
/**
82
- * {@inheritdoc}
89
+ * @inheritdoc
90
+ *
91
+ * @param InputInterface $input
92
+ * @param OutputInterface $output
93
+ * @return int|void
94
+ * @throws FileSystemException
95
+ * @throws LocalizedException
83
96
*/
84
97
protected function execute (InputInterface $ input , OutputInterface $ output )
85
98
{
86
- $ rootJson = json_decode ($ this ->filesystem ->getDirectoryRead (DirectoryList::ROOT )->readFile ("composer.json " ));
99
+ $ rootJson = json_decode (
100
+ $ this ->filesystem ->getDirectoryRead (
101
+ DirectoryList::ROOT
102
+ )->readFile ("composer.json " )
103
+ );
87
104
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 ;
105
+ $ magentoProductPackage = array_filter (
106
+ (array ) $ rootJson ->require ,
107
+ function ($ package ) {
108
+ return false !== strpos ($ package , 'magento/product- ' );
109
+ },
110
+ ARRAY_FILTER_USE_KEY
111
+ );
112
+ $ version = reset ($ magentoProductPackage );
113
+ $ output ->writeln (
114
+ '<info> ' .
115
+ // @codingStandardsIgnoreLine
116
+ '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. ' .
117
+ '</info> '
118
+ );
119
+ $ restoreVersion = new ArrayInput ([
120
+ 'command ' => 'config ' ,
121
+ 'setting-key ' => 'version ' ,
122
+ 'setting-value ' => [$ version ],
123
+ '--quiet ' => 1
124
+ ]);
91
125
}
92
126
$ this ->updateMemoryLimit ();
93
127
$ this ->createAuthFile ();
@@ -109,6 +143,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
109
143
/** @var Application $application */
110
144
$ application = $ this ->applicationFactory ->create ();
111
145
$ application ->setAutoExit (false );
146
+ if (!empty ($ restoreVersion )) {
147
+ $ result = $ application ->run ($ restoreVersion , clone $ output );
148
+ if ($ result === 0 ) {
149
+ $ output ->writeln ('<info>The field "version" has been restored.</info> ' );
150
+ }
151
+ }
112
152
$ result = $ application ->run ($ commandInput , $ output );
113
153
if ($ result !== 0 ) {
114
154
$ output ->writeln (
@@ -128,7 +168,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
128
168
* We create auth.json with correct permissions instead of relying on Composer.
129
169
*
130
170
* @return void
131
- * @throws \Exception
171
+ * @throws LocalizedException
132
172
*/
133
173
private function createAuthFile ()
134
174
{
@@ -137,16 +177,18 @@ private function createAuthFile()
137
177
if (!$ directory ->isExist (PackagesAuth::PATH_TO_AUTH_FILE )) {
138
178
try {
139
179
$ 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 );
180
+ } catch (Exception $ e ) {
181
+ throw new LocalizedException ( __ (
182
+ ' Error in writing Auth file %1. Please check permissions for writing. ' ,
183
+ $ directory -> getAbsolutePath (PackagesAuth:: PATH_TO_AUTH_FILE )
184
+ ) );
145
185
}
146
186
}
147
187
}
148
188
149
189
/**
190
+ * Updates PHP memory limit
191
+ *
150
192
* @return void
151
193
*/
152
194
private function updateMemoryLimit ()
@@ -161,6 +203,8 @@ private function updateMemoryLimit()
161
203
}
162
204
163
205
/**
206
+ * Retrieves the memory size in bytes
207
+ *
164
208
* @param string $value
165
209
* @return int
166
210
*/
0 commit comments