Skip to content

Commit

Permalink
Merge pull request #2268 from Haehnchen/feature/2177-tests-fixes-routes
Browse files Browse the repository at this point in the history
#2177 #2266 add canonical routes to index: test and optimize
  • Loading branch information
Haehnchen authored Dec 14, 2023
2 parents b2f824d + 5628749 commit 8588227
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,13 @@ public static Map<String, Route> 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;
Expand Down Expand Up @@ -621,7 +620,7 @@ private static void collectRoutesOnArrayCreation(@NotNull Map<String, Route> rou
@NotNull
private static Route convertRouteConfigForReturnArray(@NotNull String routeName, @NotNull List<ArrayCreationExpression> hashElementCollection) {
Set<String> 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));
Expand Down Expand Up @@ -664,9 +663,9 @@ private static Route convertRouteConfigForReturnArray(@NotNull String routeName,

List<String> 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));
}

Expand All @@ -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());
}

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

Set<String> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']], [], []],
];

0 comments on commit 8588227

Please sign in to comment.