|
| 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.openapi.actionSystem.AnAction; |
| 9 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 10 | +import com.intellij.openapi.project.Project; |
| 11 | +import com.intellij.psi.PsiDirectory; |
| 12 | +import com.jetbrains.php.lang.psi.elements.Method; |
| 13 | +import com.jetbrains.php.lang.psi.elements.Parameter; |
| 14 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 15 | +import com.magento.idea.magento2plugin.MagentoIcons; |
| 16 | +import com.magento.idea.magento2plugin.actions.generation.dialog.NewArgumentInjectionDialog; |
| 17 | +import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass; |
| 18 | +import com.magento.idea.magento2plugin.project.Settings; |
| 19 | +import com.magento.idea.magento2plugin.util.RegExUtil; |
| 20 | +import com.magento.idea.magento2plugin.util.php.PhpPsiElementsUtil; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | + |
| 23 | +public class InjectConstructorArgumentAction extends AnAction { |
| 24 | + |
| 25 | + public static final String ACTION_NAME = "Inject argument"; |
| 26 | + public static final String ACTION_DESCRIPTION = "Inject argument through the DI"; |
| 27 | + public static final String GATHER_ARRAY_VALUES_ACTION_DESCRIPTION = "Specify array values"; |
| 28 | + private PhpClass currentPhpClass; |
| 29 | + private Parameter currentParameter; |
| 30 | + |
| 31 | + /** |
| 32 | + * Inject constructor argument action constructor. |
| 33 | + */ |
| 34 | + public InjectConstructorArgumentAction() { |
| 35 | + super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void update(final @NotNull AnActionEvent event) { |
| 40 | + setIsAvailableForEvent(event, false); |
| 41 | + final Project project = event.getProject(); |
| 42 | + |
| 43 | + if (project == null || !Settings.isEnabled(project)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + final PhpClass phpClass = PhpPsiElementsUtil.getPhpClass(event); |
| 47 | + |
| 48 | + if (phpClass == null) { |
| 49 | + return; |
| 50 | + } |
| 51 | + // Excluding argument injection generators for Test/ and *Test.php files |
| 52 | + // in order to not overload the context menu. |
| 53 | + final String filename = phpClass.getContainingFile().getName(); |
| 54 | + |
| 55 | + if (filename.matches(RegExUtil.Magento.TEST_FILE_NAME) |
| 56 | + || phpClass.getPresentableFQN().matches(RegExUtil.Magento.TEST_CLASS_FQN)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + final Parameter parameter = PhpPsiElementsUtil.getMethodArgument(event); |
| 60 | + |
| 61 | + if (parameter == null) { |
| 62 | + return; |
| 63 | + } |
| 64 | + final Method method = parameter.getParent().getParent() instanceof Method |
| 65 | + ? (Method) parameter.getParent().getParent() : null; |
| 66 | + |
| 67 | + if (method == null) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (!method.getAccess().isPublic() |
| 72 | + || !method.getName().equals(MagentoPhpClass.CONSTRUCT_METHOD_NAME)) { |
| 73 | + return; |
| 74 | + } |
| 75 | + currentPhpClass = phpClass; |
| 76 | + currentParameter = parameter; |
| 77 | + setIsAvailableForEvent(event, true); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void actionPerformed(final @NotNull AnActionEvent event) { |
| 82 | + final PsiDirectory directory = |
| 83 | + currentPhpClass.getContainingFile().getContainingDirectory(); |
| 84 | + |
| 85 | + if (event.getProject() == null |
| 86 | + || currentPhpClass == null |
| 87 | + || directory == null |
| 88 | + || currentParameter == null) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + NewArgumentInjectionDialog.open( |
| 93 | + event.getProject(), |
| 94 | + directory, |
| 95 | + currentPhpClass, |
| 96 | + currentParameter |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Set is action available for event. |
| 102 | + * |
| 103 | + * @param event AnActionEvent |
| 104 | + * @param isAvailable boolean |
| 105 | + */ |
| 106 | + private void setIsAvailableForEvent( |
| 107 | + final @NotNull AnActionEvent event, |
| 108 | + final boolean isAvailable |
| 109 | + ) { |
| 110 | + event.getPresentation().setVisible(isAvailable); |
| 111 | + event.getPresentation().setEnabled(isAvailable); |
| 112 | + } |
| 113 | +} |
0 commit comments