Skip to content

Commit

Permalink
[Routing] Fix syntax error when dumping routes with single quotes in …
Browse files Browse the repository at this point in the history
…the requirements or pattern
  • Loading branch information
Seldaek committed Jun 29, 2011
1 parent 2b5e22d commit 418d6a0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
Expand Up @@ -114,7 +114,7 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections, $
}

if ($prefix !== $parentPrefix) {
$code[] = sprintf(" if (0 === strpos(\$pathinfo, '%s')) {", $prefix);
$code[] = sprintf(" if (0 === strpos(\$pathinfo, %s)) {", var_export($prefix, true));
$indent = ' ';
}
}
Expand Down Expand Up @@ -151,22 +151,22 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
$matches = false;
if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', str_replace(array("\n", ' '), '', $compiledRoute->getRegex()), $m)) {
if ($supportsRedirections && substr($m['url'], -1) === '/') {
$conditions[] = sprintf("rtrim(\$pathinfo, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
$conditions[] = sprintf("rtrim(\$pathinfo, '/') === %s", var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true));
$hasTrailingSlash = true;
} else {
$conditions[] = sprintf("\$pathinfo === '%s'", str_replace('\\', '', $m['url']));
$conditions[] = sprintf("\$pathinfo === %s", var_export(str_replace('\\', '', $m['url']), true));
}
} else {
if ($compiledRoute->getStaticPrefix() && $compiledRoute->getStaticPrefix() != $parentPrefix) {
$conditions[] = sprintf("0 === strpos(\$pathinfo, '%s')", $compiledRoute->getStaticPrefix());
$conditions[] = sprintf("0 === strpos(\$pathinfo, %s)", var_export($compiledRoute->getStaticPrefix(), true));
}

$regex = str_replace(array("\n", ' '), '', $compiledRoute->getRegex());
if ($supportsRedirections && $pos = strpos($regex, '/$')) {
$regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2);
$hasTrailingSlash = true;
}
$conditions[] = sprintf("preg_match('%s', \$pathinfo, \$matches)", $regex);
$conditions[] = sprintf("preg_match(%s, \$pathinfo, \$matches)", var_export($regex, true));

$matches = true;
}
Expand Down
Expand Up @@ -100,28 +100,34 @@ public function match($pathinfo)
return array ( 'def' => 'test', '_route' => 'foofoo',);
}

// quoter
if (preg_match('#^/(?P<quoter>[\']+)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'quoter';
return $matches;
}

if (0 === strpos($pathinfo, '/a')) {
if (0 === strpos($pathinfo, '/a/b')) {
if (0 === strpos($pathinfo, '/a/b\'b')) {
// foo
if (preg_match('#^/a/b/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
if (preg_match('#^/a/b\'b/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'foo';
return $matches;
}

// bar
if (preg_match('#^/a/b/(?P<bar>[^/]+?)$#x', $pathinfo, $matches)) {
if (preg_match('#^/a/b\'b/(?P<bar>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'bar';
return $matches;
}

// foo1
if (preg_match('#^/a/b/(?P<foo1>[^/]+?)$#x', $pathinfo, $matches)) {
if (preg_match('#^/a/b\'b/(?P<foo1>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'foo1';
return $matches;
}

// bar1
if (preg_match('#^/a/b/(?P<bar1>[^/]+?)$#x', $pathinfo, $matches)) {
if (preg_match('#^/a/b\'b/(?P<bar1>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'bar1';
return $matches;
}
Expand Down
Expand Up @@ -112,28 +112,34 @@ public function match($pathinfo)
return array ( 'def' => 'test', '_route' => 'foofoo',);
}

// quoter
if (preg_match('#^/(?P<quoter>[\']+)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'quoter';
return $matches;
}

if (0 === strpos($pathinfo, '/a')) {
if (0 === strpos($pathinfo, '/a/b')) {
if (0 === strpos($pathinfo, '/a/b\'b')) {
// foo
if (preg_match('#^/a/b/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
if (preg_match('#^/a/b\'b/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'foo';
return $matches;
}

// bar
if (preg_match('#^/a/b/(?P<bar>[^/]+?)$#x', $pathinfo, $matches)) {
if (preg_match('#^/a/b\'b/(?P<bar>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'bar';
return $matches;
}

// foo1
if (preg_match('#^/a/b/(?P<foo1>[^/]+?)$#x', $pathinfo, $matches)) {
if (preg_match('#^/a/b\'b/(?P<foo1>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'foo1';
return $matches;
}

// bar1
if (preg_match('#^/a/b/(?P<bar1>[^/]+?)$#x', $pathinfo, $matches)) {
if (preg_match('#^/a/b\'b/(?P<bar1>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'bar1';
return $matches;
}
Expand Down
Expand Up @@ -73,17 +73,23 @@ public function testDump()
'/foofoo',
array('def' => 'test')
));
// pattern with quotes
$collection->add('quoter', new Route(
'/{quoter}',
array(),
array('quoter' => '[\']+')
));

// prefixes
$collection1 = new RouteCollection();
$collection1->add('foo', new Route('/{foo}'));
$collection1->add('bar', new Route('/{bar}'));
$collection2 = new RouteCollection();
$collection2->addCollection($collection1, '/b');
$collection2->addCollection($collection1, '/b\'b');
$collection1 = new RouteCollection();
$collection1->add('foo1', new Route('/{foo1}'));
$collection1->add('bar1', new Route('/{bar1}'));
$collection2->addCollection($collection1, '/b');
$collection2->addCollection($collection1, '/b\'b');
$collection->addCollection($collection2, '/a');

// "dynamic" prefix
Expand Down

0 comments on commit 418d6a0

Please sign in to comment.