Skip to content

Commit e8809e2

Browse files
Developed web api xml file generation, web api xml declaration generation, improved action development experience, added PhpPsiElementsUtil
1 parent 27fb74b commit e8809e2

16 files changed

+924
-0
lines changed

resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
<action id="OverrideInTheme.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideInThemeAction">
9797
<add-to-group group-id="ProjectViewPopupMenu"/>
9898
</action>
99+
<action id="MagentoCreateAWebApiDeclaration.Menu" class="com.magento.idea.magento2plugin.actions.generation.NewWebApiDeclarationAction">
100+
<add-to-group group-id="EditorPopupMenu"/>
101+
</action>
99102

100103
<action id="CopyMagentoPath"
101104
class="com.magento.idea.magento2plugin.actions.CopyMagentoPath"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<route url="/V1/${ROUTE}" method="${METHOD}">
2+
<service class="${SERVICE}" method="${SERVICE_METHOD}"/>
3+
<resources>
4+
<resource ref="${RESOURCE}"/>
5+
</resources>
6+
</route>

resources/fileTemplates/code/Magento Web Api Declaration.xml.html

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
4+
</routes>

resources/fileTemplates/internal/Magento Web Api XML.xml.html

Whitespace-only changes.

resources/magento2/validation.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ validator.onlyNumbers=The {0} field must contain numbers only
1111
validator.mustNotBeNegative={0} must not be negative
1212
validator.identifier=The {0} field must contain letters, numbers, dashes, and underscores only
1313
validator.identifier.colon=The {0} field must contain letters, numbers, colons, dashes, and underscores only
14+
validator.identifier.forwardSlash=The {0} field must contain letters, numbers, dashes, forward slashes and underscores only
1415
validator.class.isNotValid=The {0} field does not contain a valid class name
1516
validator.fqn.isNotValid=The {0} field does not contain a valid fully qualified class name
1617
validator.class.shouldBeUnique=Duplicated class {0}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.documentation.phpdoc.psi.PhpDocComment;
13+
import com.jetbrains.php.lang.psi.elements.Method;
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.NewWebApiDeclarationDialog;
17+
import com.magento.idea.magento2plugin.project.Settings;
18+
import com.magento.idea.magento2plugin.util.php.PhpPsiElementsUtil;
19+
import org.jetbrains.annotations.NotNull;
20+
21+
public class NewWebApiDeclarationAction extends AnAction {
22+
23+
public static final String ACTION_NAME = "Create a new WebApi declaration for this method";
24+
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 WebApi XML declaration";
25+
private Method currentPhpMethod;
26+
27+
/**
28+
* New WebApi declaration action constructor.
29+
*/
30+
public NewWebApiDeclarationAction() {
31+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
32+
}
33+
34+
@Override
35+
public void update(final @NotNull AnActionEvent event) {
36+
setIsAvailableForEvent(event, false);
37+
final Project project = event.getProject();
38+
final Method method = PhpPsiElementsUtil.getPhpMethod(event);
39+
40+
if (project == null || !Settings.isEnabled(project) || method == null) {
41+
return;
42+
}
43+
44+
if (method.getContainingClass() == null) {
45+
return;
46+
}
47+
48+
final PhpDocComment classDocComment = method.getContainingClass().getDocComment();
49+
50+
if (!method.getAccess().isPublic()
51+
|| method.getName().equals("__construct")
52+
|| classDocComment == null
53+
|| !classDocComment.getText().contains("@api")) {
54+
return;
55+
}
56+
57+
currentPhpMethod = method;
58+
setIsAvailableForEvent(event, true);
59+
}
60+
61+
@Override
62+
public void actionPerformed(final @NotNull AnActionEvent event) {
63+
final PsiDirectory directory =
64+
currentPhpMethod.getContainingFile().getContainingDirectory();
65+
66+
if (event.getProject() == null || directory == null) {
67+
return;
68+
}
69+
70+
final PhpClass phpClass = currentPhpMethod.getContainingClass();
71+
72+
if (phpClass == null) {
73+
return;
74+
}
75+
76+
final String classFqn = phpClass.getPresentableFQN();
77+
final String methodName = currentPhpMethod.getName();
78+
79+
NewWebApiDeclarationDialog.open(event.getProject(), directory, classFqn, methodName);
80+
}
81+
82+
/**
83+
* Set is action available for event.
84+
*
85+
* @param event AnActionEvent
86+
* @param isAvailable boolean
87+
*/
88+
private void setIsAvailableForEvent(
89+
final @NotNull AnActionEvent event,
90+
final boolean isAvailable
91+
) {
92+
event.getPresentation().setVisible(isAvailable);
93+
event.getPresentation().setEnabled(isAvailable);
94+
}
95+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 org.jetbrains.annotations.NotNull;
9+
10+
public class WebApiXmlRouteData {
11+
12+
private final String moduleName;
13+
private final String url;
14+
private final String httpMethod;
15+
private final String serviceClass;
16+
private final String serviceMethod;
17+
private final String aclResource;
18+
19+
/**
20+
* Web Api xml declaration DTO constructor.
21+
*
22+
* @param moduleName String
23+
* @param url String
24+
* @param httpMethod String
25+
* @param serviceClass String
26+
* @param serviceMethod String
27+
* @param aclResource String
28+
*/
29+
public WebApiXmlRouteData(
30+
final @NotNull String moduleName,
31+
final @NotNull String url,
32+
final @NotNull String httpMethod,
33+
final @NotNull String serviceClass,
34+
final @NotNull String serviceMethod,
35+
final @NotNull String aclResource
36+
) {
37+
this.moduleName = moduleName;
38+
this.url = url;
39+
this.httpMethod = httpMethod;
40+
this.serviceClass = serviceClass;
41+
this.serviceMethod = serviceMethod;
42+
this.aclResource = aclResource;
43+
}
44+
45+
/**
46+
* Get module name.
47+
*
48+
* @return String
49+
*/
50+
public String getModuleName() {
51+
return moduleName;
52+
}
53+
54+
/**
55+
* Get service url.
56+
*
57+
* @return String
58+
*/
59+
public String getUrl() {
60+
return url;
61+
}
62+
63+
/**
64+
* Get HTTP method.
65+
*
66+
* @return String
67+
*/
68+
public String getHttpMethod() {
69+
return httpMethod;
70+
}
71+
72+
/**
73+
* Get service class.
74+
*
75+
* @return String
76+
*/
77+
public String getServiceClass() {
78+
return serviceClass;
79+
}
80+
81+
/**
82+
* Get service method.
83+
*
84+
* @return String
85+
*/
86+
public String getServiceMethod() {
87+
return serviceMethod;
88+
}
89+
90+
/**
91+
* Get ACL resource id.
92+
*
93+
* @return String
94+
*/
95+
public String getAclResource() {
96+
return aclResource;
97+
}
98+
}

0 commit comments

Comments
 (0)