Skip to content

Commit d5282e3

Browse files
author
Vitaliy
authored
Merge pull request #548 from bohdan-harniuk/entity-web-api-generation
Developed web api xml file generation, web api xml declaration generation, improved action development experience, added PhpPsiElementsUtil
2 parents 5852f9a + 050a295 commit d5282e3

File tree

23 files changed

+1061
-22
lines changed

23 files changed

+1061
-22
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: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.PhpClass;
14+
import com.magento.idea.magento2plugin.MagentoIcons;
15+
import com.magento.idea.magento2plugin.actions.generation.dialog.NewWebApiDeclarationDialog;
16+
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
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 Web API declaration for this method";
24+
public static final String ACTION_DESCRIPTION =
25+
"Create a new Magento 2 Web API XML declaration";
26+
private Method currentPhpMethod;
27+
28+
/**
29+
* New WebApi declaration action constructor.
30+
*/
31+
public NewWebApiDeclarationAction() {
32+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
33+
}
34+
35+
@Override
36+
public void update(final @NotNull AnActionEvent event) {
37+
setIsAvailableForEvent(event, false);
38+
final Project project = event.getProject();
39+
final Method method = PhpPsiElementsUtil.getPhpMethod(event);
40+
41+
if (project == null || !Settings.isEnabled(project) || method == null) {
42+
return;
43+
}
44+
45+
if (method.getContainingClass() == null) {
46+
return;
47+
}
48+
49+
if (!method.getAccess().isPublic()
50+
|| method.getName().equals(MagentoPhpClass.CONSTRUCT_METHOD_NAME)) {
51+
return;
52+
}
53+
54+
currentPhpMethod = method;
55+
setIsAvailableForEvent(event, true);
56+
}
57+
58+
@Override
59+
public void actionPerformed(final @NotNull AnActionEvent event) {
60+
final PsiDirectory directory =
61+
currentPhpMethod.getContainingFile().getContainingDirectory();
62+
63+
if (event.getProject() == null || directory == null) {
64+
return;
65+
}
66+
67+
final PhpClass phpClass = currentPhpMethod.getContainingClass();
68+
69+
if (phpClass == null) {
70+
return;
71+
}
72+
73+
final String classFqn = phpClass.getPresentableFQN();
74+
final String methodName = currentPhpMethod.getName();
75+
76+
NewWebApiDeclarationDialog.open(event.getProject(), directory, classFqn, methodName);
77+
}
78+
79+
/**
80+
* Set is action available for event.
81+
*
82+
* @param event AnActionEvent
83+
* @param isAvailable boolean
84+
*/
85+
private void setIsAvailableForEvent(
86+
final @NotNull AnActionEvent event,
87+
final boolean isAvailable
88+
) {
89+
event.getPresentation().setVisible(isAvailable);
90+
event.getPresentation().setEnabled(isAvailable);
91+
}
92+
}
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)