summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorminstel <minstel@yandex.ru>2016-10-19 14:43:27 +0300
committerminstel <minstel@yandex.ru>2016-10-19 14:43:27 +0300
commitf55bccf1c0fb060f548db7838c533755f21f493d (patch)
treee909698f4bfa46433b3270f3a7b20b4203418c91 /src
parentea5556fe7059fe07bd6fe85c9a656f637d04309b (diff)
downloadrouter-f55bccf1c0fb060f548db7838c533755f21f493d.zip
router-f55bccf1c0fb060f548db7838c533755f21f493d.tar.gz
router-f55bccf1c0fb060f548db7838c533755f21f493d.tar.bz2
Fixes
Diffstat (limited to 'src')
-rw-r--r--src/Router/Middleware/BasePath.php17
1 files changed, 7 insertions, 10 deletions
diff --git a/src/Router/Middleware/BasePath.php b/src/Router/Middleware/BasePath.php
index a209a1f..a80d029 100644
--- a/src/Router/Middleware/BasePath.php
+++ b/src/Router/Middleware/BasePath.php
@@ -48,9 +48,9 @@ class BasePath
* @param callback $next
* @return ResponseInterface
*/
- public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null)
+ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
{
- if ($next && !is_callable($next)) {
+ if (!is_callable($next)) {
throw new \InvalidArgumentException("'next' should be a callback");
}
@@ -58,11 +58,10 @@ class BasePath
$path = $this->normalizePath($uri->getPath());
if (!$this->hasBasePath($path)) return $this->setError($response);
- if (!$next) return $response;
$noBase = $this->getBaselessPath($path);
- $uri = $uri->withPath($noBase);
- $request = $request->withUri($uri)->withAttribute('original_uri', $path);
+ $noBaseUri = $uri->withPath($noBase);
+ $request = $request->withUri($noBaseUri)->withAttribute('original_uri', $uri);
return call_user_func($next, $request, $response);
}
@@ -75,9 +74,7 @@ class BasePath
*/
protected function getBaselessPath($path)
{
- $path = preg_replace('|^' . preg_quote($this->getBasePath()) . '|i', '', $path);
-
- return $path ?: '/';
+ return substr($path, strlen($this->getBasePath())) ?: '/';
}
/**
@@ -88,7 +85,7 @@ class BasePath
*/
protected function normalizePath($path)
{
- return '/' . trim($path, '/');
+ return '/' . ltrim($path, '/');
}
/**
@@ -99,7 +96,7 @@ class BasePath
*/
protected function hasBasePath($path)
{
- return preg_match('#^' . preg_quote($this->getBasePath()) . '(\/|$)#i', $path);
+ return strpos($path . '/', $this->getBasePath() . '/') === 0;
}
/**