Add path offset

This commit is contained in:
Dennis Eichhorn 2019-03-17 16:39:54 +01:00
parent 8203389ec0
commit 38ed968cc5
2 changed files with 31 additions and 16 deletions

View File

@ -50,14 +50,6 @@ abstract class RequestAbstract implements MessageInterface
*/ */
protected $type = null; protected $type = null;
/**
* Root.
*
* @var string
* @since 1.0.0
*/
protected $rootPath = null;
/** /**
* Request data. * Request data.
* *

View File

@ -39,6 +39,22 @@ final class Http implements UriInterface
*/ */
private $rootPath = '/'; private $rootPath = '/';
/**
* Path offset.
*
* @var int
* @since 1.0.0
*/
private $pathOffset = 0;
/**
* Path elements.
*
* @var array
* @since 1.0.0
*/
private $pathElements = [];
/** /**
* Uri. * Uri.
* *
@ -152,7 +168,7 @@ final class Http implements UriInterface
$this->port = $url['port'] ?? 80; $this->port = $url['port'] ?? 80;
$this->user = $url['user'] ?? ''; $this->user = $url['user'] ?? '';
$this->pass = $url['pass'] ?? ''; $this->pass = $url['pass'] ?? '';
$this->path = $url['path'] ?? ''; $this->path = \ltrim($url['path'] ?? '', '/');
if (StringUtils::endsWith($this->path, '.php')) { if (StringUtils::endsWith($this->path, '.php')) {
$path = \substr($this->path, 0, -4); $path = \substr($this->path, 0, -4);
@ -164,8 +180,8 @@ final class Http implements UriInterface
$this->path = $path; $this->path = $path;
} }
$this->path = \strpos($this->path, $this->rootPath) === 0 ? \substr($this->path, \strlen($this->rootPath), \strlen($this->path)) : $this->path; $this->pathElements = \explode('/', $this->path);
$this->queryString = $url['query'] ?? ''; $this->queryString = $url['query'] ?? '';
if (!empty($this->queryString)) { if (!empty($this->queryString)) {
\parse_str($this->queryString, $this->query); \parse_str($this->queryString, $this->query);
@ -227,7 +243,14 @@ final class Http implements UriInterface
public function setRootPath(string $root) : void public function setRootPath(string $root) : void
{ {
$this->rootPath = $root; $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 public function getPathOffset() : int
{ {
return \substr_count($this->rootPath, '/') - 1; return $this->pathOffset;
} }
/** /**
@ -327,9 +350,9 @@ final class Http implements UriInterface
/** /**
* {@inheritdoc} * {@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 public function getPathElements() : array
{ {
return \explode('/', $this->path); return $this->pathElements;
} }
/** /**