Skip to content

Commit

Permalink
[refactor]: computed/converted renames
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Anthony committed Sep 19, 2013
1 parent 53cd56a commit 7cea479
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions Redirector/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ public function handleRedirects()
}

// Convert the wildcards into expressions for replacement
$convertedWilcards = preg_replace_callback($pattern, function ($captures) use ($self) {
$computedWildcards = preg_replace_callback($pattern, function ($captures) use ($self) {
$self->computedReplacements[] = $captures[1];

return '(' . $self->wildcards[$captures[2]] . ')';
}, $self->source);

// Check to see if we have these conversions in the requested path and replace where necessary
if (preg_match("~^$convertedWilcards$~i", $requestedPath)) {
$convertedReplacements = preg_replace_callback("~^$convertedWilcards$~i", function ($captures) use ($self) {
if (preg_match("~^$computedWildcards$~i", $requestedPath)) {
$convertedWildcards = preg_replace_callback("~^$computedWildcards$~i", function ($captures) use ($self) {
$result = $self->destination;
for ($c = 1, $n = count($captures); $c < $n; ++$c) {
$value = array_shift($self->computedReplacements);
Expand All @@ -266,7 +266,7 @@ public function handleRedirects()
// Replace variables with actual data
foreach ($self->variables as $variable => $data) {
$self->assert(preg_match('~^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$~', $variable), 'inavlid_variable_declaration');
$convertedReplacements = str_replace("{@$variable}", ltrim($data, '/'), $convertedReplacements);
$convertedWildcards = str_replace("{@$variable}", ltrim($data, '/'), $convertedWildcards);
}

// Check for Just In Time replacements and apply where necessary
Expand All @@ -283,18 +283,18 @@ public function handleRedirects()
}
// Match and replace
$jitMatcher = "~$jitReplace~i";
if (preg_match($jitMatcher, $convertedReplacements)) {
$convertedReplacements = preg_replace($jitMatcher, trim($jitWith, '/'), $convertedReplacements);
if (preg_match($jitMatcher, $convertedWildcards)) {
$convertedWildcards = preg_replace($jitMatcher, trim($jitWith, '/'), $convertedWildcards);
}
}

// Check for un-processed variables and throw an exception if there are any
if (preg_match('~\{@(.*)\}~', $convertedReplacements, $variableMatches)) {
$self->except(sprintf($self->errors['missing_variables'], $variableMatches[1], $convertedReplacements));
if (preg_match('~\{@(.*)\}~', $convertedWildcards, $variableMatches)) {
$self->except(sprintf($self->errors['missing_variables'], $variableMatches[1], $convertedWildcards));
}

// Redirect the user to the final, processed path
return $app->redirect(strtolower("{$self->prefix}{$convertedReplacements}{$self->sourceQueryString}"), 301);
return $app->redirect(strtolower("{$self->prefix}{$convertedWildcards}{$self->sourceQueryString}"), 301);
}
}
}, Silex::EARLY_EVENT);
Expand Down

0 comments on commit 7cea479

Please sign in to comment.