Skip to content

Commit

Permalink
Merge pull request #2329 from Haehnchen/feature/services-template
Browse files Browse the repository at this point in the history
migrate new services file actions
  • Loading branch information
Haehnchen committed Apr 1, 2024
2 parents 77f7b21 + ba7207a commit 3f9d46b
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public AbstractProjectDumbAwareAction(String text, String description, Icon phpF
super(text, description, phpFile);
}

public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
setStatus(event, false);

Project project = event.getData(PlatformDataKeys.PROJECT);
if (project == null || !Symfony2ProjectComponent.isEnabled(project)) {
setStatus(event, false);
if (Symfony2ProjectComponent.isEnabled(project)) {
setStatus(event, true);
}
}

protected void setStatus(AnActionEvent event, boolean status) {
event.getPresentation().setVisible(status);
event.getPresentation().setEnabled(status);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public NewCommandAction() {
super("Command", "Create Command Class", Symfony2Icons.SYMFONY);
}

public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
this.setStatus(event, false);
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
Expand Down Expand Up @@ -96,7 +96,7 @@ public void actionPerformed(@NotNull AnActionEvent event) {

public static class Shortcut extends NewCommandAction {
@Override
public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public NewCompilerPassAction() {
super("CompilerPass", "Create CompilerPass Class", Symfony2Icons.SYMFONY);
}

public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
this.setStatus(event, false);
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
Expand Down Expand Up @@ -83,7 +83,7 @@ public void actionPerformed(@NotNull AnActionEvent event) {

public static class Shortcut extends NewCompilerPassAction {
@Override
public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public NewControllerAction() {
super("Controller", "Create Controller Class", Symfony2Icons.SYMFONY);
}

public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
this.setStatus(event, false);
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
Expand Down Expand Up @@ -95,7 +95,7 @@ public void actionPerformed(@NotNull AnActionEvent event) {

public static class Shortcut extends NewControllerAction {
@Override
public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.StreamUtil;
import com.intellij.psi.PsiDirectory;
import com.jetbrains.php.roots.PhpNamespaceCompositeProvider;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
Expand All @@ -13,8 +14,11 @@
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyCommandUtil;
import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle;
import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyCommand;
import fr.adrienbrault.idea.symfony2plugin.util.psi.PhpBundleFileFactory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.*;

/**
Expand Down Expand Up @@ -133,4 +137,15 @@ public static String getCommandPrefix(@NotNull PsiDirectory psiDirectory) {

return "app";
}


@Nullable
public static String getFileTemplateContent(@NotNull String filename) {
try {
// replace on windows, just for secure reasons
return StreamUtil.readText(PhpBundleFileFactory.class.getResourceAsStream(filename), "UTF-8").replace("\r\n", "\n");
} catch (IOException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public NewFormTypeAction() {
super("Form", "Create FormType Class", Symfony2Icons.SYMFONY);
}

public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
this.setStatus(event, false);
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
Expand Down Expand Up @@ -83,7 +83,7 @@ public void actionPerformed(@NotNull AnActionEvent event) {

public static class Shortcut extends NewFormTypeAction {
@Override
public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public NewKernelTestCaseAction() {
super("KernelTestCase", "Create KernelTestCase Class", Symfony2Icons.SYMFONY);
}

public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
this.setStatus(event, false);
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
Expand Down Expand Up @@ -89,7 +89,7 @@ public void actionPerformed(@NotNull AnActionEvent event) {

public static class Shortcut extends NewKernelTestCaseAction {
@Override
public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
this.setStatus(event, false);
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package fr.adrienbrault.idea.symfony2plugin.action;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.jetbrains.php.PhpIcons;

/**
* @author Daniel Espendiller <[email protected]>
*/
public class NewPhpServiceAction extends AbstractProjectDumbAwareAction {

public NewPhpServiceAction() {
super("PHP Service", "Create new PHP File", PhpIcons.PHP_FILE);
}

@Override
public void actionPerformed(AnActionEvent event) {
final Project project = event.getData(PlatformDataKeys.PROJECT);
ServiceActionUtil.buildFile(event, project, "/fileTemplates/container.php");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public NewTwigExtensionAction() {
super("TwigExtension", "Create TwigExtension Class", Symfony2Icons.SYMFONY);
}

public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
this.setStatus(event, false);
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
Expand Down Expand Up @@ -83,7 +83,7 @@ public void actionPerformed(@NotNull AnActionEvent event) {

public static class Shortcut extends NewTwigExtensionAction {
@Override
public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public NewWebTestCaseAction() {
super("WebTestCase", "Create WebTestCase Class", Symfony2Icons.SYMFONY);
}

public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
this.setStatus(event, false);
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
Expand Down Expand Up @@ -89,7 +89,7 @@ public void actionPerformed(@NotNull AnActionEvent event) {

public static class Shortcut extends NewWebTestCaseAction {
@Override
public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
this.setStatus(event, false);
Project project = getEventProject(event);
if (!Symfony2ProjectComponent.isEnabled(project)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ public void actionPerformed(AnActionEvent event) {
final Project project = event.getData(PlatformDataKeys.PROJECT);
ServiceActionUtil.buildFile(event, project, "/fileTemplates/container.xml");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ public void actionPerformed(AnActionEvent event) {
final Project project = event.getData(PlatformDataKeys.PROJECT);
ServiceActionUtil.buildFile(event, project, "/fileTemplates/container.yml");
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package fr.adrienbrault.idea.symfony2plugin.action;


import com.intellij.ide.IdeView;
import com.intellij.ide.highlighter.XmlFileType;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.io.StreamUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.impl.file.PsiDirectoryFactory;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlTag;
import com.jetbrains.php.lang.PhpFileType;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.Parameter;
import com.jetbrains.php.lang.psi.elements.PhpClass;
Expand All @@ -28,17 +25,14 @@
import fr.adrienbrault.idea.symfony2plugin.intentions.yaml.dict.YamlUpdateArgumentServicesCallback;
import fr.adrienbrault.idea.symfony2plugin.stubs.ContainerCollectionResolver;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil;
import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle;
import fr.adrienbrault.idea.symfony2plugin.util.psi.PhpBundleFileFactory;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.yaml.YAMLFileType;
import org.jetbrains.yaml.psi.*;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -58,79 +52,44 @@ private static String[] getInvalidArgumentAttributes() {
.toArray(String[]::new);
}

public static void buildFile(AnActionEvent event, final Project project, String templatePath) {
String extension = (templatePath.endsWith(".yml") || templatePath.endsWith(".yaml")) ? "yml" : "xml" ;
public static void buildFile(@NotNull AnActionEvent event, @NotNull Project project, @NotNull String templatePath) {
String extension;

String fileName = Messages.showInputDialog(project, "File name (without extension)", String.format("Create %s Service", extension), Symfony2Icons.SYMFONY);
if(fileName == null || StringUtils.isBlank(fileName)) {
return;
}

FileType fileType = (templatePath.endsWith(".yml") || templatePath.endsWith(".yaml")) ? YAMLFileType.YML : XmlFileType.INSTANCE ;

if(!fileName.endsWith("." + extension)) {
fileName = fileName.concat("." + extension);
}

DataContext dataContext = event.getDataContext();
IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
if (view == null) {
return;
if ((templatePath.endsWith(".yml") || templatePath.endsWith(".yaml"))) {
extension = "yaml";
} else if (templatePath.endsWith(".xml")) {
extension = "xml";
} else if (templatePath.endsWith(".php")) {
extension = "php";
} else {
throw new RuntimeException("no valid extension for: " + templatePath);
}

PsiDirectory[] directories = view.getDirectories();
if(directories.length == 0) {
String fileName = Messages.showInputDialog(project, "File name (without extension)", String.format("Create %s Service", extension), Symfony2Icons.SYMFONY);
if(fileName == null || StringUtils.isBlank(fileName)) {
return;
}

final PsiDirectory initialBaseDir = directories[0];
if (initialBaseDir == null) {
final PsiDirectory parentDirectory = NewFileActionUtil.getSelectedDirectoryFromAction(event);
if (parentDirectory == null) {
return;
}

if(initialBaseDir.findFile(fileName) != null) {
if(parentDirectory.findFile(fileName) != null) {
Messages.showInfoMessage("File exists", "Error");
return;
}

String content;
try {
content = StreamUtil.readText(ServiceActionUtil.class.getResourceAsStream(templatePath), "UTF-8").replace("\r\n", "\n");
} catch (IOException e) {
e.printStackTrace();
return;
}

final PsiFileFactory factory = PsiFileFactory.getInstance(project);

String bundleName = "Acme\\DemoBundle";

SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(project);
SymfonyBundle symfonyBundle = symfonyBundleUtil.getContainingBundle(initialBaseDir);

if(symfonyBundle != null) {
bundleName = StringUtils.strip(symfonyBundle.getNamespaceName(), "\\");
}

String underscoreBundle = bundleName.replace("\\", ".").toLowerCase();
if(underscoreBundle.endsWith("bundle")) {
underscoreBundle = underscoreBundle.substring(0, underscoreBundle.length() - 6);
}

content = content.replace("{{ BundleName }}", bundleName).replace("{{ BundleNameUnderscore }}", underscoreBundle);

final PsiFile file = factory.createFileFromText(fileName, fileType, content);

ApplicationManager.getApplication().runWriteAction(() -> {
CodeStyleManager.getInstance(project).reformat(file);
initialBaseDir.add(file);
});
PsiFile fileFromText = PsiFileFactory.getInstance(project).createFileFromText("services." + extension, PhpFileType.INSTANCE, NewFileActionUtil.getFileTemplateContent(templatePath));

PsiFile psiFile = initialBaseDir.findFile(fileName);
if(psiFile != null) {
view.selectElement(psiFile);
}
if (extension.equals("php")) {
CodeStyleManager.getInstance(project).reformat(fileFromText);
}

PsiElement newFile = PsiDirectoryFactory.getInstance(project).createDirectory(parentDirectory.getVirtualFile()).add(fileFromText);
new OpenFileDescriptor(project, newFile.getContainingFile().getVirtualFile(), 0).navigate(true);
});
}

@NotNull
Expand Down
Loading

0 comments on commit 3f9d46b

Please sign in to comment.