Skip to content

Commit 14df6aa

Browse files
author
Vitaliy
authored
Merge pull request #102 from vrann/issue-93
Implemented #93: Action/Code Generation. Observer Generation
2 parents db51cd8 + f178304 commit 14df6aa

24 files changed

+1069
-19
lines changed

resources/META-INF/plugin.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
<action id="Magento2NewModule" class="com.magento.idea.magento2plugin.actions.generation.NewModuleAction"/>
6666
<add-to-group group-id="NewGroup" anchor="after" relative-to-action="NewDir"/>
6767
</group>
68+
<action id="MagentoCreateAnObserver.Menu" class="com.magento.idea.magento2plugin.actions.generation.CreateAnObserverAction">
69+
<add-to-group group-id="EditorPopupMenu"/>
70+
</action>
6871
<action id="MagentoCreateAPlugin.Menu" class="com.magento.idea.magento2plugin.actions.generation.CreateAPluginAction">
6972
<add-to-group group-id="EditorPopupMenu"/>
7073
</action>
@@ -145,6 +148,8 @@
145148
<internalFileTemplate name="Magento Module DI Xml"/>
146149
<internalFileTemplate name="Magento Php Preference Class"/>
147150
<internalFileTemplate name="Magento Module Common Php Class"/>
151+
<internalFileTemplate name="Magento Observer Class"/>
152+
<internalFileTemplate name="Magento Module Events Xml"/>
148153
</extensions>
149154

150155
</idea-plugin>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<event name="${EVENT_NAME}">
2+
<observer name="${OBSERVER_NAME}" instance="${OBSERVER_CLASS}" />
3+
</event>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!--
2+
/*
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
</body>
10+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Observer for ${EVENT_NAME}
3+
*
4+
* @param Observer $observer
5+
* @return void
6+
*/
7+
public function execute(Observer $observer)
8+
{
9+
$event = $observer->getEvent();
10+
// TODO: Implement observer method.
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!--
2+
/*
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
</body>
10+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
3+
</config>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
4+
#if (${NAMESPACE})
5+
6+
namespace ${NAMESPACE};
7+
8+
#end
9+
use Magento\Framework\Event\ObserverInterface;
10+
use Magento\Framework\Event\Observer;
11+
12+
class ${NAME} implements ObserverInterface {
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!--
2+
/*
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
</body>
10+
</html>

src/com/magento/idea/magento2plugin/actions/generation/CreateAPluginAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.magento.idea.magento2plugin.project.Settings;
2424

2525
public class CreateAPluginAction extends DumbAwareAction {
26-
public static String ACTION_NAME = "Create A Plugin...";
26+
public static String ACTION_NAME = "Create a Plugin...";
2727
public static String ACTION_DESCRIPTION = "Create a new Magento 2 plugin for the class";
2828
private final IsPluginAllowedForMethod isPluginAllowed;
2929
private final GetFirstClassOfFile getFirstClassOfFile;
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation;
6+
7+
import com.intellij.lang.ASTNode;
8+
import com.intellij.openapi.actionSystem.AnActionEvent;
9+
import com.intellij.openapi.actionSystem.PlatformDataKeys;
10+
import com.intellij.openapi.editor.Caret;
11+
import com.intellij.openapi.project.DumbAwareAction;
12+
import com.intellij.openapi.project.Project;
13+
import com.intellij.psi.PsiElement;
14+
import com.intellij.psi.PsiFile;
15+
import com.intellij.psi.tree.IElementType;
16+
import com.jetbrains.php.lang.lexer.PhpTokenTypes;
17+
import com.jetbrains.php.lang.psi.PhpFile;
18+
import com.jetbrains.php.lang.psi.elements.MethodReference;
19+
import com.jetbrains.php.lang.psi.elements.ParameterList;
20+
import com.magento.idea.magento2plugin.MagentoIcons;
21+
import com.magento.idea.magento2plugin.actions.generation.dialog.CreateAnObserverDialog;
22+
import com.magento.idea.magento2plugin.project.Settings;
23+
import org.jetbrains.annotations.NotNull;
24+
25+
public class CreateAnObserverAction extends DumbAwareAction {
26+
public static final String ACTION_NAME = "Create a Magento Observer...";
27+
static final String ACTION_DESCRIPTION = "Create a new Magento 2 Observer for the event";
28+
static final String SIGNATURE_INTERFACE = "#M#C\\Magento\\Framework\\Event\\ManagerInterface.dispatch";
29+
static final String SIGNATURE_CONTEXT = "#M#M#C\\Magento\\Framework\\App\\Action\\Context.getEventManager.dispatch";
30+
public String targetEvent;
31+
32+
public CreateAnObserverAction() {
33+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
34+
}
35+
36+
public void update(AnActionEvent event) {
37+
Project project = event.getData(PlatformDataKeys.PROJECT);
38+
if (!Settings.isEnabled(project)) {
39+
this.setStatus(event, false);
40+
return;
41+
}
42+
PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);
43+
if (!(psiFile instanceof PhpFile)) {
44+
this.setStatus(event, false);
45+
return;
46+
}
47+
48+
PsiElement element = getElement(event);
49+
if (element == null) {
50+
this.setStatus(event, false);
51+
return;
52+
}
53+
54+
if (isObserverEventNameClicked(element)) {
55+
this.setStatus(event, true);
56+
targetEvent = element.getText();
57+
return;
58+
}
59+
60+
this.setStatus(event, false);
61+
}
62+
63+
private PsiElement getElement(@NotNull AnActionEvent event) {
64+
Caret caret = event.getData(PlatformDataKeys.CARET);
65+
PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);
66+
if (caret == null) {
67+
return null;
68+
}
69+
int offset = caret.getOffset();
70+
PsiElement element = psiFile.findElementAt(offset);
71+
if (element == null) {
72+
return null;
73+
}
74+
return element;
75+
}
76+
77+
private boolean isObserverEventNameClicked(@NotNull PsiElement element) {
78+
if (checkIsElementStringLiteral(element)) {
79+
if (checkIsParametersList(element.getParent().getParent()) &&
80+
checkIsMethodReference(element.getParent().getParent().getParent()) &&
81+
checkIsEventDispatchMethod((MethodReference) element.getParent().getParent().getParent())) {
82+
return true;
83+
}
84+
}
85+
return false;
86+
}
87+
88+
private boolean checkIsParametersList(@NotNull PsiElement element) {
89+
if (element instanceof ParameterList) {
90+
return true;
91+
}
92+
return false;
93+
}
94+
95+
private boolean checkIsMethodReference(@NotNull PsiElement element) {
96+
if (element instanceof MethodReference) {
97+
return true;
98+
}
99+
return false;
100+
}
101+
102+
private boolean checkIsEventDispatchMethod(MethodReference element) {
103+
return element.getSignature().equals(SIGNATURE_INTERFACE) || element.getSignature().equals(SIGNATURE_CONTEXT);
104+
}
105+
106+
private boolean checkIsElementStringLiteral(@NotNull PsiElement element) {
107+
ASTNode astNode = element.getNode();
108+
if (astNode == null) {
109+
return false;
110+
}
111+
IElementType elementType = astNode.getElementType();
112+
113+
if (elementType != PhpTokenTypes.STRING_LITERAL && elementType != PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE) {
114+
return false;
115+
}
116+
117+
return true;
118+
}
119+
120+
private void setStatus(AnActionEvent event, boolean status) {
121+
event.getPresentation().setVisible(status);
122+
event.getPresentation().setEnabled(status);
123+
}
124+
125+
@Override
126+
public void actionPerformed(@NotNull AnActionEvent e) {
127+
CreateAnObserverDialog.open(e.getProject(), this.targetEvent);
128+
}
129+
130+
@Override
131+
public boolean isDumbAware() {
132+
return false;
133+
}
134+
}

0 commit comments

Comments
 (0)