OSDN Git Service

fix route regex and router matching
authorfrostbane <frostbane@programmer.net>
Thu, 28 Nov 2019 04:43:40 +0000 (13:43 +0900)
committerfrostbane <frostbane@programmer.net>
Thu, 28 Nov 2019 04:45:40 +0000 (13:45 +0900)
src/Route.php
src/Router.php
tests/src/PHPRouterTest/RouterTest.php

index 14fcf09..30237f0 100755 (executable)
@@ -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-%]+)';
index 09b26a2..7b02103 100755 (executable)
@@ -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)) {
index ae9f54e..0dd1944 100644 (file)
@@ -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()