Skip to content

Commit 11dbae8

Browse files
409: Added arguments injection/replacement action
1 parent c7c3d48 commit 11dbae8

18 files changed

+2525
-3
lines changed

resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@
126126
<action id="MagentoCreateAWebApiInterfaceForService.Menu" class="com.magento.idea.magento2plugin.actions.generation.NewWebApiInterfaceAction">
127127
<add-to-group group-id="EditorPopupMenu"/>
128128
</action>
129+
<action id="MagentoInjectConstructorArgumentAction.Menu" class="com.magento.idea.magento2plugin.actions.generation.InjectConstructorArgumentAction">
130+
<add-to-group group-id="EditorPopupMenu"/>
131+
</action>
129132

130133
<action id="CopyMagentoPath"
131134
class="com.magento.idea.magento2plugin.actions.CopyMagentoPath"

resources/magento2/validation.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ validator.alphaAndDashCharacters=The {0} field must contain alphabets and dashes
99
validator.alreadyDeclared={0} is already declared in the {1} module
1010
validator.startWithNumberOrCapitalLetter=The {0} field must start with a number or a capital letter
1111
validator.onlyNumbers=The {0} field must contain numbers only
12+
validator.onlyIntegerOrFloatNumbers=The {0} field must contain only integer or float numbers
1213
validator.mustNotBeNegative={0} must not be negative
1314
validator.identifier=The {0} field must contain letters, numbers, dashes, and underscores only
1415
validator.identifier.colon=The {0} field must contain letters, numbers, colons, dashes, and underscores only
@@ -41,3 +42,6 @@ validator.lowerSnakeCase=The {0} field must be of the lower snake case format
4142
validator.menuIdentifierInvalid=The menu identifier is invalid
4243
validator.someFieldsHaveErrors=Please, check the dialog. Some fields have errors
4344
validator.dbSchema.invalidColumnType=Invalid ''{0}'' column type specified
45+
validator.arrayValuesDialog.invalidValueForRowWithName=Invalid value ''{0}'' specified for the row with name ''{1}''
46+
validator.arrayValuesDialog.namesMustBeUnique=Duplicated items names
47+
validator.arrayValuesDialog.nameMustNotBeEmpty=The array name cannot be empty
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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.openapi.actionSystem.AnAction;
9+
import com.intellij.openapi.actionSystem.AnActionEvent;
10+
import com.intellij.openapi.project.Project;
11+
import com.intellij.psi.PsiDirectory;
12+
import com.jetbrains.php.lang.psi.elements.Method;
13+
import com.jetbrains.php.lang.psi.elements.Parameter;
14+
import com.jetbrains.php.lang.psi.elements.PhpClass;
15+
import com.magento.idea.magento2plugin.MagentoIcons;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.NewArgumentInjectionDialog;
17+
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
18+
import com.magento.idea.magento2plugin.project.Settings;
19+
import com.magento.idea.magento2plugin.util.RegExUtil;
20+
import com.magento.idea.magento2plugin.util.php.PhpPsiElementsUtil;
21+
import org.jetbrains.annotations.NotNull;
22+
23+
public class InjectConstructorArgumentAction extends AnAction {
24+
25+
public static final String ACTION_NAME = "Inject argument";
26+
public static final String ACTION_DESCRIPTION = "Inject argument through the DI";
27+
public static final String GATHER_ARRAY_VALUES_ACTION_DESCRIPTION = "Specify array values";
28+
private PhpClass currentPhpClass;
29+
private Parameter currentParameter;
30+
31+
/**
32+
* Inject constructor argument action constructor.
33+
*/
34+
public InjectConstructorArgumentAction() {
35+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
36+
}
37+
38+
@Override
39+
public void update(final @NotNull AnActionEvent event) {
40+
setIsAvailableForEvent(event, false);
41+
final Project project = event.getProject();
42+
43+
if (project == null || !Settings.isEnabled(project)) {
44+
return;
45+
}
46+
final PhpClass phpClass = PhpPsiElementsUtil.getPhpClass(event);
47+
48+
if (phpClass == null) {
49+
return;
50+
}
51+
// Excluding argument injection generators for Test/ and *Test.php files
52+
// in order to not overload the context menu.
53+
final String filename = phpClass.getContainingFile().getName();
54+
55+
if (filename.matches(RegExUtil.Magento.TEST_FILE_NAME)
56+
|| phpClass.getPresentableFQN().matches(RegExUtil.Magento.TEST_CLASS_FQN)) {
57+
return;
58+
}
59+
final Parameter parameter = PhpPsiElementsUtil.getMethodArgument(event);
60+
61+
if (parameter == null) {
62+
return;
63+
}
64+
final Method method = parameter.getParent().getParent() instanceof Method
65+
? (Method) parameter.getParent().getParent() : null;
66+
67+
if (method == null) {
68+
return;
69+
}
70+
71+
if (!method.getAccess().isPublic()
72+
|| !method.getName().equals(MagentoPhpClass.CONSTRUCT_METHOD_NAME)) {
73+
return;
74+
}
75+
currentPhpClass = phpClass;
76+
currentParameter = parameter;
77+
setIsAvailableForEvent(event, true);
78+
}
79+
80+
@Override
81+
public void actionPerformed(final @NotNull AnActionEvent event) {
82+
final PsiDirectory directory =
83+
currentPhpClass.getContainingFile().getContainingDirectory();
84+
85+
if (event.getProject() == null
86+
|| currentPhpClass == null
87+
|| directory == null
88+
|| currentParameter == null) {
89+
return;
90+
}
91+
92+
NewArgumentInjectionDialog.open(
93+
event.getProject(),
94+
directory,
95+
currentPhpClass,
96+
currentParameter
97+
);
98+
}
99+
100+
/**
101+
* Set is action available for event.
102+
*
103+
* @param event AnActionEvent
104+
* @param isAvailable boolean
105+
*/
106+
private void setIsAvailableForEvent(
107+
final @NotNull AnActionEvent event,
108+
final boolean isAvailable
109+
) {
110+
event.getPresentation().setVisible(isAvailable);
111+
event.getPresentation().setEnabled(isAvailable);
112+
}
113+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.data.xml;
7+
8+
import com.magento.idea.magento2plugin.magento.packages.Areas;
9+
import com.magento.idea.magento2plugin.magento.packages.DiArgumentType;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
public class DiArgumentData {
13+
14+
private final String moduleName;
15+
private final String clazz;
16+
private final String parameter;
17+
private final Areas area;
18+
private final DiArgumentType valueType;
19+
private final String value;
20+
21+
/**
22+
* DI argument DTO constructor.
23+
*
24+
* @param moduleName String
25+
* @param clazz String
26+
* @param parameter String
27+
* @param area Areas
28+
* @param valueType DiArgumentType
29+
* @param value String
30+
*/
31+
public DiArgumentData(
32+
final @NotNull String moduleName,
33+
final @NotNull String clazz,
34+
final @NotNull String parameter,
35+
final @NotNull Areas area,
36+
final @NotNull DiArgumentType valueType,
37+
final @NotNull String value
38+
) {
39+
this.moduleName = moduleName;
40+
this.clazz = clazz;
41+
this.parameter = parameter;
42+
this.area = area;
43+
this.valueType = valueType;
44+
this.value = value;
45+
}
46+
47+
/**
48+
* Get module name.
49+
*
50+
* @return String
51+
*/
52+
public String getModuleName() {
53+
return moduleName;
54+
}
55+
56+
/**
57+
* Get target class.
58+
*
59+
* @return String
60+
*/
61+
public String getClazz() {
62+
return clazz;
63+
}
64+
65+
/**
66+
* Get target parameter.
67+
*
68+
* @return String
69+
*/
70+
public String getParameter() {
71+
return parameter;
72+
}
73+
74+
/**
75+
* Get target area.
76+
*
77+
* @return Areas
78+
*/
79+
public Areas getArea() {
80+
return area;
81+
}
82+
83+
/**
84+
* Get argument value xsi:type.
85+
*
86+
* @return DiArgumentType
87+
*/
88+
public DiArgumentType getValueType() {
89+
return valueType;
90+
}
91+
92+
/**
93+
* Get argument value.
94+
*
95+
* @return String
96+
*/
97+
public String getValue() {
98+
return value;
99+
}
100+
}

0 commit comments

Comments
 (0)