Skip to content
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

Action/Code Generation. Plugin class generation from a class method #63

Merged
merged 9 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
* "Go to GraphQL resolver class" line marke in scope of GraphQL schema type arguments
* RequireJS mapping support (reference navigation, completion)
* Plugin class methods generation
* Plugin declaration inspection in scope of Plugin Class
* Plugin declaration inspection in scope of a Plugin Class
* MFTF support (reference navigation, completion)
* Fixed support of 2020.* versions of IDE's
* Create a New Magento 2 Module action
* Code Inspection: Duplicated Observer Usage in events XML
* Create a Plugin class for a class public method action

0.3.0
=============
Expand Down
11 changes: 7 additions & 4 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
<actions>
<group id="MagentoGenerateGroup">
<action id="MagentoGenerateBeforeMethodAction"
class="com.magento.idea.magento2plugin.actions.generation.MagentoGenerateBeforeMethodAction"
class="com.magento.idea.magento2plugin.actions.generation.PluginGenerateBeforeMethodAction"
text="Magento Before Plugin..."
description="Create Magento before plugin method."/>
<action id="MagentoGenerateAfterMethodAction"
class="com.magento.idea.magento2plugin.actions.generation.MagentoGenerateAfterMethodAction"
class="com.magento.idea.magento2plugin.actions.generation.PluginGenerateAfterMethodAction"
text="Magento After Plugin..."
description="Create Magento after plugin method."/>
<action id="MagentoGenerateAroundMethodAction"
class="com.magento.idea.magento2plugin.actions.generation.MagentoGenerateAroundMethodAction"
class="com.magento.idea.magento2plugin.actions.generation.PluginGenerateAroundMethodAction"
text="Magento Around Plugin..."
description="Create Magento around plugin method."/>
<add-to-group group-id="PhpGenerateGroup" anchor="last"/>
Expand All @@ -57,7 +57,9 @@
<action id="Magento2NewModule" class="com.magento.idea.magento2plugin.actions.generation.NewModuleAction"/>
<add-to-group group-id="NewGroup" anchor="after" relative-to-action="NewDir"/>
</group>

<action id="MagentoCreateAPlugin.Menu" class="com.magento.idea.magento2plugin.actions.generation.CreateAPluginAction">
<add-to-group group-id="EditorPopupMenu"/>
</action>
</actions>

<extensions defaultExtensionNs="com.intellij">
Expand Down Expand Up @@ -120,6 +122,7 @@
<internalFileTemplate name="Magento Module Composer"/>
<internalFileTemplate name="Magento Module Registration Php"/>
<internalFileTemplate name="Magento Module Xml"/>
<internalFileTemplate name="Magento Module DI Xml"/>
</extensions>

<application-components>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#if (${TYPE})
<type name="${TYPE}">
#end
<plugin name="${NAME}"
type="${PLUGIN_TYPE}" sortOrder="${SORT_ORDER}" />
#if (${TYPE})
</type>
#end
10 changes: 10 additions & 0 deletions resources/fileTemplates/code/Magento Module DI Xml Plugin.xml.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!--
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<html>
<body>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<!--
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<html>
<body>
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<!--
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<html>
<body>
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<!--
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<html>
<body>
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
Expand Down
3 changes: 3 additions & 0 deletions resources/fileTemplates/internal/Magento Module DI Xml.xml.ft
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
</config>
6 changes: 6 additions & 0 deletions resources/inspectionDescriptions/PluginInspection.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<!--
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<html>
<body>
<p>Plugins can not be used with the following:</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.generation;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.jetbrains.php.lang.psi.PhpFile;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.generation.dialog.CreateAPluginDialog;
import com.magento.idea.magento2plugin.util.GetFirstClassOfFile;
import com.magento.idea.magento2plugin.util.magento.plugin.IsPluginAllowedForMethod;
import org.jetbrains.annotations.NotNull;
import com.magento.idea.magento2plugin.project.Settings;

public class CreateAPluginAction extends DumbAwareAction {
public static String ACTION_NAME = "Create A Plugin...";
public static String ACTION_DESCRIPTION = "Create a new Magento 2 plugin for the class";
private final IsPluginAllowedForMethod isPluginAllowed;
private final GetFirstClassOfFile getFirstClassOfFile;
private Method targetMethod;
private PhpClass targetClass;

public CreateAPluginAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
this.isPluginAllowed = IsPluginAllowedForMethod.getInstance();
this.getFirstClassOfFile = GetFirstClassOfFile.getInstance();
}

public void update(AnActionEvent event) {
targetClass = null;
targetMethod = null;
Project project = event.getData(PlatformDataKeys.PROJECT);
if (Settings.isEnabled(project)) {
Pair<PsiFile, PhpClass> pair = this.findPhpClass(event);
PsiFile psiFile = pair.getFirst();
PhpClass phpClass = pair.getSecond();
targetClass = phpClass;
if (!(psiFile instanceof PhpFile) || phpClass.isFinal() || this.targetMethod == null) {
this.setStatus(event, false);
return;
}
} else {
this.setStatus(event, false);
return;
}
this.setStatus(event, true);
}

private void setStatus(AnActionEvent event, boolean status) {
event.getPresentation().setVisible(status);
event.getPresentation().setEnabled(status);
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
CreateAPluginDialog.open(e.getProject(), this.targetMethod, this.targetClass);
}

@Override
public boolean isDumbAware() {
return false;
}

private Pair<PsiFile, PhpClass> findPhpClass(@NotNull AnActionEvent event) {
PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);

PhpClass phpClass = null;
if (psiFile instanceof PhpFile) {
phpClass = getFirstClassOfFile.execute((PhpFile) psiFile);
fetchTargetMethod(event, psiFile, phpClass);
}

return Pair.create(psiFile, phpClass);
}

private void fetchTargetMethod(@NotNull AnActionEvent event, PsiFile psiFile, PhpClass phpClass) {
Caret caret = event.getData(PlatformDataKeys.CARET);
if (caret == null) {
return;
}
int offset = caret.getOffset();
PsiElement element = psiFile.findElementAt(offset);
if (element == null) {
return;
}
if (element instanceof Method && element.getParent() == phpClass && isPluginAllowed.check((Method)element)) {
this.targetMethod = (Method)element;
return;
}
PsiElement parent = element.getParent();
if (parent instanceof Method && parent.getParent() == phpClass && isPluginAllowed.check((Method)parent)) {
this.targetMethod = (Method)parent;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.actions.generation.data.MagentoPluginMethodData;
import com.magento.idea.magento2plugin.actions.generation.generator.MagentoPluginMethodsGenerator;
import com.magento.idea.magento2plugin.magento.files.Plugin;
import org.jetbrains.annotations.NotNull;

public class MagentoGenerateAroundMethodAction extends CodeInsightAction {
public static final String MAGENTO_PLUGIN_AROUND_METHOD_TEMPLATE_NAME = "Magento Plugin Around Method";

private final MagentoGeneratePluginMethodHandlerBase myHandler = new MagentoGeneratePluginMethodHandlerBase(MagentoPluginMethodData.Type.AROUND) {
public class PluginGenerateAfterMethodAction extends CodeInsightAction {
private final PluginGeneratePluginMethodHandlerBase myHandler = new PluginGeneratePluginMethodHandlerBase(Plugin.PluginType.after) {
protected MagentoPluginMethodData[] createPluginMethods(PhpClass currentClass, Method method, Key<Object> targetClassKey) {
return (new MagentoPluginMethodsGenerator(currentClass, method, targetClassKey)
.createPluginMethods(MAGENTO_PLUGIN_AROUND_METHOD_TEMPLATE_NAME, MagentoPluginMethodData.Type.AROUND));
.createPluginMethods(Plugin.PluginType.after));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.actions.generation.data.MagentoPluginMethodData;
import com.magento.idea.magento2plugin.actions.generation.generator.MagentoPluginMethodsGenerator;
import com.magento.idea.magento2plugin.magento.files.Plugin;
import org.jetbrains.annotations.NotNull;

public class MagentoGenerateAfterMethodAction extends CodeInsightAction {
public static final String MAGENTO_PLUGIN_AFTER_METHOD_TEMPLATE_NAME = "Magento Plugin After Method";

private final MagentoGeneratePluginMethodHandlerBase myHandler = new MagentoGeneratePluginMethodHandlerBase(MagentoPluginMethodData.Type.AFTER) {
public class PluginGenerateAroundMethodAction extends CodeInsightAction {
private final PluginGeneratePluginMethodHandlerBase myHandler = new PluginGeneratePluginMethodHandlerBase(Plugin.PluginType.around) {
protected MagentoPluginMethodData[] createPluginMethods(PhpClass currentClass, Method method, Key<Object> targetClassKey) {
return (new MagentoPluginMethodsGenerator(currentClass, method, targetClassKey)
.createPluginMethods(MAGENTO_PLUGIN_AFTER_METHOD_TEMPLATE_NAME, MagentoPluginMethodData.Type.AFTER));
.createPluginMethods(Plugin.PluginType.around));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.actions.generation.data.MagentoPluginMethodData;
import com.magento.idea.magento2plugin.actions.generation.generator.MagentoPluginMethodsGenerator;
import com.magento.idea.magento2plugin.magento.files.Plugin;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.util.Key;

public class MagentoGenerateBeforeMethodAction extends CodeInsightAction {
public static final String MAGENTO_PLUGIN_BEFORE_METHOD_TEMPLATE_NAME = "Magento Plugin Before Method";

private final MagentoGeneratePluginMethodHandlerBase myHandler = new MagentoGeneratePluginMethodHandlerBase(MagentoPluginMethodData.Type.BEFORE) {
public class PluginGenerateBeforeMethodAction extends CodeInsightAction {
private final PluginGeneratePluginMethodHandlerBase myHandler = new PluginGeneratePluginMethodHandlerBase(Plugin.PluginType.before) {
protected MagentoPluginMethodData[] createPluginMethods(PhpClass currentClass, Method method, Key<Object> targetClassKey) {
return (new MagentoPluginMethodsGenerator(currentClass, method, targetClassKey)
.createPluginMethods(MAGENTO_PLUGIN_BEFORE_METHOD_TEMPLATE_NAME, MagentoPluginMethodData.Type.BEFORE));
.createPluginMethods(Plugin.PluginType.before));
}
};

Expand Down
Loading