Skip to content

Commit 5628749

Browse files
committed
Haehnchen#2177 Haehnchen#2266 add canonical routes to index: test and optimize
1 parent b2f824d commit 5628749

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/routing/RouteHelper.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,13 @@ public static Map<String, Route> getRoutesInsideUrlGeneratorFile(@NotNull PsiFil
514514
routes.put(routeArray.getKey(), route);
515515

516516
for (ArrayCreationExpression expression : routeArrayOptions) {
517-
for (ArrayHashElement e : expression.getHashElements()) {
518-
PhpPsiElement key = e.getKey();
519-
if (key != null && "'_canonical_route'".equals(key.getText())) {
520-
PhpPsiElement value = e.getValue();
521-
if (value != null) {
522-
String canonical = value.getText().replace("'", "");
523-
if (!routes.containsKey(canonical)) {
524-
routes.put(canonical, route);
517+
for (ArrayHashElement arrayHashElement : expression.getHashElements()) {
518+
if (arrayHashElement.getKey() instanceof StringLiteralExpression stringLiteralExpression && "_canonical_route".equals(stringLiteralExpression.getContents())) {
519+
if (arrayHashElement.getValue() instanceof StringLiteralExpression literalExpression) {
520+
String canonical = literalExpression.getContents();
521+
if (!canonical.isBlank() && !routes.containsKey(canonical)) {
522+
Route routeCanonical = convertRouteConfigForReturnArray(canonical, routeArrayOptions);
523+
routes.put(canonical, routeCanonical);
525524
}
526525
}
527526
break;
@@ -621,7 +620,7 @@ private static void collectRoutesOnArrayCreation(@NotNull Map<String, Route> rou
621620
@NotNull
622621
private static Route convertRouteConfigForReturnArray(@NotNull String routeName, @NotNull List<ArrayCreationExpression> hashElementCollection) {
623622
Set<String> variables = new HashSet<>();
624-
if(hashElementCollection.size() >= 1 && hashElementCollection.get(0) != null) {
623+
if(!hashElementCollection.isEmpty() && hashElementCollection.get(0) != null) {
625624
ArrayCreationExpression value = hashElementCollection.get(0);
626625
if(value != null) {
627626
variables.addAll(PhpElementsUtil.getArrayValuesAsString(value));
@@ -664,9 +663,9 @@ private static Route convertRouteConfigForReturnArray(@NotNull String routeName,
664663

665664
List<String> collect = foo.stream()
666665
.map(psiElement -> psiElement.getFirstChild() instanceof StringLiteralExpression ? ((StringLiteralExpression) psiElement.getFirstChild()).getContents() : null)
667-
.collect(Collectors.toList());
666+
.toList();
668667

669-
if (collect.size() > 0) {
668+
if (!collect.isEmpty()) {
670669
path.append(collect.get(1));
671670
}
672671

@@ -683,7 +682,7 @@ private static Route convertRouteConfigForReturnArray(@NotNull String routeName,
683682
}
684683

685684
// hostTokens = 4 need them?
686-
return new Route(routeName, variables, defaults, requirements, tokens, (path.length() == 0) ? null : path.toString());
685+
return new Route(routeName, variables, defaults, requirements, tokens, (path.isEmpty()) ? null : path.toString());
687686
}
688687

689688
/**
@@ -695,7 +694,7 @@ private static Route convertRouteConfig(@NotNull String routeName, @NotNull Arra
695694
hashValue.getHashElements().forEach(hashElementCollection::add);
696695

697696
Set<String> variables = new HashSet<>();
698-
if(hashElementCollection.size() >= 1 && hashElementCollection.get(0).getValue() instanceof ArrayCreationExpression value) {
697+
if(!hashElementCollection.isEmpty() && hashElementCollection.get(0).getValue() instanceof ArrayCreationExpression value) {
699698
if(value != null) {
700699
variables.addAll(PhpElementsUtil.getArrayKeyValueMap(value).values());
701700
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/routing/RouteHelperTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ public void testGetRoutesInsideUrlGeneratorFileUrlGeneratorRoutes() {
286286

287287
Route exceptionCss = routes.get("_profiler_exception_css");
288288
assertEquals("/_profiler/{token}/exception.css", exceptionCss.getPath());
289+
290+
Route appHomepage = routes.get("app.homepage");
291+
assertEquals("Company\\Controller\\App\\HomepageController::index", appHomepage.getController());
292+
assertEquals("app.homepage", appHomepage.getName());
293+
294+
Route appHomepage2 = routes.get("app.homepage.2");
295+
assertEquals("Company\\Controller\\App\\HomepageController::index", appHomepage2.getController());
296+
assertEquals("app.homepage.2", appHomepage2.getName());
289297
}
290298

291299
/**

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/routing/fixtures/url_generating_routes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@
3636
'_profiler_router' => [['token'], ['_controller' => 'web_profiler.controller.router::panelAction'], [], [['text', '/router'], ['variable', '/', '[^/]++', 'token', true], ['text', '/_profiler']], [], []],
3737
'_profiler_exception' => [['token'], ['_controller' => 'web_profiler.controller.exception_panel::body'], [], [['text', '/exception'], ['variable', '/', '[^/]++', 'token', true], ['text', '/_profiler']], [], []],
3838
'_profiler_exception_css' => [['token'], ['_controller' => 'web_profiler.controller.exception_panel::stylesheet'], [], [['text', '/exception.css'], ['variable', '/', '[^/]++', 'token', true], ['text', '/_profiler']], [], []],
39+
40+
// canonical routes
41+
'app.homepage.0' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 0, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'app.domain.com']], [], []],
42+
'app.homepage.1' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 1, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'staging.app.domain.com']], [], []],
43+
'app.homepage.2' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 2, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'tests.app.domain.local']], [], []],
44+
'app.homepage.3' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 3, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'app.localhost']], [], []],
3945
];

0 commit comments

Comments
 (0)