|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2plugin.util.magento; |
| 7 | + |
| 8 | +import com.intellij.openapi.project.Project; |
| 9 | +import com.intellij.psi.PsiDirectory; |
| 10 | +import com.intellij.psi.PsiElement; |
| 11 | +import com.intellij.psi.util.PsiTreeUtil; |
| 12 | +import com.jetbrains.php.lang.psi.PhpFile; |
| 13 | +import com.jetbrains.php.lang.psi.elements.MethodReference; |
| 14 | +import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; |
| 15 | +import com.magento.idea.magento2plugin.magento.files.RegistrationPhp; |
| 16 | +import org.jetbrains.annotations.Nullable; |
| 17 | + |
| 18 | +public final class GetComponentNameByDirectoryUtil { |
| 19 | + |
| 20 | + private GetComponentNameByDirectoryUtil() {} |
| 21 | + |
| 22 | + /** |
| 23 | + * Returns component name. |
| 24 | + * |
| 25 | + * @param psiDirectory PsiDirectory |
| 26 | + * @param project Project |
| 27 | + * @return String |
| 28 | + */ |
| 29 | + public static String execute(final PsiDirectory psiDirectory, final Project project) { |
| 30 | + final PhpFile registrationPhp = getRegistrationPhpRecursively(psiDirectory, project); |
| 31 | + if (registrationPhp == null) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + final PsiElement[] childElements = registrationPhp.getChildren(); |
| 35 | + if (childElements.length == 0) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + return getName(childElements); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Get module registration.php file. |
| 43 | + * |
| 44 | + * @param psiDirectory PsiDirectory |
| 45 | + * @param project Project |
| 46 | + * @return PhpFile |
| 47 | + */ |
| 48 | + private static PhpFile getRegistrationPhpRecursively( |
| 49 | + final PsiDirectory psiDirectory, |
| 50 | + final Project project |
| 51 | + ) { |
| 52 | + final PsiElement[] containingFiles = psiDirectory.getChildren(); |
| 53 | + final PhpFile containingFile = getModuleRegistrationPhpFile(containingFiles); |
| 54 | + if (containingFile != null) { |
| 55 | + return containingFile; |
| 56 | + } |
| 57 | + final String basePath = project.getBasePath(); |
| 58 | + if (psiDirectory.getVirtualFile().getPath().equals(basePath)) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + final PsiDirectory getParent = psiDirectory.getParent(); |
| 62 | + if (getParent != null) { |
| 63 | + return getRegistrationPhpRecursively(getParent, project); |
| 64 | + } |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + private static String getName(final PsiElement... childElements) { |
| 69 | + final MethodReference[] methods = parseRegistrationPhpElements(childElements); |
| 70 | + for (final MethodReference method: methods) { |
| 71 | + if (!method.getName().equals(RegistrationPhp.REGISTER_METHOD_NAME)) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + final PsiElement[] parameters = method.getParameters(); |
| 75 | + for (final PsiElement parameter: parameters) { |
| 76 | + if (!(parameter instanceof StringLiteralExpression)) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + return ((StringLiteralExpression) parameter).getContents(); //NOPMD |
| 80 | + } |
| 81 | + } |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Parse registration PHP. |
| 87 | + * |
| 88 | + * @param elements PsiElement... |
| 89 | + * @return MethodReference[] |
| 90 | + */ |
| 91 | + public static MethodReference[] parseRegistrationPhpElements(final PsiElement... elements) { |
| 92 | + for (final PsiElement element: elements) { |
| 93 | + MethodReference[] methods = PsiTreeUtil |
| 94 | + .getChildrenOfType(element, MethodReference.class); |
| 95 | + if (methods == null) { |
| 96 | + final PsiElement[] children = element.getChildren(); |
| 97 | + methods = parseRegistrationPhpElements(children); |
| 98 | + } |
| 99 | + if (methods != null) { |
| 100 | + return methods; |
| 101 | + } |
| 102 | + } |
| 103 | + return new MethodReference[0]; |
| 104 | + } |
| 105 | + |
| 106 | + @Nullable |
| 107 | + private static PhpFile getModuleRegistrationPhpFile(final PsiElement... containingFiles) { |
| 108 | + if (containingFiles.length != 0) { |
| 109 | + for (final PsiElement containingFile: containingFiles) { |
| 110 | + if (!(containingFile instanceof PhpFile)) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + final String filename = ((PhpFile) containingFile).getName(); |
| 114 | + if (!filename.equals(RegistrationPhp.FILE_NAME)) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + return (PhpFile) containingFile; //NOPMD |
| 118 | + } |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | +} |
0 commit comments