Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions lib/routing/sfRoute.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,7 @@ public function generate($params, $context = [], $absolute = false)
throw new InvalidArgumentException(sprintf('The "%s" route has some missing mandatory parameters (%s).', $this->pattern, implode(', ', $diff)));
}

$elements = array_merge(
[SharedCacheHelper::ROUTING_NAMESPACE],
$tparams
);

if ($absolute) {
$elements[] = 'Absolute';
}

$cache_key = SharedCacheHelper::getNamespace($elements);
$cache_key = $this->generateCacheKey($tparams, $absolute);
$cached = SharedCacheHelper::getValue($cache_key);
if ($cached) {
return $cached;
Expand Down Expand Up @@ -273,6 +264,45 @@ public function generate($params, $context = [], $absolute = false)
return $url;
}

/**
* Generates a consistent cache key for a route
*/
protected function generateCacheKey(array $elements = [], bool $absolute = false): string
{
$elements = [
SharedCacheHelper::ROUTING_NAMESPACE,
$this->flattenArrayElements($elements),
];

if ($absolute) {
$elements[] = 'Absolute';
}

return SharedCacheHelper::getNamespace($elements);
}

/**
* Recursive method to pack a key => value pair array down into a flat
* numerically-indexed array, with a consistent ordering of elements.
*/
protected function flattenArrayElements(array $elements = []): array
{
$flattened = [];
ksort($elements);
foreach ($elements as $key => $value) {
$flattened[] = $key;

if (is_array($value)) {
$flattened[] = $this->flattenArrayElements($value);
continue;
}

$flattened[] = $value;
}

return $flattened;
}

static private function generateCompareVarsByStrlen($a, $b)
{
return strlen($a) < strlen($b);
Expand Down