From: frostbane Date: Thu, 28 Nov 2019 04:43:40 +0000 (+0900) Subject: fix route regex and router matching X-Git-Tag: 2.0.0~2^2 X-Git-Url: http://git.osdn.net/view?p=php-libraries%2FRouter.git;a=commitdiff_plain;h=86617d364d3bc5a9f2b00b5ae92eb2ba8de973c7 fix route regex and router matching --- diff --git a/src/Route.php b/src/Route.php index 14fcf09..30237f0 100755 --- a/src/Route.php +++ b/src/Route.php @@ -133,13 +133,15 @@ class Route public function getRegex() { - return preg_replace_callback('/(:\w+)/', array(&$this, 'substituteFilter'), $this->url); + $url = preg_quote($this->url); + + return preg_replace_callback('/(\\\\(:\w+))/', array(&$this, 'substituteFilter'), $url); } private function substituteFilter($matches) { - if (isset($matches[1], $this->filters[$matches[1]])) { - return $this->filters[$matches[1]]; + if (isset($matches[1], $this->filters[$matches[2]])) { + return $this->filters[$matches[2]]; } return '([\w-%]+)'; diff --git a/src/Router.php b/src/Router.php index 09b26a2..7b02103 100755 --- a/src/Router.php +++ b/src/Router.php @@ -113,6 +113,11 @@ class Router $foundRoute = null; $params = array(); + // must be unit testing + if($currentDir === "." || "..") { + $currentDir = ""; + } + foreach ($this->routes->all() as $routes) { // compare server request method with route's allowed http methods if (!in_array($requestMethod, (array)$routes->getMethods(), true)) { diff --git a/tests/src/PHPRouterTest/RouterTest.php b/tests/src/PHPRouterTest/RouterTest.php index ae9f54e..0dd1944 100644 --- a/tests/src/PHPRouterTest/RouterTest.php +++ b/tests/src/PHPRouterTest/RouterTest.php @@ -116,8 +116,6 @@ class RouterTest extends PHPUnit_Framework_TestCase public function testParamsWithDynamicFilterMatch() { - $this->markTestSkipped("dynamic filter unittest skipped, regex not yet fixed"); - $collection = new RouteCollection(); $route = new Route( '/js/:filename.js', @@ -126,10 +124,12 @@ class RouterTest extends PHPUnit_Framework_TestCase 'methods' => 'GET', ) ); + $route->setFilters(array(':filename' => '([[:alnum:]\.]+)'), true); $collection->attachRoute($route); $router = new Router($collection); + $this->assertEquals( array('filename' => 'someJsFile'), $router->getRoute('/js/someJsFile.js')->getParameters()