-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2320 from Haehnchen/feature/controller-new
migrate Symfony controller action and including template guessing
- Loading branch information
Showing
8 changed files
with
194 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,9 @@ | |
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
*/ | ||
public class NewCommandAction extends AbstractProjectDumbAwareAction { | ||
public NewCommandAction() { | ||
super("Command", "Create Command Class", Symfony2Icons.SYMFONY); | ||
|
@@ -49,6 +52,10 @@ public void actionPerformed(@NotNull AnActionEvent event) { | |
return; | ||
} | ||
|
||
if (!className.toLowerCase().endsWith("command")) { | ||
className += "Command"; | ||
} | ||
|
||
if (!PhpNameUtil.isValidClassName(className)) { | ||
JOptionPane.showMessageDialog(null, "Invalid class name"); | ||
return; | ||
|
@@ -60,16 +67,17 @@ public void actionPerformed(@NotNull AnActionEvent event) { | |
return; | ||
} | ||
|
||
String finalClassName = className; | ||
ApplicationManager.getApplication().runWriteAction(() -> { | ||
HashMap<String, String> hashMap = new HashMap<>() {{ | ||
String clazz = className; | ||
if (className.endsWith("Command")) { | ||
clazz = className.substring(0, "Command".length()); | ||
String clazz = finalClassName; | ||
if (finalClassName.endsWith("Command")) { | ||
clazz = finalClassName.substring(0, finalClassName.length() - "Command".length()); | ||
} | ||
|
||
String prefix = NewFileActionUtil.getCommandPrefix(directory); | ||
|
||
put("class", className); | ||
put("class", finalClassName); | ||
put("namespace", strings.get(0)); | ||
put("command_name", prefix + ":" + fr.adrienbrault.idea.symfony2plugin.util.StringUtils.underscore(clazz)); | ||
}}; | ||
|
@@ -78,7 +86,7 @@ public void actionPerformed(@NotNull AnActionEvent event) { | |
project, | ||
directory.getVirtualFile(), | ||
NewFileActionUtil.guessCommandTemplateType(project), | ||
className, | ||
finalClassName, | ||
hashMap | ||
); | ||
|
||
|
@@ -94,8 +102,7 @@ public void update(AnActionEvent event) { | |
return; | ||
} | ||
|
||
PsiDirectory directory = NewFileActionUtil.getSelectedDirectoryFromAction(event); | ||
this.setStatus(event, directory != null && "Command".equals(directory.getName())); | ||
this.setStatus(event, NewFileActionUtil.isInGivenDirectoryScope(event, "Command")); | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/main/java/fr/adrienbrault/idea/symfony2plugin/action/NewControllerAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.action; | ||
|
||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.fileEditor.OpenFileDescriptor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.ui.Messages; | ||
import com.intellij.psi.PsiDirectory; | ||
import com.intellij.psi.PsiElement; | ||
import com.jetbrains.php.refactoring.PhpNameUtil; | ||
import com.jetbrains.php.roots.PhpNamespaceCompositeProvider; | ||
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons; | ||
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; | ||
import fr.adrienbrault.idea.symfony2plugin.util.psi.PhpBundleFileFactory; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.swing.*; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
*/ | ||
public class NewControllerAction extends AbstractProjectDumbAwareAction { | ||
public NewControllerAction() { | ||
super("Controller", "Create Controller Class", Symfony2Icons.SYMFONY); | ||
} | ||
|
||
public void update(AnActionEvent event) { | ||
this.setStatus(event, false); | ||
Project project = getEventProject(event); | ||
if (!Symfony2ProjectComponent.isEnabled(project)) { | ||
return; | ||
} | ||
|
||
if (NewFileActionUtil.getSelectedDirectoryFromAction(event) != null) { | ||
this.setStatus(event, true); | ||
} | ||
} | ||
|
||
@Override | ||
public void actionPerformed(@NotNull AnActionEvent event) { | ||
PsiDirectory directory = NewFileActionUtil.getSelectedDirectoryFromAction(event); | ||
if (directory == null) { | ||
return; | ||
} | ||
|
||
Project project = getEventProject(event); | ||
String className = Messages.showInputDialog(project, "New class name:", "New File", Symfony2Icons.SYMFONY); | ||
if (StringUtils.isBlank(className)) { | ||
return; | ||
} | ||
|
||
if (!className.toLowerCase().endsWith("controller")) { | ||
className += "Controller"; | ||
} | ||
|
||
if (!PhpNameUtil.isValidClassName(className)) { | ||
JOptionPane.showMessageDialog(null, "Invalid class name"); | ||
return; | ||
} | ||
|
||
List<String> strings = PhpNamespaceCompositeProvider.INSTANCE.suggestNamespaces(directory); | ||
if (strings.isEmpty()) { | ||
JOptionPane.showMessageDialog(null, "No namespace found"); | ||
return; | ||
} | ||
|
||
String finalClassName = className; | ||
ApplicationManager.getApplication().runWriteAction(() -> { | ||
HashMap<String, String> hashMap = new HashMap<>() {{ | ||
String clazz = finalClassName; | ||
if (finalClassName.toLowerCase().endsWith("controller")) { | ||
clazz = finalClassName.substring(0, finalClassName.length() - "controller".length()); | ||
} | ||
|
||
put("class", finalClassName); | ||
put("namespace", strings.get(0)); | ||
put("path", "/" + fr.adrienbrault.idea.symfony2plugin.util.StringUtils.underscore(clazz).replace("_", "-")); | ||
put("template_path", fr.adrienbrault.idea.symfony2plugin.util.StringUtils.underscore(clazz)); | ||
}}; | ||
|
||
PsiElement commandAttributes = PhpBundleFileFactory.createFile( | ||
project, | ||
directory.getVirtualFile(), | ||
NewFileActionUtil.guessControllerTemplateType(project), | ||
finalClassName, | ||
hashMap | ||
); | ||
|
||
new OpenFileDescriptor(project, commandAttributes.getContainingFile().getVirtualFile(), 0).navigate(true); | ||
}); | ||
} | ||
|
||
public static class Shortcut extends NewControllerAction { | ||
@Override | ||
public void update(AnActionEvent event) { | ||
Project project = getEventProject(event); | ||
if (!Symfony2ProjectComponent.isEnabled(project)) { | ||
return; | ||
} | ||
|
||
this.setStatus(event, NewFileActionUtil.isInGivenDirectoryScope(event, "Controller")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 0 additions & 58 deletions
58
...ain/java/fr/adrienbrault/idea/symfony2plugin/action/bundle/NewBundleControllerAction.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
src/main/resources/fileTemplates/controller_annotations.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace {{ namespace }}; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
class {{ class }} extends AbstractController | ||
{ | ||
|
||
/** | ||
* @Route("{{ path }}") | ||
*/ | ||
public function index(): Response | ||
{ | ||
return $this->render('{{ template_path }}/index.html.twig'); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/resources/fileTemplates/controller_attributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace {{ namespace }}; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
|
||
class {{ class }} extends AbstractController | ||
{ | ||
#[Route('{{ path }}')] | ||
public function index(): Response | ||
{ | ||
return $this->render('{{ template_path }}/index.html.twig'); | ||
} | ||
} |