Skip to content

Commit 7540082

Browse files
authored
Merge pull request #586 from bohdan-harniuk/entity-web-api-generation
Web Api interface generation
2 parents fcec11e + 646e288 commit 7540082

File tree

23 files changed

+1699
-6
lines changed

23 files changed

+1699
-6
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"
@@ -260,6 +263,8 @@
260263
<internalFileTemplate name="Magento Delete Entity By Id Command"/>
261264
<internalFileTemplate name="Magento Entity Edit Action Controller Class"/>
262265
<internalFileTemplate name="Magento Entity Delete Controller Class"/>
266+
<internalFileTemplate name="Magento Web Api XML"/>
267+
<internalFileTemplate name="Web Api Interface"/>
263268

264269
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
265270

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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.RegExUtil;
17+
import com.magento.idea.magento2plugin.util.magento.IsFileInEditableModuleUtil;
18+
import com.magento.idea.magento2plugin.util.php.PhpPsiElementsUtil;
19+
import org.jetbrains.annotations.NotNull;
20+
21+
public class NewWebApiInterfaceAction extends AnAction {
22+
23+
public static final String ACTION_NAME = "Create a new Web API interface for this class";
24+
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Web API interface";
25+
private PhpClass currentPhpClass;
26+
27+
/**
28+
* New Web API interface action constructor.
29+
*/
30+
public NewWebApiInterfaceAction() {
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+
39+
if (project == null || !Settings.isEnabled(project)) {
40+
return;
41+
}
42+
final PhpClass phpClass = PhpPsiElementsUtil.getPhpClass(event);
43+
44+
if (phpClass == null
45+
|| phpClass.isAbstract()
46+
|| !IsFileInEditableModuleUtil.execute(phpClass.getContainingFile())) {
47+
return;
48+
}
49+
// Excluding API generators for Test/ and *Test.php files
50+
// in order to not overload the context menu.
51+
final String filename = phpClass.getContainingFile().getName();
52+
53+
if (filename.matches(RegExUtil.Magento.TEST_FILE_NAME)
54+
|| phpClass.getPresentableFQN().matches(RegExUtil.Magento.TEST_CLASS_FQN)) {
55+
return;
56+
}
57+
58+
currentPhpClass = phpClass;
59+
setIsAvailableForEvent(event, true);
60+
}
61+
62+
@Override
63+
public void actionPerformed(final @NotNull AnActionEvent event) {
64+
final PsiDirectory directory =
65+
currentPhpClass.getContainingFile().getContainingDirectory();
66+
67+
if (event.getProject() == null || currentPhpClass == null || directory == null) {
68+
return;
69+
}
70+
71+
NewInterfaceForServiceDialog.open(
72+
event.getProject(),
73+
directory,
74+
currentPhpClass
75+
);
76+
}
77+
78+
/**
79+
* Set is action available for event.
80+
*
81+
* @param event AnActionEvent
82+
* @param isAvailable boolean
83+
*/
84+
private void setIsAvailableForEvent(
85+
final @NotNull AnActionEvent event,
86+
final boolean isAvailable
87+
) {
88+
event.getPresentation().setVisible(isAvailable);
89+
event.getPresentation().setEnabled(isAvailable);
90+
}
91+
}
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)