Skip to content

Commit 72c0c28

Browse files
committed
[T3CMS] Index ajax routes correctly
1 parent 7d20d42 commit 72c0c28

File tree

6 files changed

+84
-9
lines changed

6 files changed

+84
-9
lines changed

typo3-cms/src/main/java/com/cedricziel/idea/typo3/index/RouteIndex.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
public class RouteIndex extends FileBasedIndexExtension<String, RouteStub> {
2121

2222
public static final ID<String, RouteStub> KEY = ID.create("com.cedricziel.idea.typo3.index.route");
23+
public static final String AJAX_ROUTES_PHP = "AjaxRoutes.php";
24+
public static final String ROUTES_PHP = "Routes.php";
25+
public static final String EXT_TABLES_PHP = "ext_tables.php";
2326

2427
@NotNull
2528
public static boolean hasRoute(@NotNull Project project, @NotNull String routeName) {
@@ -75,13 +78,13 @@ public DataExternalizer<RouteStub> getValueExternalizer() {
7578

7679
@Override
7780
public int getVersion() {
78-
return 0;
81+
return 1;
7982
}
8083

8184
@NotNull
8285
@Override
8386
public FileBasedIndex.InputFilter getInputFilter() {
84-
return file -> file.getName().equals("Routes.php") || file.getName().equals("AjaxRoutes.php") || file.getName().equals("ext_tables.php");
87+
return file -> file.getName().equals(ROUTES_PHP) || file.getName().equals(AJAX_ROUTES_PHP) || file.getName().equals(EXT_TABLES_PHP);
8588
}
8689

8790
@Override
@@ -107,12 +110,15 @@ public void visitElement(PsiElement element) {
107110
PhpExpression classRefExpr = methodReference.getClassReference();
108111
if (!(classRefExpr instanceof ClassReference)) {
109112
super.visitElement(element);
113+
110114
return;
111115
}
112116

113117
ClassReference classReference = (ClassReference) classRefExpr;
114118

115-
if (classReference.getFQN().equals("\\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility") && methodReference.getName().equals("addModule")) {
119+
String fqn = classReference.getFQN();
120+
String methodName = methodReference.getName();
121+
if (fqn != null && fqn.equals("\\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility") && methodName != null && methodName.equals("addModule")) {
116122
RouteStub e = extractRouteStubFromMethodCall(methodReference);
117123
if (e != null) {
118124
routeStubs.add(e);
@@ -135,7 +141,7 @@ private RouteStub extractRouteStubFromMethodCall(MethodReference methodReference
135141
ArrayCreationExpression routeArray = (ArrayCreationExpression) psiElement;
136142
for (ArrayHashElement arrayHashElement : routeArray.getHashElements()) {
137143
PhpPsiElement key = arrayHashElement.getKey();
138-
if (key == null || !(key instanceof StringLiteralExpression)) {
144+
if (!(key instanceof StringLiteralExpression)) {
139145
continue;
140146
}
141147

typo3-cms/src/main/java/com/cedricziel/idea/typo3/routing/RouteHelper.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@
1818
public class RouteHelper {
1919
@NotNull
2020
public static Collection<RouteStub> routesFromRoutesPhp(@NotNull PsiFile psiFile) {
21+
RouteParserVisitor visitor;
22+
if (psiFile.getName().equals(RouteIndex.AJAX_ROUTES_PHP)) {
23+
visitor = new RouteParserVisitor("ajax_");
24+
} else {
25+
visitor = new RouteParserVisitor();
26+
}
2127

22-
RouteParserVisitor visitor = new RouteParserVisitor();
2328
visitor.visitElement(psiFile);
2429

2530
return visitor.getRouteStubs();
@@ -56,7 +61,7 @@ public static PsiElement[] getRouteDefinitionElements(@NotNull Project project,
5661
return true;
5762
}, GlobalSearchScope.allScope(project));
5863

59-
return results.toArray(new PsiElement[results.size()]);
64+
return results.toArray(new PsiElement[0]);
6065
}
6166

6267
@NotNull
@@ -76,7 +81,7 @@ private static PsiElement[] getTargetMethods(@NotNull Project project, @NotNull
7681
});
7782
}
7883

79-
return result.toArray(new PsiElement[result.size()]);
84+
return result.toArray(new PsiElement[0]);
8085
}
8186

8287
@NotNull

typo3-cms/src/main/java/com/cedricziel/idea/typo3/routing/RouteParserVisitor.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@
1414
*/
1515
public class RouteParserVisitor extends PsiRecursiveElementVisitor {
1616

17+
private final String prefix;
1718
private Collection<RouteStub> routeStubs;
1819

1920
public RouteParserVisitor() {
20-
routeStubs = new ArrayList<>();
21+
this.prefix = "";
22+
this.routeStubs = new ArrayList<>();
23+
}
24+
25+
public RouteParserVisitor(String prefix) {
26+
this.prefix = prefix;
27+
this.routeStubs = new ArrayList<>();
2128
}
2229

2330
@NotNull
@@ -60,7 +67,7 @@ private void visitRouteCreation(ArrayHashElement element) {
6067
if (valueMap instanceof ArrayCreationExpression) {
6168
ArrayCreationExpression propertyArray = (ArrayCreationExpression) valueMap;
6269

63-
routeDefinition.setName(key);
70+
routeDefinition.setName(prefix + key);
6471

6572
for (ArrayHashElement routePropertyHashElement : propertyArray.getHashElements()) {
6673
visitProperty(routeDefinition, routePropertyHashElement);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.cedricziel.idea.typo3.routing;
2+
3+
import com.cedricziel.idea.typo3.AbstractTestCase;
4+
import com.jetbrains.php.lang.PhpFileType;
5+
6+
import java.util.List;
7+
8+
public class RouteHelperTest extends AbstractTestCase {
9+
@Override
10+
protected String getTestDataPath() {
11+
return "testData/com/cedricziel/idea/typo3/routing";
12+
}
13+
14+
public void testCanCorrectlyIdentifyRoutesFromRoutesPhp() {
15+
myFixture.copyFileToProject("AjaxRoutes.php");
16+
myFixture.copyFileToProject("Routes.php");
17+
18+
myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
19+
"/** @var $uriBuilder \\TYPO3\\CMS\\Backend\\Routing\\UriBuilder */\n" +
20+
"$uriBuilder->buildUriFromRoute('<caret>');"
21+
);
22+
23+
myFixture.completeBasic();
24+
25+
List<String> lookupElementStrings = myFixture.getLookupElementStrings();
26+
assertTrue(lookupElementStrings.contains("ajax_solr_updateConnections"));
27+
assertTrue(lookupElementStrings.contains("ajax_solr_updateConnection"));
28+
assertTrue(lookupElementStrings.contains("xMOD_tximpexp"));
29+
}
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Definitions for routes provided by EXT:solr
5+
*/
6+
return [
7+
'solr_updateConnections' => [
8+
'path' => '/solr/updateConnections',
9+
'target' => \ApacheSolrForTypo3\Solr\Controller\Backend\AjaxController::class . '::updateConnections',
10+
],
11+
'solr_updateConnection' => [
12+
'path' => '/solr/updateConnection',
13+
'target' => \ApacheSolrForTypo3\Solr\Controller\Backend\AjaxController::class . '::updateConnection',
14+
],
15+
];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/**
4+
* Definitions for routes provided by EXT:impexp
5+
*/
6+
return [
7+
// Register click menu entry point
8+
'xMOD_tximpexp' => [
9+
'path' => '/record/importexport/',
10+
'target' => \TYPO3\CMS\Impexp\Controller\ImportExportController::class . '::mainAction',
11+
],
12+
];

0 commit comments

Comments
 (0)