7
7
8
8
namespace Magento \Developer \Console \Command ;
9
9
10
- use Magento \Developer \ Model \ Di \ Information ;
10
+ use Magento \Framework \ App \ ObjectManager ;
11
11
use Magento \Framework \Component \ComponentRegistrar ;
12
12
use Magento \Framework \Console \Cli ;
13
+ use Magento \Framework \Exception \FileSystemException ;
14
+ use Magento \Framework \Filesystem \DirectoryList ;
15
+ use Magento \Framework \Filesystem \Directory \ReadFactory ;
16
+ use Magento \Framework \Filesystem \Directory \WriteFactory ;
13
17
use Symfony \Component \Console \Command \Command ;
14
- use Symfony \Component \Console \Exception \ InvalidArgumentException ;
18
+ use Symfony \Component \Console \Input \ InputArgument ;
15
19
use Symfony \Component \Console \Input \InputInterface ;
16
20
use Symfony \Component \Console \Input \InputOption ;
17
21
use Symfony \Component \Console \Output \OutputInterface ;
18
- use Symfony \Component \Console \Input \InputArgument ;
19
- use Symfony \Component \Console \Helper \Table ;
20
22
21
23
/**
22
24
* Allows to generate setup patches
@@ -37,20 +39,45 @@ class GeneratePatchCommand extends Command
37
39
*/
38
40
private $ componentRegistrar ;
39
41
42
+ /**
43
+ * @var DirectoryList
44
+ */
45
+ private $ directoryList ;
46
+
47
+ /**
48
+ * @var ReadFactory
49
+ */
50
+ private $ readFactory ;
51
+
52
+ /**
53
+ * @var WriteFactory
54
+ */
55
+ private $ writeFactory ;
56
+
40
57
/**
41
58
* GeneratePatchCommand constructor.
59
+ *
42
60
* @param ComponentRegistrar $componentRegistrar
61
+ * @param DirectoryList|null $directoryList
62
+ * @param ReadFactory|null $readFactory
63
+ * @param WriteFactory|null $writeFactory
43
64
*/
44
- public function __construct (ComponentRegistrar $ componentRegistrar )
45
- {
65
+ public function __construct (
66
+ ComponentRegistrar $ componentRegistrar ,
67
+ DirectoryList $ directoryList = null ,
68
+ ReadFactory $ readFactory = null ,
69
+ WriteFactory $ writeFactory = null
70
+ ) {
46
71
$ this ->componentRegistrar = $ componentRegistrar ;
72
+ $ this ->directoryList = $ directoryList ?: ObjectManager::getInstance ()->get (DirectoryList::class);
73
+ $ this ->readFactory = $ readFactory ?: ObjectManager::getInstance ()->get (ReadFactory::class);
74
+ $ this ->writeFactory = $ writeFactory ?: ObjectManager::getInstance ()->get (WriteFactory::class);
75
+
47
76
parent ::__construct ();
48
77
}
49
78
50
79
/**
51
- * @inheritdoc
52
- *
53
- * @throws InvalidArgumentException
80
+ * Configures the current command.
54
81
*/
55
82
protected function configure ()
56
83
{
@@ -89,24 +116,21 @@ protected function configure()
89
116
}
90
117
91
118
/**
92
- * Patch template
119
+ * Execute command
93
120
*
94
- * @return string
95
- */
96
- private function getPatchTemplate (): string
97
- {
98
- // phpcs:ignore Magento2.Functions.DiscouragedFunction
99
- return file_get_contents (__DIR__ . '/patch_template.php.dist ' );
100
- }
101
-
102
- /**
103
- * @inheritdoc
104
- * @throws \InvalidArgumentException
121
+ * @param InputInterface $input
122
+ * @param OutputInterface $output
123
+ * @return int
124
+ * @throws FileSystemException
105
125
*/
106
126
protected function execute (InputInterface $ input , OutputInterface $ output ): int
107
127
{
108
128
$ moduleName = $ input ->getArgument (self ::MODULE_NAME );
109
129
$ patchName = $ input ->getArgument (self ::INPUT_KEY_PATCH_NAME );
130
+ $ includeRevertMethod = false ;
131
+ if ($ input ->getOption (self ::INPUT_KEY_IS_REVERTABLE )) {
132
+ $ includeRevertMethod = true ;
133
+ }
110
134
$ type = $ input ->getOption (self ::INPUT_KEY_PATCH_TYPE );
111
135
$ modulePath = $ this ->componentRegistrar ->getPath (ComponentRegistrar::MODULE , $ moduleName );
112
136
$ preparedModuleName = str_replace ('_ ' , '\\' , $ moduleName );
@@ -115,18 +139,75 @@ protected function execute(InputInterface $input, OutputInterface $output): int
115
139
$ patchTemplateData = $ this ->getPatchTemplate ();
116
140
$ patchTemplateData = str_replace ('%moduleName% ' , $ preparedModuleName , $ patchTemplateData );
117
141
$ patchTemplateData = str_replace ('%patchType% ' , $ preparedType , $ patchTemplateData );
118
- $ patchTemplateData = str_replace ('%patchInterface% ' , $ patchInterface , $ patchTemplateData );
119
142
$ patchTemplateData = str_replace ('%class% ' , $ patchName , $ patchTemplateData );
120
- $ patchDir = $ patchToFile = $ modulePath . '/Setup/Patch/ ' . $ preparedType ;
121
143
122
- // phpcs:ignore Magento2.Functions.DiscouragedFunction
123
- if (!is_dir ($ patchDir )) {
124
- // phpcs:ignore Magento2.Functions.DiscouragedFunction
125
- mkdir ($ patchDir , 0777 , true );
144
+ $ tplUseSchemaPatchInt = '%SchemaPatchInterface% ' ;
145
+ $ tplUseDataPatchInt = '%useDataPatchInterface% ' ;
146
+ $ valUseSchemaPatchInt = 'use Magento\Framework\Setup\Patch\SchemaPatchInterface; ' . "\n" ;
147
+ $ valUseDataPatchInt = 'use Magento\Framework\Setup\Patch\DataPatchInterface; ' . "\n" ;
148
+ if ($ type === 'schema ' ) {
149
+ $ patchTemplateData = str_replace ($ tplUseSchemaPatchInt , $ valUseSchemaPatchInt , $ patchTemplateData );
150
+ $ patchTemplateData = str_replace ($ tplUseDataPatchInt , '' , $ patchTemplateData );
151
+ } else {
152
+ $ patchTemplateData = str_replace ($ tplUseDataPatchInt , $ valUseDataPatchInt , $ patchTemplateData );
153
+ $ patchTemplateData = str_replace ($ tplUseSchemaPatchInt , '' , $ patchTemplateData );
126
154
}
127
- $ patchToFile = $ patchDir . '/ ' . $ patchName . '.php ' ;
128
- // phpcs:ignore Magento2.Functions.DiscouragedFunction
129
- file_put_contents ($ patchToFile , $ patchTemplateData );
155
+
156
+ $ tplUsePatchRevertInt = '%usePatchRevertableInterface% ' ;
157
+ $ tplImplementsInt = '%implementsInterfaces% ' ;
158
+ $ tplRevertFunction = '%revertFunction% ' ;
159
+ $ valUsePatchRevertInt = 'use Magento\Framework\Setup\Patch\PatchRevertableInterface; ' . "\n" ;
160
+
161
+ if ($ includeRevertMethod ) {
162
+ $ valImplementsInt = <<<BOF
163
+
164
+ $ patchInterface,
165
+ PatchRevertableInterface
166
+ BOF ;
167
+ $ patchTemplateData = str_replace ($ tplUsePatchRevertInt , $ valUsePatchRevertInt , $ patchTemplateData );
168
+ $ patchTemplateData = str_replace (' ' . $ tplImplementsInt , $ valImplementsInt , $ patchTemplateData );
169
+ $ patchTemplateData = str_replace ($ tplRevertFunction , $ this ->getRevertMethodTemplate (), $ patchTemplateData );
170
+ } else {
171
+ $ patchTemplateData = str_replace ($ tplUsePatchRevertInt , '' , $ patchTemplateData );
172
+ $ patchTemplateData = str_replace ($ tplImplementsInt , $ patchInterface , $ patchTemplateData );
173
+ $ patchTemplateData = str_replace ($ tplRevertFunction , '' , $ patchTemplateData );
174
+ }
175
+
176
+ $ patchDir = $ modulePath . '/Setup/Patch/ ' . $ preparedType ;
177
+ $ patchFile = $ patchName . '.php ' ;
178
+
179
+ $ fileWriter = $ this ->writeFactory ->create ($ patchDir );
180
+ $ fileWriter ->writeFile ($ patchFile , $ patchTemplateData );
181
+
182
+ $ outputPatchFile = str_replace ($ this ->directoryList ->getRoot () . '/ ' , '' , $ patchDir . '/ ' . $ patchFile );
183
+ $ output ->writeln (__ ('Patch %1 has been successfully generated. ' , $ outputPatchFile ));
184
+
130
185
return Cli::RETURN_SUCCESS ;
131
186
}
187
+
188
+ /**
189
+ * Returns patch template
190
+ *
191
+ * @return string
192
+ * @throws FileSystemException
193
+ */
194
+ private function getPatchTemplate (): string
195
+ {
196
+ $ read = $ this ->readFactory ->create (__DIR__ . '/ ' );
197
+ $ content = $ read ->readFile ('patch_template.php.dist ' );
198
+ return $ content ;
199
+ }
200
+
201
+ /**
202
+ * Returns template of revert() function
203
+ *
204
+ * @return string
205
+ * @throws FileSystemException
206
+ */
207
+ private function getRevertMethodTemplate (): string
208
+ {
209
+ $ read = $ this ->readFactory ->create (__DIR__ . '/ ' );
210
+ $ content = $ read ->readFile ('template_revert_function.php.dist ' );
211
+ return $ content ;
212
+ }
132
213
}
0 commit comments