-
Notifications
You must be signed in to change notification settings - Fork 105
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
eduard13
merged 5 commits into
magento:4.0.0-develop
from
bohdan-harniuk:entity-web-api-generation
May 5, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e75cf41
Added Web Api interface generation for a service
bohdan-harniuk cf8b80f
Merge branch '4.0.0-develop' of github.com:magento/magento2-phpstorm-…
bohdan-harniuk 858efb1
Fixed tests static code checks
bohdan-harniuk 5f17af6
Fixed failed static code checks
bohdan-harniuk 646e288
Code refactoring after code review
bohdan-harniuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
91 changes: 91 additions & 0 deletions
91
src/com/magento/idea/magento2plugin/actions/generation/NewWebApiInterfaceAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* 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.RegExUtil; | ||
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; | ||
} | ||
// Excluding API generators for Test/ and *Test.php files | ||
// in order to not overload the context menu. | ||
final String filename = phpClass.getContainingFile().getName(); | ||
|
||
if (filename.matches(RegExUtil.Magento.TEST_FILE_NAME) | ||
|| phpClass.getPresentableFQN().matches(RegExUtil.Magento.TEST_CLASS_FQN)) { | ||
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); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/com/magento/idea/magento2plugin/actions/generation/data/php/WebApiInterfaceData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.