1818import com .magento .idea .magento2plugin .magento .files .RegistrationPhp ;
1919import com .magento .idea .magento2plugin .magento .packages .ComponentType ;
2020import com .magento .idea .magento2plugin .magento .packages .Package ;
21+ import com .magento .idea .magento2plugin .util .RegExUtil ;
2122import java .util .Collection ;
23+ import java .util .regex .Matcher ;
24+ import java .util .regex .Pattern ;
2225import org .jetbrains .annotations .NotNull ;
2326import org .jetbrains .annotations .Nullable ;
2427
@@ -49,12 +52,9 @@ public static MagentoModuleData getByContext(
4952 if (registrationFile == null ) {
5053 return null ;
5154 }
52- final PsiDirectory configDir = registrationFile
53- .getContainingDirectory ()
54- .findSubdirectory (Package .moduleBaseAreaDir );
55- final PsiDirectory viewDir = registrationFile
56- .getContainingDirectory ()
57- .findSubdirectory (Package .moduleViewDir );
55+ final PsiDirectory moduleDir = registrationFile .getContainingDirectory ();
56+ final PsiDirectory configDir = moduleDir .findSubdirectory (Package .moduleBaseAreaDir );
57+ final PsiDirectory viewDir = moduleDir .findSubdirectory (Package .moduleViewDir );
5858 final Collection <MethodReference > methodReferences = PsiTreeUtil .findChildrenOfType (
5959 registrationFile ,
6060 MethodReference .class
@@ -80,12 +80,42 @@ public static MagentoModuleData getByContext(
8080 return null ;
8181 }
8282
83- return new MagentoModuleData (name , resolvedType , configDir , viewDir );
83+ return new MagentoModuleData (name , resolvedType , moduleDir , configDir , viewDir );
8484 }
8585
8686 return null ;
8787 }
8888
89+ /**
90+ * Check if specified module is in the app/code directory.
91+ *
92+ * @param moduleData MagentoModuleData
93+ *
94+ * @return boolean
95+ */
96+ public static boolean isEditableModule (final @ NotNull MagentoModuleData moduleData ) {
97+ final Pattern pattern = Pattern .compile (RegExUtil .Magento .CUSTOM_VENDOR_NAME );
98+ final Matcher matcher = pattern .matcher (
99+ moduleData .getModuleDir ().getVirtualFile ().getPath ()
100+ );
101+
102+ return matcher .find ();
103+ }
104+
105+ /**
106+ * Check if specified directory is in the app/code.
107+ *
108+ * @param directory PsiDirectory
109+ *
110+ * @return boolean
111+ */
112+ public static boolean isDirectoryInEditableModule (final @ NotNull PsiDirectory directory ) {
113+ final Pattern pattern = Pattern .compile (RegExUtil .Magento .CUSTOM_VENDOR_NAME );
114+ final Matcher matcher = pattern .matcher (directory .getVirtualFile ().getPath ());
115+
116+ return matcher .find ();
117+ }
118+
89119 private static PsiFile getModuleRegistrationFile (
90120 final @ NotNull PsiDirectory directory ,
91121 final @ NotNull String basePath
@@ -136,6 +166,7 @@ public static class MagentoModuleData {
136166
137167 private final String name ;
138168 private final ComponentType type ;
169+ private final PsiDirectory moduleDir ;
139170 private final PsiDirectory configDir ;
140171 private final PsiDirectory viewDir ;
141172
@@ -144,30 +175,35 @@ public static class MagentoModuleData {
144175 *
145176 * @param name String
146177 * @param type ComponentType
178+ * @param moduleDir PsiDirectory
147179 */
148180 public MagentoModuleData (
149181 final @ NotNull String name ,
150- final @ NotNull ComponentType type
182+ final @ NotNull ComponentType type ,
183+ final @ NotNull PsiDirectory moduleDir
151184 ) {
152- this (name , type , null , null );
185+ this (name , type , moduleDir , null , null );
153186 }
154187
155188 /**
156189 * Constructor with a config directory specified.
157190 *
158191 * @param name String
159192 * @param type ComponentType
193+ * @param moduleDir PsiDirectory
160194 * @param configDir PsiDirectory
161195 * @param viewDir PsiDirectory
162196 */
163197 public MagentoModuleData (
164198 final @ NotNull String name ,
165199 final @ NotNull ComponentType type ,
200+ final @ NotNull PsiDirectory moduleDir ,
166201 final @ Nullable PsiDirectory configDir ,
167202 final @ Nullable PsiDirectory viewDir
168203 ) {
169204 this .name = name ;
170205 this .type = type ;
206+ this .moduleDir = moduleDir ;
171207 this .configDir = configDir ;
172208 this .viewDir = viewDir ;
173209 }
@@ -180,6 +216,10 @@ public ComponentType getType() {
180216 return type ;
181217 }
182218
219+ public PsiDirectory getModuleDir () {
220+ return moduleDir ;
221+ }
222+
183223 public @ Nullable PsiDirectory getConfigDir () {
184224 return configDir ;
185225 }
0 commit comments