Skip to content

Web Api interface generation #586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
<action id="MagentoCreateAWebApiDeclaration.Menu" class="com.magento.idea.magento2plugin.actions.generation.NewWebApiDeclarationAction">
<add-to-group group-id="EditorPopupMenu"/>
</action>
<action id="MagentoCreateAWebApiInterfaceForService.Menu" class="com.magento.idea.magento2plugin.actions.generation.NewWebApiInterfaceAction">
<add-to-group group-id="EditorPopupMenu"/>
</action>

<action id="CopyMagentoPath"
class="com.magento.idea.magento2plugin.actions.CopyMagentoPath"
Expand Down Expand Up @@ -260,6 +263,8 @@
<internalFileTemplate name="Magento Delete Entity By Id Command"/>
<internalFileTemplate name="Magento Entity Edit Action Controller Class"/>
<internalFileTemplate name="Magento Entity Delete Controller Class"/>
<internalFileTemplate name="Magento Web Api XML"/>
<internalFileTemplate name="Web Api Interface"/>
Comment on lines +266 to +267
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<internalFileTemplate name="Magento Web Api XML"/>
<internalFileTemplate name="Web Api Interface"/>
<internalFileTemplate name="Magento Web API XML"/>
<internalFileTemplate name="Web API Interface"/>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


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

Expand Down
24 changes: 24 additions & 0 deletions resources/fileTemplates/internal/Web Api Interface.php.ft
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
#parse("PHP File Header.php")

namespace ${NAMESPACE};

#if (${USES})
#set ($uses = ${USES})
#foreach ($use in $uses.split(","))
use $use;
#end

#end
/**
* ${DESCRIPTION}
*
* @api
*/
interface ${INTERFACE_NAME}
{
#set ($methods = ${METHODS})
#foreach ($method in $methods.split(${METHODS_DELIMITER}))
$method;
#end
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.generation;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.generation.dialog.NewInterfaceForServiceDialog;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.util.magento.IsFileInEditableModuleUtil;
import com.magento.idea.magento2plugin.util.php.PhpPsiElementsUtil;
import org.jetbrains.annotations.NotNull;

public class NewWebApiInterfaceAction extends AnAction {

public static final String ACTION_NAME = "Create a new Web API interface for this class";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Web API interface";
private PhpClass currentPhpClass;

/**
* New Web API interface action constructor.
*/
public NewWebApiInterfaceAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
}

@Override
public void update(final @NotNull AnActionEvent event) {
setIsAvailableForEvent(event, false);
final Project project = event.getProject();

if (project == null || !Settings.isEnabled(project)) {
return;
}
final PhpClass phpClass = PhpPsiElementsUtil.getPhpClass(event);

if (phpClass == null
|| phpClass.isAbstract()
|| !IsFileInEditableModuleUtil.execute(phpClass.getContainingFile())) {
return;
}
currentPhpClass = phpClass;
setIsAvailableForEvent(event, true);
}

@Override
public void actionPerformed(final @NotNull AnActionEvent event) {
final PsiDirectory directory =
currentPhpClass.getContainingFile().getContainingDirectory();

if (event.getProject() == null || currentPhpClass == null || directory == null) {
return;
}

NewInterfaceForServiceDialog.open(
event.getProject(),
directory,
currentPhpClass
);
}

/**
* Set is action available for event.
*
* @param event AnActionEvent
* @param isAvailable boolean
*/
private void setIsAvailableForEvent(
final @NotNull AnActionEvent event,
final boolean isAvailable
) {
event.getPresentation().setVisible(isAvailable);
event.getPresentation().setEnabled(isAvailable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.generation.data.php;

import com.jetbrains.php.lang.psi.elements.Method;
import java.util.List;
import org.jetbrains.annotations.NotNull;

public class WebApiInterfaceData {

private final String moduleName;
private final String classFqn;
private final String name;
private final String description;
private final List<Method> methods;

/**
* Web Api interface DTO constructor.
*
* @param moduleName String
* @param classFqn String
* @param name String
* @param description String
* @param methods List[Method]
*/
public WebApiInterfaceData(
final @NotNull String moduleName,
final @NotNull String classFqn,
final @NotNull String name,
final @NotNull String description,
final @NotNull List<Method> methods
) {
this.moduleName = moduleName;
this.classFqn = classFqn;
this.name = name;
this.description = description;
this.methods = methods;
}

/**
* Get module name.
*
* @return String
*/
public String getModuleName() {
return moduleName;
}

/**
* Get class FQN for which api interface to be generated.
*
* @return String
*/
public String getClassFqn() {
return classFqn;
}

/**
* Get interface name.
*
* @return String
*/
public String getName() {
return name;
}

/**
* Get description for the api interface class.
*
* @return String
*/
public String getDescription() {
return description;
}

/**
* Get list of methods for the api interface.
*
* @return List[Method]
*/
public List<Method> getMethods() {
return methods;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ private String resolveFieldValueByComponentType(final Field field) {
final JComponent component = ExtractComponentFromFieldUtil.extract(field, this);

if (component instanceof JTextField) {
return ((JTextField) component).isEditable()
? ((JTextField) component).getText() : null;
return ((JTextField) component).getText();
} else if (component instanceof JComboBox) {
if (((JComboBox<?>) component).getSelectedIndex() == -1) {
return "";
Expand Down
Loading