diff --git a/Message/RequestAbstract.php b/Message/RequestAbstract.php index f01b32079..ebc1090ef 100644 --- a/Message/RequestAbstract.php +++ b/Message/RequestAbstract.php @@ -50,14 +50,6 @@ abstract class RequestAbstract implements MessageInterface */ protected $type = null; - /** - * Root. - * - * @var string - * @since 1.0.0 - */ - protected $rootPath = null; - /** * Request data. * diff --git a/Uri/Http.php b/Uri/Http.php index f9d2894a0..fd060e5ef 100644 --- a/Uri/Http.php +++ b/Uri/Http.php @@ -39,6 +39,22 @@ final class Http implements UriInterface */ private $rootPath = '/'; + /** + * Path offset. + * + * @var int + * @since 1.0.0 + */ + private $pathOffset = 0; + + /** + * Path elements. + * + * @var array + * @since 1.0.0 + */ + private $pathElements = []; + /** * Uri. * @@ -152,7 +168,7 @@ final class Http implements UriInterface $this->port = $url['port'] ?? 80; $this->user = $url['user'] ?? ''; $this->pass = $url['pass'] ?? ''; - $this->path = $url['path'] ?? ''; + $this->path = \ltrim($url['path'] ?? '', '/'); if (StringUtils::endsWith($this->path, '.php')) { $path = \substr($this->path, 0, -4); @@ -164,8 +180,8 @@ final class Http implements UriInterface $this->path = $path; } - $this->path = \strpos($this->path, $this->rootPath) === 0 ? \substr($this->path, \strlen($this->rootPath), \strlen($this->path)) : $this->path; - $this->queryString = $url['query'] ?? ''; + $this->pathElements = \explode('/', $this->path); + $this->queryString = $url['query'] ?? ''; if (!empty($this->queryString)) { \parse_str($this->queryString, $this->query); @@ -227,7 +243,14 @@ final class Http implements UriInterface public function setRootPath(string $root) : void { $this->rootPath = $root; - $this->set($this->uri); + } + + /** + * {@inheritdoc} + */ + public function setPathOffset(int $offset) : void + { + $this->pathOffset = $offset; } /** @@ -298,7 +321,7 @@ final class Http implements UriInterface */ public function getPathOffset() : int { - return \substr_count($this->rootPath, '/') - 1; + return $this->pathOffset; } /** @@ -327,9 +350,9 @@ final class Http implements UriInterface /** * {@inheritdoc} */ - public function getPathElement(int $pos = null) : string + public function getPathElement(int $pos = 0) : string { - return \explode('/', $this->path)[$pos] ?? ''; + return $this->pathElements[$pos + $this->pathOffset] ?? ''; } /** @@ -337,7 +360,7 @@ final class Http implements UriInterface */ public function getPathElements() : array { - return \explode('/', $this->path); + return $this->pathElements; } /**