Skip to content

Commit 4915ec4

Browse files
committed
1139: Creating a new observer template
1 parent 428e6ec commit 4915ec4

File tree

11 files changed

+864
-0
lines changed

11 files changed

+864
-0
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<action id="MagentoCreateWidgetFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewWidgetXmlAction"/>
8080
<action id="MagentoCreateLayoutFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewLayoutXmlAction"/>
8181
<action id="MagentoCreateReadmeFile" class="com.magento.idea.magento2plugin.actions.context.md.NewReadmeMdAction"/>
82+
<action id="MagentoDataPatchFile" class="com.magento.idea.magento2plugin.actions.context.php.NewObserverAction"/>
8283
<!-- Context dependent actions -->
8384
<separator/>
8485
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewXml"/>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
4+
namespace ${NAMESPACE};
5+
6+
use Magento\Framework\Event\ObserverInterface;
7+
use Magento\Framework\Event\Observer;
8+
9+
class ${CLASS_NAME} implements ObserverInterface
10+
{
11+
/**
12+
* Observer for ${EVENT_NAME}.
13+
*
14+
* @param Observer $observer
15+
* @return void
16+
*/
17+
public function execute(Observer $observer)
18+
{
19+
$event = $observer->getEvent();
20+
// TODO: Implement observer method.
21+
}
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!--
2+
/*
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
10+
<tr>
11+
<td>
12+
<font face="verdana" size="-1">Observers are a certain type of Magento class that can influence
13+
general behavior, performance, or change business logic. Observers are executed whenever the
14+
event they are configured to watch is dispatched by the event manager.
15+
<a href="https://devdocs.magento.com/guides/v2.3/extension-dev-guide/events-and-observers.html">
16+
Read more</a> about observers.
17+
</font>
18+
</td>
19+
</tr>
20+
<tr>
21+
<td>
22+
<font face="verdana" size="-1">Check out the
23+
<a href="https://devdocs.magento.com/guides/v2.3/ext-best-practices/extension-coding/observers-bp.html">
24+
Observers best practices</a> to have a clean and professional implementation.</font>
25+
</td>
26+
</tr>
27+
</table>
28+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
29+
<tr>
30+
<td colspan="3"><font face="verdana" size="-1">Predefined variables explanation:</font></td>
31+
</tr>
32+
<tr>
33+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
34+
<td width="10">&nbsp;</td>
35+
<td width="100%" valign="top"><font face="verdana" size="-1">Namespace for the class.</font></td>
36+
</tr>
37+
<tr>
38+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${CLASS_NAME}</b></font></nobr></td>
39+
<td width="10">&nbsp;</td>
40+
<td width="100%" valign="top"><font face="verdana" size="-1">Class name.</font></td>
41+
</tr>
42+
<tr>
43+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${EVENT_NAME}</b></font></nobr></td>
44+
<td width="10">&nbsp;</td>
45+
<td width="100%" valign="top"><font face="verdana" size="-1">The name of the observer for the event definition.</font></td>
46+
</tr>
47+
</table>
48+
</body>
49+
</html>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.context.php;
7+
8+
import com.intellij.openapi.actionSystem.AnActionEvent;
9+
import com.intellij.psi.PsiDirectory;
10+
import com.intellij.psi.PsiFile;
11+
import com.magento.idea.magento2plugin.actions.context.CustomGeneratorContextAction;
12+
import com.magento.idea.magento2plugin.actions.generation.dialog.NewObserverDialog;
13+
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
14+
import com.magento.idea.magento2plugin.magento.packages.Package;
15+
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
16+
import org.jetbrains.annotations.NotNull;
17+
18+
public class NewObserverAction extends CustomGeneratorContextAction {
19+
20+
public static final String ACTION_NAME = "Magento 2 Observer";
21+
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Observer";
22+
public static final String ROOT_DIRECTORY = "Observer";
23+
24+
public NewObserverAction() {
25+
super(ACTION_NAME, ACTION_DESCRIPTION);
26+
}
27+
28+
@Override
29+
public void actionPerformed(final @NotNull AnActionEvent event) {
30+
final GetMagentoModuleUtil.MagentoModuleData moduleData = getModuleData();
31+
32+
if (event.getProject() == null || moduleData == null || getDirectory() == null) {
33+
return;
34+
}
35+
final String[] templateData = moduleData.getName().split(Package.vendorModuleNameSeparator);
36+
37+
if (templateData.length != 2) { //NOPMD
38+
return;
39+
}
40+
41+
NewObserverDialog.open(
42+
event.getProject(),
43+
getDirectory(),
44+
templateData[0],
45+
templateData[1]
46+
);
47+
}
48+
49+
@Override
50+
protected boolean isVisible(
51+
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
52+
final PsiDirectory targetDirectory,
53+
final PsiFile targetFile
54+
) {
55+
if (!moduleData.getType().equals(ComponentType.module)) {
56+
return false;
57+
}
58+
59+
return ROOT_DIRECTORY.equals(targetDirectory.getName());
60+
}
61+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.psi.PsiDirectory;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
public class ModuleObserverData {
12+
13+
private final String packageName;
14+
private final String moduleName;
15+
private final String classFqn;
16+
private final String evenName;
17+
private final PsiDirectory baseDir;
18+
private final String className;
19+
20+
/**
21+
* Constructor.
22+
*
23+
* @param packageName String
24+
* @param moduleName String
25+
* @param classFqn String
26+
* @param evenName String
27+
* @param baseDir PsiDirectory
28+
* @param className PsiDirectory
29+
*/
30+
public ModuleObserverData(
31+
final @NotNull String packageName,
32+
final @NotNull String moduleName,
33+
final @NotNull String classFqn,
34+
final @NotNull String evenName,
35+
final @NotNull PsiDirectory baseDir,
36+
final @NotNull String className
37+
) {
38+
this.packageName = packageName;
39+
this.moduleName = moduleName;
40+
this.classFqn = classFqn;
41+
this.evenName = evenName;
42+
this.baseDir = baseDir;
43+
this.className = className;
44+
}
45+
46+
public @NotNull String getPackageName() {
47+
return packageName;
48+
}
49+
50+
public @NotNull String getModuleName() {
51+
return moduleName;
52+
}
53+
54+
public @NotNull String getClassFqn() {
55+
return classFqn;
56+
}
57+
58+
public @NotNull PsiDirectory getBaseDir() {
59+
return baseDir;
60+
}
61+
62+
public @NotNull String getClassName() {
63+
return className;
64+
}
65+
66+
public @NotNull String getEventName() {
67+
return evenName;
68+
}
69+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.NewObserverDialog">
3+
<grid id="1871d" binding="contentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4+
<margin top="10" left="10" bottom="10" right="10"/>
5+
<constraints>
6+
<xy x="71" y="78" width="648" height="361"/>
7+
</constraints>
8+
<properties>
9+
<opaque value="true"/>
10+
<preferredSize width="720" height="280"/>
11+
<requestFocusEnabled value="true"/>
12+
</properties>
13+
<border type="none"/>
14+
<children>
15+
<grid id="9ad5" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
16+
<margin top="0" left="0" bottom="0" right="0"/>
17+
<constraints>
18+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
19+
</constraints>
20+
<properties/>
21+
<border type="none"/>
22+
<children>
23+
<hspacer id="4aa6d">
24+
<constraints>
25+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
26+
</constraints>
27+
</hspacer>
28+
<grid id="f892c" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
29+
<margin top="0" left="0" bottom="0" right="0"/>
30+
<constraints>
31+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
32+
</constraints>
33+
<properties/>
34+
<border type="none"/>
35+
<children>
36+
<component id="1b860" class="javax.swing.JButton" binding="buttonCancel">
37+
<constraints>
38+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
39+
</constraints>
40+
<properties>
41+
<text resource-bundle="magento2/common" key="common.cancel"/>
42+
</properties>
43+
</component>
44+
<component id="20801" class="javax.swing.JButton" binding="buttonOK">
45+
<constraints>
46+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
47+
</constraints>
48+
<properties>
49+
<text resource-bundle="magento2/common" key="common.ok"/>
50+
</properties>
51+
</component>
52+
</children>
53+
</grid>
54+
</children>
55+
</grid>
56+
<grid id="ad0b1" layout-manager="GridLayoutManager" row-count="8" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
57+
<margin top="0" left="0" bottom="0" right="0"/>
58+
<constraints>
59+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
60+
</constraints>
61+
<properties>
62+
<toolTipText value=""/>
63+
</properties>
64+
<border type="none"/>
65+
<children>
66+
<component id="96659" class="javax.swing.JLabel" binding="directoryStructureLabel">
67+
<constraints>
68+
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
69+
</constraints>
70+
<properties>
71+
<text value="Directory Path"/>
72+
</properties>
73+
</component>
74+
<component id="58ad9" class="javax.swing.JLabel" binding="evenNamesLabel">
75+
<constraints>
76+
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
77+
</constraints>
78+
<properties>
79+
<text value="Observer Even Name"/>
80+
</properties>
81+
</component>
82+
<component id="be834" class="javax.swing.JLabel" binding="directoryStructureErrorMessage">
83+
<constraints>
84+
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
85+
</constraints>
86+
<properties>
87+
<text value=""/>
88+
</properties>
89+
</component>
90+
<component id="f2094" class="javax.swing.JTextField" binding="directoryStructure">
91+
<constraints>
92+
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
93+
<preferred-size width="150" height="-1"/>
94+
</grid>
95+
</constraints>
96+
<properties>
97+
<editable value="true"/>
98+
<horizontalAlignment value="2"/>
99+
<text value=""/>
100+
<toolTipText value=""/>
101+
</properties>
102+
</component>
103+
<component id="12752" class="com.magento.idea.magento2plugin.ui.FilteredComboBox" binding="eventName" custom-create="true">
104+
<constraints>
105+
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
106+
</constraints>
107+
<properties/>
108+
</component>
109+
<component id="e9631" class="javax.swing.JLabel" binding="targetAreaLabel">
110+
<constraints>
111+
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
112+
</constraints>
113+
<properties>
114+
<text value="Target Area"/>
115+
</properties>
116+
</component>
117+
<component id="ec3f4" class="javax.swing.JComboBox" binding="observerArea" custom-create="true">
118+
<constraints>
119+
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
120+
</constraints>
121+
<properties/>
122+
</component>
123+
<component id="292c" class="javax.swing.JTextField" binding="className">
124+
<constraints>
125+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
126+
<preferred-size width="150" height="-1"/>
127+
</grid>
128+
</constraints>
129+
<properties/>
130+
</component>
131+
<component id="ffc6e" class="javax.swing.JTextField" binding="observerName">
132+
<constraints>
133+
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
134+
<preferred-size width="150" height="-1"/>
135+
</grid>
136+
</constraints>
137+
<properties/>
138+
</component>
139+
<component id="2c31c" class="javax.swing.JLabel" binding="observerNameLabel">
140+
<constraints>
141+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
142+
</constraints>
143+
<properties>
144+
<text value="Observer Name"/>
145+
</properties>
146+
</component>
147+
<component id="662ad" class="javax.swing.JLabel" binding="classNameLabel">
148+
<constraints>
149+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
150+
<preferred-size width="113" height="16"/>
151+
</grid>
152+
</constraints>
153+
<properties>
154+
<text value="Class Name"/>
155+
</properties>
156+
</component>
157+
<component id="5ad39" class="javax.swing.JLabel" binding="observerNameErrorMessage">
158+
<constraints>
159+
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
160+
</constraints>
161+
<properties>
162+
<text value=""/>
163+
</properties>
164+
</component>
165+
<component id="d30e2" class="javax.swing.JLabel" binding="classNameErrorMessage">
166+
<constraints>
167+
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
168+
</constraints>
169+
<properties>
170+
<text value=""/>
171+
</properties>
172+
</component>
173+
</children>
174+
</grid>
175+
<vspacer id="ec33f">
176+
<constraints>
177+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
178+
</constraints>
179+
</vspacer>
180+
</children>
181+
</grid>
182+
</form>

0 commit comments

Comments
 (0)