77
88namespace Magento \Developer \Console \Command ;
99
10- use Magento \Developer \ Model \ Di \ Information ;
10+ use Magento \Framework \ App \ ObjectManager ;
1111use Magento \Framework \Component \ComponentRegistrar ;
1212use 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 ;
1317use Symfony \Component \Console \Command \Command ;
14- use Symfony \Component \Console \Exception \ InvalidArgumentException ;
18+ use Symfony \Component \Console \Input \ InputArgument ;
1519use Symfony \Component \Console \Input \InputInterface ;
1620use Symfony \Component \Console \Input \InputOption ;
1721use Symfony \Component \Console \Output \OutputInterface ;
18- use Symfony \Component \Console \Input \InputArgument ;
19- use Symfony \Component \Console \Helper \Table ;
2022
2123/**
2224 * Allows to generate setup patches
@@ -37,20 +39,45 @@ class GeneratePatchCommand extends Command
3739 */
3840 private $ componentRegistrar ;
3941
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+
4057 /**
4158 * GeneratePatchCommand constructor.
59+ *
4260 * @param ComponentRegistrar $componentRegistrar
61+ * @param DirectoryList|null $directoryList
62+ * @param ReadFactory|null $readFactory
63+ * @param WriteFactory|null $writeFactory
4364 */
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+ ) {
4671 $ 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+
4776 parent ::__construct ();
4877 }
4978
5079 /**
51- * @inheritdoc
52- *
53- * @throws InvalidArgumentException
80+ * Configures the current command.
5481 */
5582 protected function configure ()
5683 {
@@ -89,24 +116,21 @@ protected function configure()
89116 }
90117
91118 /**
92- * Patch template
119+ * Execute command
93120 *
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
105125 */
106126 protected function execute (InputInterface $ input , OutputInterface $ output ): int
107127 {
108128 $ moduleName = $ input ->getArgument (self ::MODULE_NAME );
109129 $ patchName = $ input ->getArgument (self ::INPUT_KEY_PATCH_NAME );
130+ $ includeRevertMethod = false ;
131+ if ($ input ->getOption (self ::INPUT_KEY_IS_REVERTABLE )) {
132+ $ includeRevertMethod = true ;
133+ }
110134 $ type = $ input ->getOption (self ::INPUT_KEY_PATCH_TYPE );
111135 $ modulePath = $ this ->componentRegistrar ->getPath (ComponentRegistrar::MODULE , $ moduleName );
112136 $ preparedModuleName = str_replace ('_ ' , '\\' , $ moduleName );
@@ -115,18 +139,75 @@ protected function execute(InputInterface $input, OutputInterface $output): int
115139 $ patchTemplateData = $ this ->getPatchTemplate ();
116140 $ patchTemplateData = str_replace ('%moduleName% ' , $ preparedModuleName , $ patchTemplateData );
117141 $ patchTemplateData = str_replace ('%patchType% ' , $ preparedType , $ patchTemplateData );
118- $ patchTemplateData = str_replace ('%patchInterface% ' , $ patchInterface , $ patchTemplateData );
119142 $ patchTemplateData = str_replace ('%class% ' , $ patchName , $ patchTemplateData );
120- $ patchDir = $ patchToFile = $ modulePath . '/Setup/Patch/ ' . $ preparedType ;
121143
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 );
126154 }
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+
130185 return Cli::RETURN_SUCCESS ;
131186 }
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+ }
132213}
0 commit comments