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

977: Added system.xml file in context generation #978

Merged
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
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<action id="MagentoCreateWebapiFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewWebapiXmlAction"/>
<action id="MagentoCreateAclFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewAclXmlAction"/>
<action id="MagentoCreateConfigFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewConfigXmlAction"/>
<action id="MagentoCreateSystemFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewSystemXmlAction"/>
<!-- Context dependent actions -->
<separator/>
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewXml"/>
Expand Down
7 changes: 7 additions & 0 deletions resources/fileTemplates/internal/Magento System XML.xml.ft
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
#parse("XML File Header.xml")
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
</system>
</config>
27 changes: 27 additions & 0 deletions resources/fileTemplates/internal/Magento System XML.xml.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
/*
* 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">
The system.xml file allows you to manage the Magento system configuration. Use this topic as a general reference for the system.xml file.
The system.xml file is located under etc/adminhtml/system.xml in a given Magento 2 extension.
</font><br>
</td>
</tr>
<tr>
<td><font face="verdana" size="-1">
Read more about the system.xml in the
<a href="https://devdocs.magento.com/guides/v2.4/config-guide/prod/config-reference-systemxml.html">
DevDocs</a>.
</font><br>
</td>
</tr>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.magento.files.ModuleFileInterface;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -154,6 +155,18 @@ protected FileTemplate getTemplate(
return template;
}

protected @Nullable PsiDirectory getGlobalScopeDir(final @NotNull PsiDirectory directory) {
PsiDirectory globalScopeDir;

if (Package.moduleBaseAreaDir.equals(directory.getName())) {
globalScopeDir = directory;
} else {
globalScopeDir = directory.getParentDirectory();
}

return globalScopeDir;
}

@Override
public boolean isDumbAware() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ protected boolean isVisible(
final @NotNull PsiDirectory targetDirectory,
final PsiFile targetFile
) {
final PsiDirectory configDir = moduleData.getConfigDir();

if (configDir == null) {
return false;
}

return targetDirectory.getName().equals(Package.moduleBaseAreaDir)
&& targetDirectory.equals(configDir)
&& moduleData.getType().equals(ComponentType.module);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ protected boolean isVisible(
final @NotNull PsiDirectory targetDirectory,
final PsiFile targetFile
) {
final PsiDirectory configDir = moduleData.getConfigDir();

if (configDir == null) {
return false;
}

return targetDirectory.getName().equals(Package.moduleBaseAreaDir)
&& targetDirectory.equals(configDir)
&& moduleData.getType().equals(ComponentType.module);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ protected boolean isVisible(
final @NotNull PsiDirectory targetDirectory,
final PsiFile targetFile
) {
final PsiDirectory configDir = moduleData.getConfigDir();
final PsiDirectory globalScopeDir = getGlobalScopeDir(targetDirectory);

if (configDir == null || globalScopeDir == null) {
return false;
}
final List<String> allowedDirectories = Arrays.asList(
Package.moduleBaseAreaDir,
Areas.adminhtml.toString(),
Expand All @@ -47,6 +53,7 @@ protected boolean isVisible(
);

return allowedDirectories.contains(targetDirectory.getName())
&& globalScopeDir.equals(configDir)
&& moduleData.getType().equals(ComponentType.module);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ protected boolean isVisible(
final @NotNull PsiDirectory targetDirectory,
final PsiFile targetFile
) {
final PsiDirectory configDir = moduleData.getConfigDir();
final PsiDirectory globalScopeDir = getGlobalScopeDir(targetDirectory);

if (configDir == null || globalScopeDir == null) {
return false;
}
final List<String> allowedDirectories = Arrays.asList(
Package.moduleBaseAreaDir,
Areas.adminhtml.toString(),
Areas.frontend.toString()
);

return allowedDirectories.contains(targetDirectory.getName())
&& globalScopeDir.equals(configDir)
&& moduleData.getType().equals(ComponentType.module);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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

import com.intellij.ide.fileTemplates.actions.AttributesDefaults;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.actions.context.AbstractContextAction;
import com.magento.idea.magento2plugin.magento.files.ModuleSystemXmlFile;
import com.magento.idea.magento2plugin.magento.packages.Areas;
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
import org.jetbrains.annotations.NotNull;

public class NewSystemXmlAction extends AbstractContextAction {

public static final String ACTION_NAME = "Magento 2 System File";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 system.xml file";

/**
* New system.xml file generation action constructor.
*/
public NewSystemXmlAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, new ModuleSystemXmlFile());
}

@Override
protected boolean isVisible(
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
final @NotNull PsiDirectory targetDirectory,
final PsiFile targetFile
) {
final PsiDirectory parentDir = targetDirectory.getParentDirectory();
final PsiDirectory configDir = moduleData.getConfigDir();

if (parentDir == null || configDir == null) {
return false;
}

return targetDirectory.getName().equals(Areas.adminhtml.toString())
&& parentDir.equals(configDir)
&& moduleData.getType().equals(ComponentType.module);
}

@Override
protected AttributesDefaults getProperties(
final @NotNull AttributesDefaults defaults,
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
final PsiDirectory targetDirectory,
final PsiFile targetFile
) {
return defaults;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ protected boolean isVisible(
final @NotNull PsiDirectory targetDirectory,
final PsiFile targetFile
) {
final PsiDirectory configDir = moduleData.getConfigDir();

if (configDir == null) {
return false;
}

return targetDirectory.getName().equals(Package.moduleBaseAreaDir)
&& targetDirectory.equals(configDir)
&& moduleData.getType().equals(ComponentType.module);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,24 @@ public XmlCompletionContributor() {

// <source_model>php class completion</source_model> in system.xml files.
extend(CompletionType.BASIC, PlatformPatterns.psiElement(XmlTokenType.XML_DATA_CHARACTERS)
.inside(XmlPatterns.xmlTag().withName(ModuleSystemXml.XML_TAG_SOURCE_MODEL)
.withParent(XmlPatterns.xmlTag().withName(ModuleSystemXml.FIELD_ELEMENT_NAME))
).inFile(XmlPatterns.xmlFile().withName(StandardPatterns.string().matches(ModuleSystemXml.FILE_NAME))),
.inside(XmlPatterns.xmlTag().withName(ModuleSystemXmlFile.XML_TAG_SOURCE_MODEL)
.withParent(XmlPatterns.xmlTag().withName(ModuleSystemXmlFile.FIELD_ELEMENT_NAME))
).inFile(XmlPatterns.xmlFile().withName(StandardPatterns.string().matches(ModuleSystemXmlFile.FILE_NAME))),
new PhpClassCompletionProvider()
);

// <frontend_model>completion</frontend_model>
extend(CompletionType.BASIC,
PlatformPatterns.psiElement(XmlTokenType.XML_DATA_CHARACTERS)
.inside(XmlPatterns.xmlTag().withName(ModuleSystemXml.XML_TAG_FRONTEND_MODEL)),
.inside(XmlPatterns.xmlTag().withName(ModuleSystemXmlFile.XML_TAG_FRONTEND_MODEL)),
new PhpClassCompletionProvider()
);

// <backend_model>completion</backend_model> in system.xml
extend(CompletionType.BASIC, PlatformPatterns.psiElement(XmlTokenType.XML_DATA_CHARACTERS)
.inside(XmlPatterns.xmlTag().withName(ModuleSystemXml.XML_TAG_BACKEND_MODEL)
.withParent(XmlPatterns.xmlTag().withName(ModuleSystemXml.FIELD_ELEMENT_NAME))
).inFile(XmlPatterns.xmlFile().withName(StandardPatterns.string().matches(ModuleSystemXml.FILE_NAME))),
.inside(XmlPatterns.xmlTag().withName(ModuleSystemXmlFile.XML_TAG_BACKEND_MODEL)
.withParent(XmlPatterns.xmlTag().withName(ModuleSystemXmlFile.FIELD_ELEMENT_NAME))
).inFile(XmlPatterns.xmlFile().withName(StandardPatterns.string().matches(ModuleSystemXmlFile.FILE_NAME))),
new PhpClassCompletionProvider()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,34 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.magento.files;

public class ModuleSystemXml {
import com.intellij.lang.Language;
import com.intellij.lang.xml.XMLLanguage;

public final class ModuleSystemXmlFile implements ModuleFileInterface {

public static final String FILE_NAME = "system.xml";
public static final String TEMPLATE = "Magento System XML";

public static final String FIELD_ELEMENT_NAME = "field";
public static final String XML_TAG_SOURCE_MODEL = "source_model";
public static final String XML_TAG_FRONTEND_MODEL = "frontend_model";
public static final String XML_TAG_BACKEND_MODEL = "backend_model";

@Override
public String getFileName() {
return FILE_NAME;
}

@Override
public String getTemplate() {
return TEMPLATE;
}

@Override
public Language getLanguage() {
return XMLLanguage.INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl;
import com.magento.idea.magento2plugin.magento.files.RegistrationPhp;
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
import com.magento.idea.magento2plugin.magento.packages.Package;
import java.util.Collection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class GetMagentoModuleUtil {

Expand Down Expand Up @@ -46,6 +48,9 @@ public static MagentoModuleData getByContext(
if (registrationFile == null) {
return null;
}
final PsiDirectory configDir = registrationFile
.getContainingDirectory()
.findSubdirectory(Package.moduleBaseAreaDir);
final Collection<MethodReference> methodReferences = PsiTreeUtil.findChildrenOfType(
registrationFile,
MethodReference.class
Expand All @@ -71,7 +76,7 @@ public static MagentoModuleData getByContext(
return null;
}

return new MagentoModuleData(name, resolvedType);
return new MagentoModuleData(name, resolvedType, configDir);
}

return null;
Expand Down Expand Up @@ -124,13 +129,36 @@ public static class MagentoModuleData {

private final String name;
private final ComponentType type;

private final PsiDirectory configDir;

/**
* Default constructor.
*
* @param name String
* @param type ComponentType
*/
public MagentoModuleData(
final @NotNull String name,
final @NotNull ComponentType type
) {
this(name, type, null);
}

/**
* Constructor with a config directory specified.
*
* @param name String
* @param type ComponentType
* @param configDir PsiDirectory
*/
public MagentoModuleData(
final @NotNull String name,
final @NotNull ComponentType type,
final @Nullable PsiDirectory configDir
) {
this.name = name;
this.type = type;
this.configDir = configDir;
}

public String getName() {
Expand All @@ -140,5 +168,9 @@ public String getName() {
public ComponentType getType() {
return type;
}

public @Nullable PsiDirectory getConfigDir() {
return configDir;
}
}
}
Loading