|
12 | 12 | import com.intellij.openapi.project.Project; |
13 | 13 | import com.intellij.psi.PsiElement; |
14 | 14 | import com.intellij.psi.PsiFile; |
| 15 | +import com.intellij.psi.PsiReference; |
15 | 16 | import com.intellij.psi.tree.IElementType; |
16 | 17 | import com.jetbrains.php.lang.lexer.PhpTokenTypes; |
17 | 18 | import com.jetbrains.php.lang.psi.PhpFile; |
| 19 | +import com.jetbrains.php.lang.psi.elements.Method; |
18 | 20 | import com.jetbrains.php.lang.psi.elements.MethodReference; |
19 | 21 | import com.jetbrains.php.lang.psi.elements.ParameterList; |
| 22 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
20 | 23 | import com.magento.idea.magento2plugin.MagentoIcons; |
21 | 24 | import com.magento.idea.magento2plugin.actions.generation.dialog.CreateAnObserverDialog; |
| 25 | +import com.magento.idea.magento2plugin.magento.files.Observer; |
22 | 26 | import com.magento.idea.magento2plugin.project.Settings; |
23 | 27 | import org.jetbrains.annotations.NotNull; |
24 | 28 |
|
25 | 29 | public class CreateAnObserverAction extends DumbAwareAction { |
26 | 30 | public static final String ACTION_NAME = "Create a Magento Observer..."; |
27 | 31 | static final String ACTION_DESCRIPTION = "Create a new Magento 2 Observer for the event"; |
28 | | - static final String SIGNATURE_INTERFACE = "#M#C\\Magento\\Framework\\Event\\ManagerInterface.dispatch"; |
29 | | - static final String SIGNATURE_CONTEXT = "#M#M#C\\Magento\\Framework\\App\\Action\\Context.getEventManager.dispatch"; |
30 | 32 | public String targetEvent; |
31 | 33 |
|
32 | 34 | public CreateAnObserverAction() { |
@@ -100,20 +102,23 @@ private boolean checkIsMethodReference(@NotNull PsiElement element) { |
100 | 102 | } |
101 | 103 |
|
102 | 104 | private boolean checkIsEventDispatchMethod(MethodReference element) { |
103 | | - return element.getSignature().equals(SIGNATURE_INTERFACE) || |
104 | | - element.getSignature().equals(SIGNATURE_CONTEXT) || |
105 | | - checkIsDispatchMethodGiven(element); |
106 | | - } |
107 | | - |
108 | | - private boolean checkIsDispatchMethodGiven(MethodReference element) { |
109 | | - String methodName = element.getName(); |
110 | | - String elementType = element.getType().toString().replace("|?",""); |
111 | | - |
112 | | - if (element.getSignature().equals(elementType) && methodName.equals("dispatch")) { |
113 | | - return true; |
| 105 | + PsiReference elementReference = element.getReference(); |
| 106 | + if (elementReference == null) { |
| 107 | + return false; |
114 | 108 | } |
115 | | - |
116 | | - return false; |
| 109 | + PsiElement method = elementReference.resolve(); |
| 110 | + if (!(method instanceof Method)) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + if (!((Method) method).getName().equals(Observer.DISPATCH_METHOD)) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + PsiElement phpClass = method.getParent(); |
| 117 | + if (!(phpClass instanceof PhpClass)) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + String fqn = ((PhpClass) phpClass).getPresentableFQN(); |
| 121 | + return fqn.equals(Observer.INTERFACE); |
117 | 122 | } |
118 | 123 |
|
119 | 124 | private boolean checkIsElementStringLiteral(@NotNull PsiElement element) { |
|
0 commit comments