Skip to content

Commit fae3dc2

Browse files
Merge pull request #1200 from anzin/1139-add-generator-to-create-observer-template
1139: Creating a new observer template
2 parents a14c613 + 6946398 commit fae3dc2

File tree

11 files changed

+937
-0
lines changed

11 files changed

+937
-0
lines changed

resources/META-INF/plugin.xml

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<action id="MagentoCreateWidgetFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewWidgetXmlAction"/>
7979
<action id="MagentoCreateLayoutFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewLayoutXmlAction"/>
8080
<action id="MagentoCreateReadmeFile" class="com.magento.idea.magento2plugin.actions.context.md.NewReadmeMdAction"/>
81+
<action id="MagentoDataPatchFile" class="com.magento.idea.magento2plugin.actions.context.php.NewObserverAction"/>
8182
<!-- Context dependent actions -->
8283
<separator/>
8384
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewXml"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
/**
10+
* Observes the `${EVENT_NAME}` event.
11+
*/
12+
class ${CLASS_NAME} implements ObserverInterface
13+
{
14+
/**
15+
* Observer for ${EVENT_NAME}.
16+
*
17+
* @param Observer $observer
18+
*
19+
* @return void
20+
*/
21+
public function execute(Observer $observer)
22+
{
23+
$event = $observer->getEvent();
24+
// TODO: Implement observer method.
25+
}
26+
}
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
|| !moduleData.getModuleDir().equals(targetDirectory.getParentDirectory())) {
57+
return false;
58+
}
59+
60+
return ROOT_DIRECTORY.equals(targetDirectory.getName());
61+
}
62+
}
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+
}

0 commit comments

Comments
 (0)