Skip to content

1139: Creating a new observer template #1200

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
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<action id="MagentoCreateWidgetFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewWidgetXmlAction"/>
<action id="MagentoCreateLayoutFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewLayoutXmlAction"/>
<action id="MagentoCreateReadmeFile" class="com.magento.idea.magento2plugin.actions.context.md.NewReadmeMdAction"/>
<action id="MagentoDataPatchFile" class="com.magento.idea.magento2plugin.actions.context.php.NewObserverAction"/>
<!-- Context dependent actions -->
<separator/>
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewXml"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
#parse("PHP File Header.php")

namespace ${NAMESPACE};

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

/**
* Observes the `${EVENT_NAME}` event.
*/
class ${CLASS_NAME} implements ObserverInterface
{
/**
* Observer for ${EVENT_NAME}.
*
* @param Observer $observer
*
* @return void
*/
public function execute(Observer $observer)
{
$event = $observer->getEvent();
// TODO: Implement observer method.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!--
/*
* 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">
<tr>
<td>
<font face="verdana" size="-1">Observers are a certain type of Magento class that can influence
general behavior, performance, or change business logic. Observers are executed whenever the
event they are configured to watch is dispatched by the event manager.
<a href="https://devdocs.magento.com/guides/v2.3/extension-dev-guide/events-and-observers.html">
Read more</a> about observers.
</font>
</td>
</tr>
<tr>
<td>
<font face="verdana" size="-1">Check out the
<a href="https://devdocs.magento.com/guides/v2.3/ext-best-practices/extension-coding/observers-bp.html">
Observers best practices</a> to have a clean and professional implementation.</font>
</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
<tr>
<td colspan="3"><font face="verdana" size="-1">Predefined variables explanation:</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">Namespace for the class.</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${CLASS_NAME}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">Class name.</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${EVENT_NAME}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1">The name of the observer for the event definition.</font></td>
</tr>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.context.php;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.actions.context.CustomGeneratorContextAction;
import com.magento.idea.magento2plugin.actions.generation.dialog.NewObserverDialog;
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
import org.jetbrains.annotations.NotNull;

public class NewObserverAction extends CustomGeneratorContextAction {

public static final String ACTION_NAME = "Magento 2 Observer";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Observer";
public static final String ROOT_DIRECTORY = "Observer";

public NewObserverAction() {
super(ACTION_NAME, ACTION_DESCRIPTION);
}

@Override
public void actionPerformed(final @NotNull AnActionEvent event) {
final GetMagentoModuleUtil.MagentoModuleData moduleData = getModuleData();

if (event.getProject() == null || moduleData == null || getDirectory() == null) {
return;
}
final String[] templateData = moduleData.getName().split(Package.vendorModuleNameSeparator);

if (templateData.length != 2) { //NOPMD
return;
}

NewObserverDialog.open(
event.getProject(),
getDirectory(),
templateData[0],
templateData[1]
);
}

@Override
protected boolean isVisible(
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
final PsiDirectory targetDirectory,
final PsiFile targetFile
) {
if (!moduleData.getType().equals(ComponentType.module)
|| !moduleData.getModuleDir().equals(targetDirectory.getParentDirectory())) {
return false;
}

return ROOT_DIRECTORY.equals(targetDirectory.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.generation;

import com.intellij.psi.PsiDirectory;
import org.jetbrains.annotations.NotNull;

public class ModuleObserverData {

private final String packageName;
private final String moduleName;
private final String classFqn;
private final String evenName;
private final PsiDirectory baseDir;
private final String className;

/**
* Constructor.
*
* @param packageName String
* @param moduleName String
* @param classFqn String
* @param evenName String
* @param baseDir PsiDirectory
* @param className PsiDirectory
*/
public ModuleObserverData(
final @NotNull String packageName,
final @NotNull String moduleName,
final @NotNull String classFqn,
final @NotNull String evenName,
final @NotNull PsiDirectory baseDir,
final @NotNull String className
) {
this.packageName = packageName;
this.moduleName = moduleName;
this.classFqn = classFqn;
this.evenName = evenName;
this.baseDir = baseDir;
this.className = className;
}

public @NotNull String getPackageName() {
return packageName;
}

public @NotNull String getModuleName() {
return moduleName;
}

public @NotNull String getClassFqn() {
return classFqn;
}

public @NotNull PsiDirectory getBaseDir() {
return baseDir;
}

public @NotNull String getClassName() {
return className;
}

public @NotNull String getEventName() {
return evenName;
}
}
Loading