Remove the uri member variable from Request and make it a pure property.

Add setHeader() and hasHeader() methods to Request.
This commit is contained in:
PJ Dietz 2012-11-29 15:40:05 -05:00
parent 7c76ddb010
commit 4d0fee6dc9
1 changed files with 26 additions and 45 deletions

View File

@ -86,14 +86,6 @@ class Request {
*/ */
protected $query; protected $query;
/**
* The string value of the full URI. This is reconstructed from the
* components if requested when unset.
*
* @var string
*/
protected $uri = null;
/** /**
* Singleton instance derived from reading info from Apache. * Singleton instance derived from reading info from Apache.
* *
@ -252,11 +244,13 @@ class Request {
*/ */
public function getUri() { public function getUri() {
// Construct the URI if it is unset. $uri = $this->protocol . '://' . $this->hostname . $this->path;
if (!is_null($this->uri)) {
$this->rebuildUri(); if ($this->query) {
$uri .= '?' . http_build_query($this->query);
} }
return $this->uri;
return $uri;
} }
@ -269,18 +263,33 @@ class Request {
$this->body = $body; $this->body = $body;
} }
/**
* Add or update a header to a given value
*
* @param string $header
* @param string $value
*/
public function setHeader($header, $value) {
$this->headers[$header] = $value;
}
/**
* Return if the response contains a header with the given key.
*
* @param string $header
* @return bool
*/
public function hasHeader($header) {
return isset($this->headers[$header]);
}
/** /**
* Set the hostname for the request and update the URI. * Set the hostname for the request and update the URI.
* *
* @param string $hostname * @param string $hostname
*/ */
public function setHostname($hostname) { public function setHostname($hostname) {
$this->hostname = $hostname; $this->hostname = $hostname;
// Update the URI member.
$this->rebuildUri();
} }
/** /**
@ -305,12 +314,8 @@ class Request {
* @param string $path * @param string $path
*/ */
public function setPath($path) { public function setPath($path) {
$this->path = $path; $this->path = $path;
$this->pathParts = explode('/', substr($path, 1)); $this->pathParts = explode('/', substr($path, 1));
// Update the URI member.
$this->rebuildUri();
} }
/** /**
@ -319,12 +324,7 @@ class Request {
* @param string $protocol * @param string $protocol
*/ */
public function setProtocol($protocol) { public function setProtocol($protocol) {
$this->protocol = $protocol; $this->protocol = $protocol;
// Update the URI member.
$this->rebuildUri();
} }
/** /**
@ -344,9 +344,6 @@ class Request {
throw new \InvalidArgumentException('Unable to parse query string.'); throw new \InvalidArgumentException('Unable to parse query string.');
} }
// Update the URI member.
$this->rebuildUri();
} }
/** /**
@ -357,7 +354,6 @@ class Request {
*/ */
public function setUri($uri) { public function setUri($uri) {
$this->uri = $uri;
$parsed = parse_url($uri); $parsed = parse_url($uri);
$host = isset($parsed['host']) ? $parsed['host'] : ''; $host = isset($parsed['host']) ? $parsed['host'] : '';
@ -371,21 +367,6 @@ class Request {
} }
/**
* Build the URI member from the other members (path, query, etc.)
*/
protected function rebuildUri() {
$uri = $this->protocol . '://' . $this->hostname . $this->path;
if ($this->query) {
$uri .= '?' . http_build_query($this->query);
}
$this->uri = $uri;
}
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**