Преглед изворни кода

Merge pull request #545 from Bittarman/fix/chain-delimiter-stripping

Fixed path delimeters being stripped by chain routes affecting later routes
Frank Brückner пре 10 година
родитељ
комит
ee46e80698

+ 3 - 1
library/Zend/Controller/Router/Route/Chain.php

@@ -86,6 +86,7 @@ class Zend_Controller_Router_Route_Chain extends Zend_Controller_Router_Route_Ab
      */
     public function match($request, $partial = null)
     {
+        $rawPath     = $request->getPathInfo();
         $path        = trim($request->getPathInfo(), self::URI_DELIMITER);
         $subPath     = $path;
         $values      = array();
@@ -100,6 +101,7 @@ class Zend_Controller_Router_Route_Chain extends Zend_Controller_Router_Route_Ab
                 $separator = substr($subPath, 0, strlen($this->_separators[$key]));
 
                 if ($separator !== $this->_separators[$key]) {
+                    $request->setPathInfo($rawPath);
                     return false;
                 }
 
@@ -116,7 +118,7 @@ class Zend_Controller_Router_Route_Chain extends Zend_Controller_Router_Route_Ab
             $res = $route->match($match, true);
 
             if ($res === false) {
-                $request->setPathInfo($path);
+                $request->setPathInfo($rawPath);
                 return false;
             }
 

+ 67 - 7
tests/Zend/Controller/Router/Route/ChainTest.php

@@ -840,6 +840,30 @@ class Zend_Controller_Router_Route_ChainTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @throws Zend_Controller_Router_Exception
+     */
+    public function testMultipleChainsResettingPathInfoInSegmentBlock()
+    {
+        $foo = new Zend_Controller_Router_Route_SubclassTest('notfoo');
+        $bar = new Zend_Controller_Router_Route_SubclassTest('bar', array('baz' => 'no'));
+
+
+        $chain = $foo->chain($bar);
+
+        $static = new Zend_Controller_Router_Route_SimpleSubclassTest('/foo', array('foo' => 'foo'));
+
+        $rewrite = new Zend_Controller_Router_Rewrite();
+        $rewrite->addRoute('static', $static); // First In Last Out, we want this to be matched against second
+        $rewrite->addRoute('chain', $chain);
+        $request = new Zend_Controller_Router_ChainTest_Request('http://www.zend.com/foo');
+
+        $res = $rewrite->route($request);
+        $this->assertEquals('foo', $res->getParam('foo'), 'Route did not match');
+        $this->assertEquals('static', $rewrite->getCurrentRouteName(), 'Routing did not match expected route');
+
+    }
+
+    /**
      * @group ZF-11443
      */
     public function testGetDefault()
@@ -958,13 +982,53 @@ class Zend_Controller_Router_Route_ChainTest extends PHPUnit_Framework_TestCase
     }
 }
 
+class Zend_Controller_Router_Route_SimpleSubclassTest extends Zend_Controller_Router_Route_Abstract
+{
+    /**
+     * @var string
+     */
+    protected $path;
+
+    /**
+     * @var array
+     */
+    protected $params = array();
+
+    public function __construct($path, $params)
+    {
+        $this->path = $path;
+        $this->params = $params;
+    }
+
+    public function match($path, $partial = false)
+    {
+        $path = $path->getPathInfo();
+        if ($path == $this->path) {
+            $this->setMatchedPath($this->path);
+            return $this->params;
+        }
+        return false;
+    }
+
+    public function getVersion()
+    {
+        return 2;
+    }
+
+    public function assemble($data = array(), $reset = false, $encode = false)
+    {}
+
+    public static function getInstance(Zend_Config $config)
+    {}
+
+}
 
 class Zend_Controller_Router_Route_SubclassTest extends Zend_Controller_Router_Route_Static
 {
     public function match($path, $partial = false)
     {
         $path = $path->getPathInfo();
-        $match = parent::match($path, $partial); // TODO: Change the autogenerated stub
+        $match = parent::match($path, $partial);
         if (is_array($match)) {
             $this->setMatchedPath($this->_route);
         }
@@ -977,14 +1041,10 @@ class Zend_Controller_Router_Route_SubclassTest extends Zend_Controller_Router_R
     }
 
     public function assemble($data = array(), $reset = false, $encode = false)
-    {
-        // TODO: Implement assemble() method.
-    }
+    {}
 
     public static function getInstance(Zend_Config $config)
-    {
-        // TODO: Implement getInstance() method.
-    }
+    {}
 
 }