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
117 changes: 64 additions & 53 deletions lib/routing/sfRoute.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,67 +200,78 @@ public function matchesParameters($params, $context = array())
return true;
}

/**
* Generates a URL from the given parameters.
*
* @param mixed $params The parameter values
* @param array $context The context
* @param Boolean $absolute Whether to generate an absolute URL
*
* @return string The generated URL
*/
public function generate($params, $context = array(), $absolute = false)
{
if (!$this->compiled)
{
$this->compile();
}
/**
* Generates a URL from the given parameters.
*
* @param mixed $params The parameter values
* @param array $context The context
* @param Boolean $absolute Whether to generate an absolute URL
*
* @return string The generated URL
*/
public function generate($params, $context = [], $absolute = false)
{
if (!$this->compiled) {
$this->compile();
}

$url = $this->pattern;
$url = $this->pattern;
$defaults = $this->mergeArrays($this->getDefaultParameters(), $this->defaults);
$tparams = $this->mergeArrays($defaults, $params);

$defaults = $this->mergeArrays($this->getDefaultParameters(), $this->defaults);
$tparams = $this->mergeArrays($defaults, $params);
// all params must be given
if ($diff = array_diff_key($this->variables, $tparams)) {
throw new InvalidArgumentException(sprintf('The "%s" route has some missing mandatory parameters (%s).', $this->pattern, implode(', ', $diff)));
}

// all params must be given
if ($diff = array_diff_key($this->variables, $tparams))
{
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 ($this->options['generate_shortest_url'] || $this->customToken)
{
$url = $this->generateWithTokens($tparams);
}
else
{
// replace variables
$variables = $this->variables;
uasort($variables, array('sfRoute', 'generateCompareVarsByStrlen'));
foreach ($variables as $variable => $value)
{
$url = str_replace($value, urlencode($tparams[$variable]), $url);
}
if ($absolute) {
$elements[] = 'Absolute';
}

if(!in_array($this->suffix, $this->options['segment_separators']))
{
$url .= $this->suffix;
}
}
$cache_key = SharedCacheHelper::getNamespace($elements);
$cached = SharedCacheHelper::getValue($cache_key);
if ($cached) {
return $cached;
}

// replace extra parameters if the route contains *
$url = $this->generateStarParameter($url, $defaults, $tparams);
if ($this->options['generate_shortest_url'] || $this->customToken) {
$url = $this->generateWithTokens($tparams);
} else {
// replace variables
$variables = $this->variables;
uasort($variables, array('sfRoute', 'generateCompareVarsByStrlen'));
foreach ($variables as $variable => $value) {
$url = str_replace($value, urlencode($tparams[$variable]), $url);
}

if (!in_array($this->suffix, $this->options['segment_separators'])) {
$url .= $this->suffix;
}
}

if ($this->options['extra_parameters_as_query_string'] && !$this->hasStarParameter())
{
// add a query string if needed
if ($extra = array_diff_key($params, $this->variables, $defaults))
{
$url .= '?'.http_build_query($extra);
}
}
// replace extra parameters if the route contains *
$url = $this->generateStarParameter($url, $defaults, $tparams);

return $url;
}
if ($this->options['extra_parameters_as_query_string'] && !$this->hasStarParameter()) {
// add a query string if needed
if ($extra = array_diff_key($params, $this->variables, $defaults)) {
$url .= '?' . http_build_query($extra);
}
}

SharedCacheHelper::setValue(
$cache_key,
$url,
SharedCacheHelper::THREE_MONTHS
);

return $url;
}

static private function generateCompareVarsByStrlen($a, $b)
{
Expand Down