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 \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 ;
11
20
use Magento \Setup \Model \PackagesAuth ;
12
21
use Symfony \Component \Console \Command \Command ;
13
22
use Symfony \Component \Console \Input \ArrayInput ;
23
+ use Symfony \Component \Console \Input \ArrayInputFactory ;
14
24
use Symfony \Component \Console \Input \InputInterface ;
15
25
use Symfony \Component \Console \Input \InputOption ;
16
26
use Symfony \Component \Console \Output \OutputInterface ;
17
27
18
28
/**
19
29
* Command for deployment of Sample Data
30
+ *
31
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20
32
*/
21
33
class SampleDataDeployCommand extends Command
22
34
{
23
35
const OPTION_NO_UPDATE = 'no-update ' ;
24
36
25
37
/**
26
- * @var \Magento\Framework\ Filesystem
38
+ * @var Filesystem
27
39
*/
28
40
private $ filesystem ;
29
41
30
42
/**
31
- * @var \Magento\SampleData\Model\ Dependency
43
+ * @var Dependency
32
44
*/
33
45
private $ sampleDataDependency ;
34
46
35
47
/**
36
- * @var \Symfony\Component\Console\Input\ ArrayInputFactory
48
+ * @var ArrayInputFactory
37
49
* @deprecated 100.1.0
38
50
*/
39
51
private $ arrayInputFactory ;
40
52
41
53
/**
42
- * @var \Composer\Console\ ApplicationFactory
54
+ * @var ApplicationFactory
43
55
*/
44
56
private $ applicationFactory ;
45
57
46
58
/**
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
51
69
*/
52
70
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
57
76
) {
58
77
$ this ->filesystem = $ filesystem ;
59
78
$ this ->sampleDataDependency = $ sampleDataDependency ;
60
79
$ this ->arrayInputFactory = $ arrayInputFactory ;
61
80
$ this ->applicationFactory = $ applicationFactory ;
81
+ $ this ->serializer = $ serializer ;
62
82
parent ::__construct ();
63
83
}
64
84
65
85
/**
66
- * { @inheritdoc}
86
+ * @inheritdoc
67
87
*/
68
88
protected function configure ()
69
89
{
@@ -79,15 +99,42 @@ protected function configure()
79
99
}
80
100
81
101
/**
82
- * {@inheritdoc}
102
+ * @inheritdoc
103
+ *
104
+ * @param InputInterface $input
105
+ * @param OutputInterface $output
106
+ * @return int
107
+ * @throws FileSystemException
108
+ * @throws LocalizedException
83
109
*/
84
110
protected function execute (InputInterface $ input , OutputInterface $ output )
85
111
{
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
+ ]);
91
138
}
92
139
$ this ->updateMemoryLimit ();
93
140
$ this ->createAuthFile ();
@@ -109,16 +156,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
109
156
/** @var Application $application */
110
157
$ application = $ this ->applicationFactory ->create ();
111
158
$ 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
+ }
112
165
$ result = $ application ->run ($ commandInput , $ output );
113
166
if ($ result !== 0 ) {
114
167
$ output ->writeln (
115
168
'<info> ' . 'There is an error during sample data deployment. Composer file will be reverted. '
116
169
. '</info> '
117
170
);
118
171
$ application ->resetComposer ();
172
+
173
+ return Cli::RETURN_FAILURE ;
119
174
}
175
+
176
+ return Cli::RETURN_SUCCESS ;
120
177
} else {
121
178
$ output ->writeln ('<info> ' . 'There is no sample data for current set of modules. ' . '</info> ' );
179
+
180
+ return Cli::RETURN_FAILURE ;
122
181
}
123
182
}
124
183
@@ -128,7 +187,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
128
187
* We create auth.json with correct permissions instead of relying on Composer.
129
188
*
130
189
* @return void
131
- * @throws \Exception
190
+ * @throws LocalizedException
132
191
*/
133
192
private function createAuthFile ()
134
193
{
@@ -137,30 +196,51 @@ private function createAuthFile()
137
196
if (!$ directory ->isExist (PackagesAuth::PATH_TO_AUTH_FILE )) {
138
197
try {
139
198
$ 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
+ ) );
145
204
}
146
205
}
147
206
}
148
207
149
208
/**
209
+ * Updates PHP memory limit
210
+ *
211
+ * @throws InvalidArgumentException
150
212
* @return void
151
213
*/
152
214
private function updateMemoryLimit ()
153
215
{
154
216
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
+ }
156
226
$ memoryLimit = trim (ini_get ('memory_limit ' ));
157
227
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
+ }
159
237
}
160
238
}
161
239
}
162
240
163
241
/**
242
+ * Retrieves the memory size in bytes
243
+ *
164
244
* @param string $value
165
245
* @return int
166
246
*/
0 commit comments