Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. CompiledUrlMatcherDumper.php

function CompiledUrlMatcherDumper::groupStaticRoutes

Splits static routes from dynamic routes, so that they can be matched first, using a simple switch.

1 call to CompiledUrlMatcherDumper::groupStaticRoutes()
CompiledUrlMatcherDumper::getCompiledRoutes in vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherDumper.php
Generates the arrays for CompiledUrlMatcher's constructor.

File

vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherDumper.php, line 164

Class

CompiledUrlMatcherDumper
CompiledUrlMatcherDumper creates PHP arrays to be used with CompiledUrlMatcher.

Namespace

Symfony\Component\Routing\Matcher\Dumper

Code

private function groupStaticRoutes(RouteCollection $collection) : array {
    $staticRoutes = $dynamicRegex = [];
    $dynamicRoutes = new RouteCollection();
    foreach ($collection->all() as $name => $route) {
        $compiledRoute = $route->compile();
        $staticPrefix = rtrim($compiledRoute->getStaticPrefix(), '/');
        $hostRegex = $compiledRoute->getHostRegex();
        $regex = $compiledRoute->getRegex();
        if ($hasTrailingSlash = '/' !== $route->getPath()) {
            $pos = strrpos($regex, '$');
            $hasTrailingSlash = '/' === $regex[$pos - 1];
            $regex = substr_replace($regex, '/?$', $pos - $hasTrailingSlash, 1 + $hasTrailingSlash);
        }
        if (!$compiledRoute->getPathVariables()) {
            $host = !$compiledRoute->getHostVariables() ? $route->getHost() : '';
            $url = $route->getPath();
            if ($hasTrailingSlash) {
                $url = substr($url, 0, -1);
            }
            foreach ($dynamicRegex as [
                $hostRx,
                $rx,
                $prefix,
            ]) {
                if (('' === $prefix || str_starts_with($url, $prefix)) && (preg_match($rx, $url) || preg_match($rx, $url . '/')) && (!$host || !$hostRx || preg_match($hostRx, $host))) {
                    $dynamicRegex[] = [
                        $hostRegex,
                        $regex,
                        $staticPrefix,
                    ];
                    $dynamicRoutes->add($name, $route);
                    continue 2;
                }
            }
            $staticRoutes[$url][$name] = [
                $route,
                $hasTrailingSlash,
            ];
        }
        else {
            $dynamicRegex[] = [
                $hostRegex,
                $regex,
                $staticPrefix,
            ];
            $dynamicRoutes->add($name, $route);
        }
    }
    return [
        $staticRoutes,
        $dynamicRoutes,
    ];
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal