summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Router/Routes/Glob.php37
-rw-r--r--tests/Router/Routes/GlobTest.php9
2 files changed, 29 insertions, 17 deletions
diff --git a/src/Router/Routes/Glob.php b/src/Router/Routes/Glob.php
index a4495ee..9e96a2f 100644
--- a/src/Router/Routes/Glob.php
+++ b/src/Router/Routes/Glob.php
@@ -8,6 +8,7 @@ use Jasny\Router\Routes;
use Jasny\Router\Route;
use Jasny\Router\Routes\RouteBinding;
use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Message\UriInterface;
/**
* Match URL against a shell wildcard pattern.
@@ -126,32 +127,38 @@ class Glob extends ArrayObject implements Routes
return (boolean)preg_match("~^{$regex}$~", $url);
}
+ protected function splitRoutePattern($pattern)
+ {
+ if (strpos($pattern, ' ') !== false && preg_match_all('/\s+\+(\w+)\b|\s+\-(\w+)\b/', $pattern, $matches)) {
+ list($path) = preg_split('/\s+/', $pattern, 2);
+ $inc = isset($matches[1]) ? array_filter($matches[1]) : [];
+ $excl = isset($matches[2]) ? array_filter($matches[2]) : [];
+ } else {
+ $path = $pattern;
+ $inc = [];
+ $excl = [];
+ }
+
+ return [$path, $inc, $excl];
+ }
+
/**
* Find a matching route
*
- * @param string $url
- * @param string $method
+ * @param UriInterface $url
+ * @param string $method
* @return string
*/
- protected function findRoute($url, $method = null)
+ protected function findRoute(UriInterface $url, $method = null)
{
- $url = $this->cleanUrl($url);
+ $urlPath = $this->cleanUrl($url->getPath());
$ret = null;
foreach ($this as $pattern => $route) {
- if (strpos($pattern, ' ') !== false && preg_match_all('/\s+\+(\w+)\b|\s+\-(\w+)\b/', $pattern, $matches)) {
- list($path) = preg_split('/\s+/', $pattern, 2);
- $inc = isset($matches[1]) ? array_filter($matches[1]) : [];
- $excl = isset($matches[2]) ? array_filter($matches[2]) : [];
- } else {
- $path = $pattern;
- $inc = [];
- $excl = [];
- }
-
+ list($path, $inc, $excl) = $this->splitRoutePattern($pattern);
if ($path !== '/') $path = rtrim($path, '/');
- if ($this->fnmatch($path, $url)) {
+ if ($this->fnmatch($path, $urlPath)) {
if (!$method || ((empty($inc) || in_array($method, $inc)) && !in_array($method, $excl))) {
$ret = $route;
break;
diff --git a/tests/Router/Routes/GlobTest.php b/tests/Router/Routes/GlobTest.php
index 5efa77f..0e5253c 100644
--- a/tests/Router/Routes/GlobTest.php
+++ b/tests/Router/Routes/GlobTest.php
@@ -5,6 +5,7 @@ namespace Jasny\Router\Routes;
use Jasny\Router\Routes\Glob;
use Jasny\Router\Route;
use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Message\UriInterface;
use ArrayObject;
use BadMethodCallException;
@@ -278,7 +279,7 @@ class GlobTest extends \PHPUnit_Framework_TestCase
$this->assertTrue((bool)$match, "Found no match of uri with patterns");
$this->assertEquals($values[$match]['controller'], $route->controller, "False route obtained");
}
-
+
/**
* Provide data for creating ServerRequestInterface objects
*/
@@ -338,8 +339,12 @@ class GlobTest extends \PHPUnit_Framework_TestCase
*/
public function getServerRequest($uri, $method = 'GET', $globals = [], $header = '')
{
+ $uriMock = $this->createMock(UriInterface::class);
+ $uriMock->method('__toString')->willReturn("http://www.example.com" . $uri);
+ $uriMock->method('getPath')->willReturn($uri);
+
$request = $this->createMock(ServerRequestInterface::class);
- $request->method('getUri')->willReturn($uri);
+ $request->method('getUri')->willReturn($uriMock);
$request->method('getMethod')->willReturn($method);
$request->method('getQueryParams')->willReturn(isset($globals['get']) ? $globals['get'] : []);
$request->method('getParsedBody')->willReturn(isset($globals['post']) ? $globals['post'] : []);