diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/routing/RouteHelper.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/routing/RouteHelper.java index fb392caa0..0f882b22b 100644 --- a/src/main/java/fr/adrienbrault/idea/symfony2plugin/routing/RouteHelper.java +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/routing/RouteHelper.java @@ -514,14 +514,13 @@ public static Map getRoutesInsideUrlGeneratorFile(@NotNull PsiFil routes.put(routeArray.getKey(), route); for (ArrayCreationExpression expression : routeArrayOptions) { - for (ArrayHashElement e : expression.getHashElements()) { - PhpPsiElement key = e.getKey(); - if (key != null && "'_canonical_route'".equals(key.getText())) { - PhpPsiElement value = e.getValue(); - if (value != null) { - String canonical = value.getText().replace("'", ""); - if (!routes.containsKey(canonical)) { - routes.put(canonical, route); + for (ArrayHashElement arrayHashElement : expression.getHashElements()) { + if (arrayHashElement.getKey() instanceof StringLiteralExpression stringLiteralExpression && "_canonical_route".equals(stringLiteralExpression.getContents())) { + if (arrayHashElement.getValue() instanceof StringLiteralExpression literalExpression) { + String canonical = literalExpression.getContents(); + if (!canonical.isBlank() && !routes.containsKey(canonical)) { + Route routeCanonical = convertRouteConfigForReturnArray(canonical, routeArrayOptions); + routes.put(canonical, routeCanonical); } } break; @@ -621,7 +620,7 @@ private static void collectRoutesOnArrayCreation(@NotNull Map rou @NotNull private static Route convertRouteConfigForReturnArray(@NotNull String routeName, @NotNull List hashElementCollection) { Set variables = new HashSet<>(); - if(hashElementCollection.size() >= 1 && hashElementCollection.get(0) != null) { + if(!hashElementCollection.isEmpty() && hashElementCollection.get(0) != null) { ArrayCreationExpression value = hashElementCollection.get(0); if(value != null) { variables.addAll(PhpElementsUtil.getArrayValuesAsString(value)); @@ -664,9 +663,9 @@ private static Route convertRouteConfigForReturnArray(@NotNull String routeName, List collect = foo.stream() .map(psiElement -> psiElement.getFirstChild() instanceof StringLiteralExpression ? ((StringLiteralExpression) psiElement.getFirstChild()).getContents() : null) - .collect(Collectors.toList()); + .toList(); - if (collect.size() > 0) { + if (!collect.isEmpty()) { path.append(collect.get(1)); } @@ -683,7 +682,7 @@ private static Route convertRouteConfigForReturnArray(@NotNull String routeName, } // hostTokens = 4 need them? - return new Route(routeName, variables, defaults, requirements, tokens, (path.length() == 0) ? null : path.toString()); + return new Route(routeName, variables, defaults, requirements, tokens, (path.isEmpty()) ? null : path.toString()); } /** @@ -695,7 +694,7 @@ private static Route convertRouteConfig(@NotNull String routeName, @NotNull Arra hashValue.getHashElements().forEach(hashElementCollection::add); Set variables = new HashSet<>(); - if(hashElementCollection.size() >= 1 && hashElementCollection.get(0).getValue() instanceof ArrayCreationExpression value) { + if(!hashElementCollection.isEmpty() && hashElementCollection.get(0).getValue() instanceof ArrayCreationExpression value) { if(value != null) { variables.addAll(PhpElementsUtil.getArrayKeyValueMap(value).values()); } diff --git a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/routing/RouteHelperTest.java b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/routing/RouteHelperTest.java index c3238cb5e..fa0f058fc 100644 --- a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/routing/RouteHelperTest.java +++ b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/routing/RouteHelperTest.java @@ -286,6 +286,14 @@ public void testGetRoutesInsideUrlGeneratorFileUrlGeneratorRoutes() { Route exceptionCss = routes.get("_profiler_exception_css"); assertEquals("/_profiler/{token}/exception.css", exceptionCss.getPath()); + + Route appHomepage = routes.get("app.homepage"); + assertEquals("Company\\Controller\\App\\HomepageController::index", appHomepage.getController()); + assertEquals("app.homepage", appHomepage.getName()); + + Route appHomepage2 = routes.get("app.homepage.2"); + assertEquals("Company\\Controller\\App\\HomepageController::index", appHomepage2.getController()); + assertEquals("app.homepage.2", appHomepage2.getName()); } /** diff --git a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/routing/fixtures/url_generating_routes.php b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/routing/fixtures/url_generating_routes.php index c963f4563..3f38a42ed 100644 --- a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/routing/fixtures/url_generating_routes.php +++ b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/routing/fixtures/url_generating_routes.php @@ -36,4 +36,10 @@ '_profiler_router' => [['token'], ['_controller' => 'web_profiler.controller.router::panelAction'], [], [['text', '/router'], ['variable', '/', '[^/]++', 'token', true], ['text', '/_profiler']], [], []], '_profiler_exception' => [['token'], ['_controller' => 'web_profiler.controller.exception_panel::body'], [], [['text', '/exception'], ['variable', '/', '[^/]++', 'token', true], ['text', '/_profiler']], [], []], '_profiler_exception_css' => [['token'], ['_controller' => 'web_profiler.controller.exception_panel::stylesheet'], [], [['text', '/exception.css'], ['variable', '/', '[^/]++', 'token', true], ['text', '/_profiler']], [], []], + + // canonical routes + 'app.homepage.0' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 0, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'app.domain.com']], [], []], + 'app.homepage.1' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 1, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'staging.app.domain.com']], [], []], + 'app.homepage.2' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 2, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'tests.app.domain.local']], [], []], + 'app.homepage.3' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 3, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'app.localhost']], [], []], ];