Skip to content

Commit 1a495ef

Browse files
Merge pull request #981 from magento/plugin-for-the-parent-class-methods
Added the possibility to create a plugin for a method in the parent class
2 parents b2374d6 + a77a478 commit 1a495ef

File tree

4 files changed

+77
-11
lines changed

4 files changed

+77
-11
lines changed

resources/magento2/common.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@ common.template.type=Email Type
7272
common.diagnostic.reportButtonText=Report Me
7373
common.diagnostic.reportSubmittedTitle=The report is successfully submitted!
7474
common.diagnostic.reportSubmittedMessage=Thank you for submitting your report! We will check it as soon as possible.
75+
common.targetMethod=Target Method

src/com/magento/idea/magento2plugin/actions/generation/CreateAPluginAction.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
public class CreateAPluginAction extends DumbAwareAction {
2828

29-
public static final String ACTION_NAME = "Create a new Plugin for this method";
29+
public static final String ACTION_NAME = "Create a new Plugin";
3030
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Plugin";
3131
private final GetFirstClassOfFile getFirstClassOfFile;
3232
private Method targetMethod;
@@ -58,7 +58,6 @@ public void update(final AnActionEvent event) {
5858
|| !(psiFile instanceof PhpFile)
5959
|| phpClass.isFinal()
6060
|| PhpClassImplementsNoninterceptableInterfaceUtil.execute(phpClass)
61-
|| this.targetMethod == null
6261
) {
6362
this.setStatus(event, false);
6463
return;
@@ -103,6 +102,7 @@ private Pair<PsiFile, PhpClass> findPhpClass(final @NotNull AnActionEvent event)
103102
return Pair.create(psiFile, phpClass);
104103
}
105104

105+
@SuppressWarnings({"PMD.CyclomaticComplexity"})
106106
private void fetchTargetMethod(
107107
final @NotNull AnActionEvent event,
108108
final PsiFile psiFile,
@@ -125,6 +125,11 @@ private void fetchTargetMethod(
125125
this.targetMethod = (Method) element;
126126
return;
127127
}
128+
129+
if (element instanceof PhpClass) {
130+
return;
131+
}
132+
128133
final PsiElement parent = element.getParent();
129134

130135
if (parent instanceof Method && parent.getParent().equals(phpClass)

src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.form

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.CreateAPluginDialog">
3-
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="8" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="10" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>
66
<xy x="48" y="54" width="600" height="500"/>
77
</constraints>
88
<properties>
9-
<minimumSize width="400" height="350"/>
10-
<preferredSize width="400" height="350"/>
119
<requestFocusEnabled value="true"/>
1210
</properties>
1311
<border type="none"/>
1412
<children>
1513
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
1614
<margin top="0" left="0" bottom="0" right="0"/>
1715
<constraints>
18-
<grid row="7" column="0" row-span="1" col-span="3" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
16+
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
1917
</constraints>
2018
<properties/>
2119
<border type="none"/>
@@ -56,7 +54,7 @@
5654
<grid id="e3588" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
5755
<margin top="0" left="0" bottom="0" right="0"/>
5856
<constraints>
59-
<grid row="6" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
57+
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
6058
</constraints>
6159
<properties/>
6260
<border type="none"/>
@@ -149,7 +147,7 @@
149147
</grid>
150148
<component id="5196a" class="javax.swing.JLabel" binding="selectPluginModule">
151149
<constraints>
152-
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
150+
<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"/>
153151
</constraints>
154152
<properties>
155153
<labelFor value="bd654"/>
@@ -158,7 +156,7 @@
158156
</component>
159157
<component id="b0d15" class="javax.swing.JComboBox" binding="pluginType">
160158
<constraints>
161-
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
159+
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
162160
</constraints>
163161
<properties/>
164162
<clientProperties>
@@ -167,7 +165,7 @@
167165
</component>
168166
<component id="d3b42" class="javax.swing.JLabel" binding="pluginTypeLabel">
169167
<constraints>
170-
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
168+
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
171169
</constraints>
172170
<properties>
173171
<text resource-bundle="magento2/common" key="common.methodType"/>
@@ -193,6 +191,20 @@
193191
<text resource-bundle="magento2/common" key="common.area.target"/>
194192
</properties>
195193
</component>
194+
<component id="d0a79" class="javax.swing.JLabel" binding="targetMethodLabel">
195+
<constraints>
196+
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
197+
</constraints>
198+
<properties>
199+
<text resource-bundle="magento2/common" key="common.targetMethod"/>
200+
</properties>
201+
</component>
202+
<component id="678c2" class="com.magento.idea.magento2plugin.ui.FilteredComboBox" binding="targetMethodSelect" custom-create="true">
203+
<constraints>
204+
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
205+
</constraints>
206+
<properties/>
207+
</component>
196208
</children>
197209
</grid>
198210
</form>

src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import com.magento.idea.magento2plugin.magento.packages.File;
2828
import com.magento.idea.magento2plugin.magento.packages.Package;
2929
import com.magento.idea.magento2plugin.ui.FilteredComboBox;
30+
import com.magento.idea.magento2plugin.util.php.PhpTypeMetadataParserUtil;
3031
import java.awt.event.ActionEvent;
3132
import java.awt.event.KeyEvent;
3233
import java.awt.event.WindowAdapter;
3334
import java.awt.event.WindowEvent;
35+
import java.util.ArrayList;
3436
import java.util.List;
3537
import javax.swing.JButton;
3638
import javax.swing.JComboBox;
@@ -50,7 +52,7 @@
5052
public class CreateAPluginDialog extends AbstractDialog {
5153
@NotNull
5254
private final Project project;
53-
private final Method targetMethod;
55+
private Method targetMethod;
5456
private final PhpClass targetClass;
5557
private JPanel contentPane;
5658
private JButton buttonOK;
@@ -63,13 +65,20 @@ public class CreateAPluginDialog extends AbstractDialog {
6365
private static final String SORT_ORDER = "sort order";
6466
private static final String PLUGIN_NAME = "plugin name";
6567
private static final String TARGET_MODULE = "target module";
68+
private static final String TARGET_METHOD = "target method";
6669

6770
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
6871
message = {NotEmptyRule.MESSAGE, TARGET_MODULE})
6972
@FieldValidation(rule = RuleRegistry.BOX_NOT_EMPTY,
7073
message = {BoxNotEmptyRule.MESSAGE, TARGET_MODULE})
7174
private FilteredComboBox pluginModule;
7275

76+
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
77+
message = {NotEmptyRule.MESSAGE, TARGET_METHOD})
78+
@FieldValidation(rule = RuleRegistry.BOX_NOT_EMPTY,
79+
message = {BoxNotEmptyRule.MESSAGE, TARGET_METHOD})
80+
private FilteredComboBox targetMethodSelect;
81+
7382
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
7483
message = {NotEmptyRule.MESSAGE, CLASS_NAME})
7584
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
@@ -99,6 +108,7 @@ public class CreateAPluginDialog extends AbstractDialog {
99108
private JLabel pluginNameLabel;//NOPMD
100109
private JLabel pluginClassNameLabel;//NOPMD
101110
private JLabel pluginSortOrderLabel;//NOPMD
111+
private JLabel targetMethodLabel;
102112

103113
/**
104114
* Constructor.
@@ -124,6 +134,10 @@ public CreateAPluginDialog(
124134
fillPluginTypeOptions();
125135
fillTargetAreaOptions();
126136

137+
if (targetMethod != null) {
138+
this.targetMethodLabel.setVisible(false);
139+
}
140+
127141
buttonOK.addActionListener((final ActionEvent event) -> onOK());
128142
buttonCancel.addActionListener((final ActionEvent event) -> onCancel());
129143

@@ -157,6 +171,9 @@ private void fillTargetAreaOptions() {
157171
}
158172

159173
protected void onOK() {
174+
if (targetMethod == null) {
175+
targetMethod = getSelectedTargetMethod();
176+
}
160177
if (validateFormFields()) {
161178
new PluginClassGenerator(new PluginFileData(
162179
getPluginDirectory(),
@@ -209,6 +226,25 @@ public String getPluginModule() {
209226
return this.pluginModule.getSelectedItem().toString();
210227
}
211228

229+
/**
230+
* Searches and returns a selected target method.
231+
*
232+
* @return Method target method
233+
*/
234+
public Method getSelectedTargetMethod() {
235+
final String selectedMethodString = this.targetMethodSelect.getSelectedItem().toString();
236+
final List<Method> publicMethods = PhpTypeMetadataParserUtil.getPublicMethods(
237+
this.targetClass
238+
);
239+
for (final Method method: publicMethods) {
240+
if (method.getName().equals(selectedMethodString)) {
241+
return method;
242+
}
243+
}
244+
245+
return null;
246+
}
247+
212248
/**
213249
* Open an action dialog.
214250
*
@@ -236,6 +272,18 @@ private void createUIComponents() {
236272
.getEditableModuleNames();
237273

238274
this.pluginModule = new FilteredComboBox(allModulesList);
275+
276+
final List<Method> publicMethods
277+
= PhpTypeMetadataParserUtil.getPublicMethods(this.targetClass);
278+
final List<String> methodList = new ArrayList<>();
279+
for (final Method method: publicMethods) {
280+
methodList.add(method.getName());
281+
}
282+
283+
this.targetMethodSelect = new FilteredComboBox(methodList);
284+
if (targetMethod != null) {
285+
this.targetMethodSelect.setVisible(false);
286+
}
239287
}
240288

241289
private String getNamespace() {

0 commit comments

Comments
 (0)