Skip to content

Commit e75cf41

Browse files
Added Web Api interface generation for a service
1 parent c267602 commit e75cf41

File tree

16 files changed

+1676
-2
lines changed

16 files changed

+1676
-2
lines changed

resources/META-INF/plugin.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
<action id="MagentoCreateAWebApiDeclaration.Menu" class="com.magento.idea.magento2plugin.actions.generation.NewWebApiDeclarationAction">
100100
<add-to-group group-id="EditorPopupMenu"/>
101101
</action>
102+
<action id="MagentoCreateAWebApiInterfaceForService.Menu" class="com.magento.idea.magento2plugin.actions.generation.NewWebApiInterfaceAction">
103+
<add-to-group group-id="EditorPopupMenu"/>
104+
</action>
102105

103106
<action id="CopyMagentoPath"
104107
class="com.magento.idea.magento2plugin.actions.CopyMagentoPath"
@@ -252,6 +255,8 @@
252255
<internalFileTemplate name="Magento Delete Entity By Id Command"/>
253256
<internalFileTemplate name="Magento Entity Edit Action Controller Class"/>
254257
<internalFileTemplate name="Magento Entity Delete Controller Class"/>
258+
<internalFileTemplate name="Magento Web Api XML"/>
259+
<internalFileTemplate name="Web Api Interface"/>
255260

256261
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
257262

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
4+
namespace ${NAMESPACE};
5+
6+
#if (${USES})
7+
#set ($uses = ${USES})
8+
#foreach ($use in $uses.split(","))
9+
use $use;
10+
#end
11+
12+
#end
13+
/**
14+
* ${DESCRIPTION}
15+
*
16+
* @api
17+
*/
18+
interface ${INTERFACE_NAME}
19+
{
20+
#set ($methods = ${METHODS})
21+
#foreach ($method in $methods.split(${METHODS_DELIMITER}))
22+
$method;
23+
#end
24+
}

resources/fileTemplates/internal/Web Api Interface.php.html

Whitespace-only changes.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.PhpClass;
13+
import com.magento.idea.magento2plugin.MagentoIcons;
14+
import com.magento.idea.magento2plugin.actions.generation.dialog.NewInterfaceForServiceDialog;
15+
import com.magento.idea.magento2plugin.project.Settings;
16+
import com.magento.idea.magento2plugin.util.magento.IsFileInEditableModuleUtil;
17+
import com.magento.idea.magento2plugin.util.php.PhpPsiElementsUtil;
18+
import org.jetbrains.annotations.NotNull;
19+
20+
public class NewWebApiInterfaceAction extends AnAction {
21+
22+
public static final String ACTION_NAME = "Create a new Web API interface for this class";
23+
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Web API interface";
24+
private PhpClass currentPhpClass;
25+
26+
/**
27+
* New Web API interface action constructor.
28+
*/
29+
public NewWebApiInterfaceAction() {
30+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
31+
}
32+
33+
@Override
34+
public void update(final @NotNull AnActionEvent event) {
35+
setIsAvailableForEvent(event, false);
36+
final Project project = event.getProject();
37+
38+
if (project == null || !Settings.isEnabled(project)) {
39+
return;
40+
}
41+
final PhpClass phpClass = PhpPsiElementsUtil.getPhpClass(event);
42+
43+
if (phpClass == null
44+
|| phpClass.isAbstract()
45+
|| !IsFileInEditableModuleUtil.execute(phpClass.getContainingFile())) {
46+
return;
47+
}
48+
currentPhpClass = phpClass;
49+
setIsAvailableForEvent(event, true);
50+
}
51+
52+
@Override
53+
public void actionPerformed(final @NotNull AnActionEvent event) {
54+
final PsiDirectory directory =
55+
currentPhpClass.getContainingFile().getContainingDirectory();
56+
57+
if (event.getProject() == null || currentPhpClass == null || directory == null) {
58+
return;
59+
}
60+
61+
NewInterfaceForServiceDialog.open(
62+
event.getProject(),
63+
directory,
64+
currentPhpClass
65+
);
66+
}
67+
68+
/**
69+
* Set is action available for event.
70+
*
71+
* @param event AnActionEvent
72+
* @param isAvailable boolean
73+
*/
74+
private void setIsAvailableForEvent(
75+
final @NotNull AnActionEvent event,
76+
final boolean isAvailable
77+
) {
78+
event.getPresentation().setVisible(isAvailable);
79+
event.getPresentation().setEnabled(isAvailable);
80+
}
81+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.php;
7+
8+
import com.jetbrains.php.lang.psi.elements.Method;
9+
import java.util.List;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
public class WebApiInterfaceData {
13+
14+
private final String moduleName;
15+
private final String classFqn;
16+
private final String name;
17+
private final String description;
18+
private final List<Method> methods;
19+
20+
/**
21+
* Web Api interface DTO constructor.
22+
*
23+
* @param moduleName String
24+
* @param classFqn String
25+
* @param name String
26+
* @param description String
27+
* @param methods List[Method]
28+
*/
29+
public WebApiInterfaceData(
30+
final @NotNull String moduleName,
31+
final @NotNull String classFqn,
32+
final @NotNull String name,
33+
final @NotNull String description,
34+
final @NotNull List<Method> methods
35+
) {
36+
this.moduleName = moduleName;
37+
this.classFqn = classFqn;
38+
this.name = name;
39+
this.description = description;
40+
this.methods = methods;
41+
}
42+
43+
/**
44+
* Get module name.
45+
*
46+
* @return String
47+
*/
48+
public String getModuleName() {
49+
return moduleName;
50+
}
51+
52+
/**
53+
* Get class FQN for which api interface to be generated.
54+
*
55+
* @return String
56+
*/
57+
public String getClassFqn() {
58+
return classFqn;
59+
}
60+
61+
/**
62+
* Get interface name.
63+
*
64+
* @return String
65+
*/
66+
public String getName() {
67+
return name;
68+
}
69+
70+
/**
71+
* Get description for the api interface class.
72+
*
73+
* @return String
74+
*/
75+
public String getDescription() {
76+
return description;
77+
}
78+
79+
/**
80+
* Get list of methods for the api interface.
81+
*
82+
* @return List[Method]
83+
*/
84+
public List<Method> getMethods() {
85+
return methods;
86+
}
87+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ private String resolveFieldValueByComponentType(final Field field) {
192192
final JComponent component = ExtractComponentFromFieldUtil.extract(field, this);
193193

194194
if (component instanceof JTextField) {
195-
return ((JTextField) component).isEditable()
196-
? ((JTextField) component).getText() : null;
195+
return ((JTextField) component).getText();
197196
} else if (component instanceof JComboBox) {
198197
if (((JComboBox<?>) component).getSelectedIndex() == -1) {
199198
return "";

0 commit comments

Comments
 (0)