Skip to content

Commit c399bd5

Browse files
committed
1139: Creating a new data patch template
1 parent 0be7719 commit c399bd5

File tree

11 files changed

+753
-0
lines changed

11 files changed

+753
-0
lines changed

resources/META-INF/plugin.xml

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<action id="MagentoCreateWidgetFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewWidgetXmlAction"/>
7979
<action id="MagentoCreateLayoutFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewLayoutXmlAction"/>
8080
<action id="MagentoCreateReadmeFile" class="com.magento.idea.magento2plugin.actions.context.md.NewReadmeMdAction"/>
81+
<action id="MagentoDataPatchFile" class="com.magento.idea.magento2plugin.actions.context.php.NewSetupDataPatchAction"/>
8182
<!-- Context dependent actions -->
8283
<separator/>
8384
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewXml"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
4+
namespace ${MODULE_NAME}\Setup\Patch\Data;
5+
6+
use Magento\Framework\Setup\ModuleDataSetupInterface;
7+
use Magento\Framework\Setup\Patch\DataPatchInterface;
8+
9+
/**
10+
* Patch is mechanism, that allows to do atomic upgrade data changes
11+
*/
12+
class ${CLASS_NAME} implements DataPatchInterface
13+
{
14+
/**
15+
* @var ModuleDataSetupInterface
16+
*/
17+
private $moduleDataSetup;
18+
19+
/**
20+
* @param ModuleDataSetupInterface $moduleDataSetup
21+
*/
22+
public function __construct(
23+
ModuleDataSetupInterface $moduleDataSetup
24+
) {
25+
$this->moduleDataSetup = $moduleDataSetup;
26+
}
27+
28+
/**
29+
* Do Upgrade
30+
*
31+
* @return void
32+
*/
33+
public function apply()
34+
{
35+
$this->moduleDataSetup->getConnection()->startSetup();
36+
37+
// TODO: The code that you want apply in the patch
38+
39+
$this->moduleDataSetup->getConnection()->endSetup();
40+
}
41+
42+
/**
43+
* Get aliases (previous names) for the patch.
44+
*
45+
* @return string[]
46+
*/
47+
public function getAliases()
48+
{
49+
return [];
50+
}
51+
52+
/**
53+
* Get array of patches that have to be executed prior to this.
54+
*
55+
* Example of implementation:
56+
*
57+
* [
58+
* \Vendor_Name\Module_Name\Setup\Patch\Patch1::class,
59+
* \Vendor_Name\Module_Name\Setup\Patch\Patch2::class
60+
* ]
61+
*
62+
* @return string[]
63+
*/
64+
public static function getDependencies()
65+
{
66+
return [];
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!--
2+
/*
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
10+
<tr>
11+
<td><font face="verdana" size="-1">
12+
A data patch is a class that contains data modification instructions.
13+
</font><br>
14+
</td>
15+
</tr>
16+
<tr>
17+
<td><font face="verdana" size="-1">
18+
Read more About the data and schema patches in the
19+
<a href="https://devdocs.magento.com/guides/v2.4/extension-dev-guide/declarative-schema/data-patches.html">
20+
DevDocs</a>.
21+
</font><br>
22+
</td>
23+
</tr>
24+
</table>
25+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
26+
<tr>
27+
<td colspan="3"><font face="verdana" size="-1">Predefined variables explanation:</font></td>
28+
</tr>
29+
<tr>
30+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${CLASS_NAME}</b></font></nobr></td>
31+
<td width="10">&nbsp;</td>
32+
<td width="100%" valign="top"><font face="verdana" size="-1">Specifies the name of your class
33+
</font>
34+
</td>
35+
</tr>
36+
</table>
37+
</body>
38+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.context.php;
7+
8+
import com.intellij.openapi.actionSystem.AnActionEvent;
9+
import com.intellij.psi.PsiDirectory;
10+
import com.intellij.psi.PsiFile;
11+
import com.magento.idea.magento2plugin.actions.context.CustomGeneratorContextAction;
12+
import com.magento.idea.magento2plugin.actions.generation.dialog.NewSetupDataPatch.NewSetupDataPatchDialog;
13+
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
14+
import com.magento.idea.magento2plugin.magento.packages.Package;
15+
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
16+
import org.jetbrains.annotations.NotNull;
17+
18+
public class NewSetupDataPatchAction extends CustomGeneratorContextAction {
19+
20+
public static final String ACTION_NAME = "Magento 2 Setup Data Patch";
21+
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Setup Data Patch";
22+
public static final String ROOT_DIRECTORY = "Setup";
23+
public static final String PATCH_DIRECTORY = "Patch";
24+
public static final String DATA_DIRECTORY = "Data";
25+
26+
public NewSetupDataPatchAction() {
27+
super(ACTION_NAME, ACTION_DESCRIPTION);
28+
}
29+
30+
@Override
31+
public void actionPerformed(final @NotNull AnActionEvent event) {
32+
final GetMagentoModuleUtil.MagentoModuleData moduleData = getModuleData();
33+
34+
if (event.getProject() == null || moduleData == null || getDirectory() == null) {
35+
return;
36+
}
37+
final String[] templateData = moduleData.getName().split(Package.vendorModuleNameSeparator);
38+
39+
if (templateData.length != 2) { //NOPMD
40+
return;
41+
}
42+
43+
NewSetupDataPatchDialog.open(
44+
event.getProject(),
45+
getDirectory(),
46+
templateData[0],
47+
templateData[1]
48+
);
49+
}
50+
51+
@Override
52+
protected boolean isVisible(
53+
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
54+
final PsiDirectory targetDirectory,
55+
final PsiFile targetFile
56+
) {
57+
if (!moduleData.getType().equals(ComponentType.module)) {
58+
return false;
59+
}
60+
final PsiDirectory parentDirFirst = targetDirectory.getParentDirectory();
61+
final PsiDirectory parentDirSecond = parentDirFirst != null ?
62+
parentDirFirst.getParentDirectory() : null;
63+
64+
return targetDirectory.getName().equals(ROOT_DIRECTORY) ||
65+
(parentDirFirst != null && parentDirFirst.getName().equals(ROOT_DIRECTORY)) ||
66+
(parentDirSecond != null && parentDirSecond.getName().equals(ROOT_DIRECTORY));
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation;
7+
8+
import com.intellij.psi.PsiDirectory;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
public class ModuleSetupDataPatchData {
12+
13+
private final String packageName;
14+
private final String moduleName;
15+
private final PsiDirectory baseDir;
16+
private final String className;
17+
18+
/**
19+
* Constructor.
20+
*
21+
* @param packageName String
22+
* @param moduleName String
23+
* @param baseDir PsiDirectory
24+
*/
25+
public ModuleSetupDataPatchData(
26+
final @NotNull String packageName,
27+
final @NotNull String moduleName,
28+
final @NotNull PsiDirectory baseDir,
29+
final @NotNull String className
30+
) {
31+
this.packageName = packageName;
32+
this.moduleName = moduleName;
33+
this.baseDir = baseDir;
34+
this.className = className;
35+
}
36+
37+
public @NotNull String getPackageName() {
38+
return packageName;
39+
}
40+
41+
public @NotNull String getModuleName() {
42+
return moduleName;
43+
}
44+
45+
public @NotNull PsiDirectory getBaseDir() {
46+
return baseDir;
47+
}
48+
49+
public @NotNull String getClassName() {
50+
return className;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.NewSetupDataPatch.NewSetupDataPatchDialog">
3+
<grid id="1871d" binding="contentPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4+
<margin top="10" left="10" bottom="10" right="10"/>
5+
<constraints>
6+
<xy x="63" y="217" width="405" height="143"/>
7+
</constraints>
8+
<properties>
9+
<opaque value="true"/>
10+
<preferredSize width="440" height="150"/>
11+
<requestFocusEnabled value="true"/>
12+
</properties>
13+
<border type="none"/>
14+
<children>
15+
<grid id="9ad5" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
16+
<margin top="0" left="0" bottom="0" right="0"/>
17+
<constraints>
18+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
19+
</constraints>
20+
<properties/>
21+
<border type="none"/>
22+
<children>
23+
<hspacer id="4aa6d">
24+
<constraints>
25+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
26+
</constraints>
27+
</hspacer>
28+
<grid id="f892c" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
29+
<margin top="0" left="0" bottom="0" right="0"/>
30+
<constraints>
31+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
32+
</constraints>
33+
<properties/>
34+
<border type="none"/>
35+
<children>
36+
<component id="1b860" class="javax.swing.JButton" binding="buttonCancel">
37+
<constraints>
38+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
39+
</constraints>
40+
<properties>
41+
<text resource-bundle="magento2/common" key="common.cancel"/>
42+
</properties>
43+
</component>
44+
<component id="20801" class="javax.swing.JButton" binding="buttonOK">
45+
<constraints>
46+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
47+
</constraints>
48+
<properties>
49+
<text resource-bundle="magento2/common" key="common.ok"/>
50+
</properties>
51+
</component>
52+
</children>
53+
</grid>
54+
</children>
55+
</grid>
56+
<grid id="ad0b1" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
57+
<margin top="0" left="0" bottom="0" right="0"/>
58+
<constraints>
59+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
60+
</constraints>
61+
<properties/>
62+
<border type="none"/>
63+
<children>
64+
<component id="d30e2" class="javax.swing.JLabel" binding="classNameErrorMessage">
65+
<constraints>
66+
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
67+
</constraints>
68+
<properties>
69+
<text value=""/>
70+
</properties>
71+
</component>
72+
<component id="662ad" class="javax.swing.JLabel" binding="classNameLabel">
73+
<constraints>
74+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
75+
<preferred-size width="113" height="16"/>
76+
</grid>
77+
</constraints>
78+
<properties>
79+
<text value="Class Name"/>
80+
</properties>
81+
</component>
82+
<component id="292c" class="javax.swing.JTextField" binding="className">
83+
<constraints>
84+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
85+
<preferred-size width="150" height="-1"/>
86+
</grid>
87+
</constraints>
88+
<properties/>
89+
</component>
90+
</children>
91+
</grid>
92+
</children>
93+
</grid>
94+
</form>

0 commit comments

Comments
 (0)