Jelajahi Sumber

[Routing] removed the normalizeUrl() method and renamed url to pathinfo as this is more correct

Fabien Potencier 14 tahun lalu
induk
melakukan
17ef911f19

+ 2 - 2
src/Symfony/Component/Routing/Matcher/ApacheUrlMatcher.php

@@ -51,11 +51,11 @@ class ApacheUrlMatcher extends UrlMatcher
      *
      * Returns false if no route matches the URL.
      *
-     * @param  string $url URL to be parsed
+     * @param  string $pathinfo The pathinfo to be parsed
      *
      * @return array|false An array of parameters or false if no route matches
      */
-    public function match($url)
+    public function match($pathinfo)
     {
         if (!isset($_SERVER['_ROUTING__route'])) {
             // fall-back to the default UrlMatcher

+ 8 - 10
src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

@@ -63,25 +63,25 @@ class PhpMatcherDumper extends MatcherDumper
             $hasTrailingSlash = false;
             if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', $compiledRoute->getRegex(), $m)) {
                 if (substr($m['url'], -1) === '/') {
-                    $conditions[] = sprintf("rtrim(\$url, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
+                    $conditions[] = sprintf("rtrim(\$pathinfo, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
                     $hasTrailingSlash = true;
                 } else {
-                    $conditions[] = sprintf("\$url === '%s'", str_replace('\\', '', $m['url']));
+                    $conditions[] = sprintf("\$pathinfo === '%s'", str_replace('\\', '', $m['url']));
                 }
 
                 $matches = 'array()';
             } else {
                 if ($compiledRoute->getStaticPrefix()) {
-                    $conditions[] = sprintf("0 === strpos(\$url, '%s')", $compiledRoute->getStaticPrefix());
+                    $conditions[] = sprintf("0 === strpos(\$pathinfo, '%s')", $compiledRoute->getStaticPrefix());
                 }
 
                 $regex = $compiledRoute->getRegex();
                 if ($pos = strpos($regex, '/$')) {
                     $regex = substr($regex, 0, $pos) . '/?$' . substr($regex, $pos+2);
-                    $conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $regex);
+                    $conditions[] = sprintf("preg_match('%s', \$pathinfo, \$matches)", $regex);
                     $hasTrailingSlash = true;
                 } else {
-                    $conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $regex);
+                    $conditions[] = sprintf("preg_match('%s', \$pathinfo, \$matches)", $regex);
                 }
 
                 $matches = '$matches';
@@ -95,8 +95,8 @@ EOF;
 
             if ($hasTrailingSlash) {
                 $code[] = sprintf(<<<EOF
-            if (substr(\$url, -1) !== '/') {
-                return array('_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction', 'url' => \$this->context['base_url'].\$url.'/', 'permanent' => true, '_route' => '%s');
+            if (substr(\$pathinfo, -1) !== '/') {
+                return array('_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction', 'url' => \$this->context['base_url'].\$pathinfo.'/', 'permanent' => true, '_route' => '%s');
             }
 EOF
             , $name);
@@ -114,10 +114,8 @@ EOF
 
         return <<<EOF
 
-    public function match(\$url)
+    public function match(\$pathinfo)
     {
-        \$url = \$this->normalizeUrl(\$url);
-
 $code
         return false;
     }

+ 4 - 16
src/Symfony/Component/Routing/Matcher/UrlMatcher.php

@@ -54,14 +54,12 @@ class UrlMatcher implements UrlMatcherInterface
      *
      * Returns false if no route matches the URL.
      *
-     * @param  string $url URL to be parsed
+     * @param  string $pathinfo The path info to be parsed
      *
      * @return array|false An array of parameters or false if no route matches
      */
-    public function match($url)
+    public function match($pathinfo)
     {
-        $url = $this->normalizeUrl($url);
-
         foreach ($this->routes->all() as $name => $route) {
             $compiledRoute = $route->compile();
 
@@ -72,11 +70,11 @@ class UrlMatcher implements UrlMatcherInterface
             }
 
             // check the static prefix of the URL first. Only use the more expensive preg_match when it matches
-            if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($url, $compiledRoute->getStaticPrefix())) {
+            if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($pathinfo, $compiledRoute->getStaticPrefix())) {
                 continue;
             }
 
-            if (!preg_match($compiledRoute->getRegex(), $url, $matches)) {
+            if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
                 continue;
             }
 
@@ -98,14 +96,4 @@ class UrlMatcher implements UrlMatcherInterface
 
         return $parameters;
     }
-
-    protected function normalizeUrl($url)
-    {
-        // remove the query string
-        if (false !== $pos = strpos($url, '?')) {
-            $url = substr($url, 0, $pos);
-        }
-
-        return $url;
-    }
 }

+ 2 - 2
src/Symfony/Component/Routing/Matcher/UrlMatcherInterface.php

@@ -23,9 +23,9 @@ interface UrlMatcherInterface
      *
      * Returns false if no route matches the URL.
      *
-     * @param  string $url URL to be parsed
+     * @param  string $pathinfo The path info to be parsed
      *
      * @return array|false An array of parameters or false if no route matches
      */
-    function match($url);
+    function match($pathinfo);
 }

+ 11 - 13
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php

@@ -17,36 +17,34 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
         $this->defaults = $defaults;
     }
 
-    public function match($url)
+    public function match($pathinfo)
     {
-        $url = $this->normalizeUrl($url);
-
-        if (0 === strpos($url, '/foo') && preg_match('#^/foo/(?P<bar>baz|symfony)$#x', $url, $matches)) {
+        if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?P<bar>baz|symfony)$#x', $pathinfo, $matches)) {
             return array_merge($this->mergeDefaults($matches, array (  'def' => 'test',)), array('_route' => 'foo'));
         }
 
-        if (isset($this->context['method']) && preg_match('#^(GET|head)$#xi', $this->context['method']) && 0 === strpos($url, '/bar') && preg_match('#^/bar/(?P<foo>[^/\.]+?)$#x', $url, $matches)) {
+        if (isset($this->context['method']) && preg_match('#^(GET|head)$#xi', $this->context['method']) && 0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/\.]+?)$#x', $pathinfo, $matches)) {
             return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'bar'));
         }
 
-        if ($url === '/test/baz') {
+        if ($pathinfo === '/test/baz') {
             return array_merge($this->mergeDefaults(array(), array ()), array('_route' => 'baz'));
         }
 
-        if ($url === '/test/baz.html') {
+        if ($pathinfo === '/test/baz.html') {
             return array_merge($this->mergeDefaults(array(), array ()), array('_route' => 'baz2'));
         }
 
-        if (rtrim($url, '/') === '/test/baz3') {
-            if (substr($url, -1) !== '/') {
-                return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$url.'/', 'permanent' => true, '_route' => 'baz3');
+        if (rtrim($pathinfo, '/') === '/test/baz3') {
+            if (substr($pathinfo, -1) !== '/') {
+                return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$pathinfo.'/', 'permanent' => true, '_route' => 'baz3');
             }
             return array_merge($this->mergeDefaults(array(), array ()), array('_route' => 'baz3'));
         }
 
-        if (0 === strpos($url, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/?$#x', $url, $matches)) {
-            if (substr($url, -1) !== '/') {
-                return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$url.'/', 'permanent' => true, '_route' => 'baz4');
+        if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/?$#x', $pathinfo, $matches)) {
+            if (substr($pathinfo, -1) !== '/') {
+                return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$pathinfo.'/', 'permanent' => true, '_route' => 'baz4');
             }
             return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'baz4'));
         }

+ 0 - 18
tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php

@@ -17,16 +17,6 @@ use Symfony\Component\Routing\Route;
 
 class UrlMatcherTest extends \PHPUnit_Framework_TestCase
 {
-    public function testNormalizeUrl()
-    {
-        $collection = new RouteCollection();
-        $collection->add('foo', new Route('/{foo}'));
-
-        $matcher = new UrlMatcherForTests($collection, array(), array());
-
-        $this->assertEquals('/foo', $matcher->normalizeUrl('/foo?foo=bar'), '->normalizeUrl() removes the query string');
-    }
-
     public function testMatch()
     {
       // test the patterns are matched are parameters are returned
@@ -61,11 +51,3 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
       $this->assertNotEquals(false, $matcher->match('/foo'));
     }
 }
-
-class UrlMatcherForTests extends UrlMatcher
-{
-    public function normalizeUrl($url)
-    {
-        return parent::normalizeUrl($url);
-    }
-}