function RouteProvider::getQueryParametersCacheIdPart

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::getQueryParametersCacheIdPart()

Returns the query parameters identifier for the route collection cache.

The query parameters on the request may be altered programmatically, e.g. while serving private files or in subrequests. As such, we must vary on both the query string from the client and the parameter bag after incoming route processors have modified the request object.

Parameters

\Symfony\Component\HttpFoundation\Request $request: Request.

Return value

string

1 call to RouteProvider::getQueryParametersCacheIdPart()
RouteProvider::getRouteCollectionCacheId in core/lib/Drupal/Core/Routing/RouteProvider.php
Returns the cache ID for the route collection cache.

File

core/lib/Drupal/Core/Routing/RouteProvider.php, line 482

Class

RouteProvider
A Route Provider front-end for all Drupal-stored routes.

Namespace

Drupal\Core\Routing

Code

protected function getQueryParametersCacheIdPart(Request $request) {
    // @todo Use \Symfony\Component\HttpFoundation\Request::normalizeQueryString
    //   for recursive key ordering if support is added in the future.
    $recursive_sort = function (&$array) use (&$recursive_sort) {
        foreach ($array as &$v) {
            if (is_array($v)) {
                $recursive_sort($v);
            }
        }
        ksort($array);
    };
    // Recursively normalize the query parameters to ensure maximal cache hits.
    // If we did not normalize the order, functionally identical query string
    // sets could be sent in differing order creating a potential DoS vector
    // and decreasing cache hit rates.
    $sorted_resolved_parameters = $request->query
        ->all();
    $recursive_sort($sorted_resolved_parameters);
    $sorted_original_parameters = Request::create('/?' . $request->getQueryString())->query
        ->all();
    $recursive_sort($sorted_original_parameters);
    // Hash this portion to help shorten the total key length.
    $resolved_hash = $sorted_resolved_parameters ? sha1(http_build_query($sorted_resolved_parameters)) : NULL;
    return implode(',', array_filter([
        http_build_query($sorted_original_parameters),
        $resolved_hash,
    ]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.