Made Response->statusCode into a property and added ->reasonPhrase.
PhpDoc updates. When you set the ->statusCode to a standard HTTP status code, the instance will set the reason phrase appropriately. You may also set statusCode and reasonPhrase separately, or set them at once by passing two parameters to setStatusCode();
This commit is contained in:
parent
7ee4ee39b9
commit
b3f997136f
|
|
@ -60,6 +60,11 @@ class Handler {
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
// !Accessors
|
// !Accessors
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
* @return Response
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
public function __get($name) {
|
public function __get($name) {
|
||||||
|
|
||||||
switch ($name) {
|
switch ($name) {
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ require_once(dirname(__FILE__) . '/exceptions/CurlException.inc.php');
|
||||||
* Second, you can create a custom Request and use it to obtain a Response
|
* Second, you can create a custom Request and use it to obtain a Response
|
||||||
* from a server through cURL.
|
* from a server through cURL.
|
||||||
*
|
*
|
||||||
|
* @property string hostname Hostname part of the URI
|
||||||
|
* @property string method HTTP method (GET, POST, PUT, DELETE, etc.)
|
||||||
* @property string path Path component of the URI for the request
|
* @property string path Path component of the URI for the request
|
||||||
* @property string pathParts Fragments of the path, delimited by slashes
|
* @property string pathParts Fragments of the path, delimited by slashes
|
||||||
* @property array query Associative array of query parameters
|
* @property array query Associative array of query parameters
|
||||||
|
|
|
||||||
178
Response.inc.php
178
Response.inc.php
|
|
@ -15,27 +15,45 @@ require_once(dirname(__FILE__) . '/Message.inc.php');
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property string body Entity body of the response
|
* @property string reasonPhrase Text explanation of status code.
|
||||||
* @property array headers Associative array of headers
|
* @property int statusCode HTTP status code
|
||||||
|
* @property string statusLine HTTP status line, e.g. "HTTP/1.1 200 OK"
|
||||||
*/
|
*/
|
||||||
class Response extends Message {
|
class Response extends Message {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The protocol. Set this to the protocol you wish to use, such as
|
* The protocol. Set this to the protocol you wish to use, such as
|
||||||
* "HTTP/1.1". If unset, the class uses $_SERVER['SERVER_PROTOCOL']
|
* "HTTP/1.1". The default is $_SERVER['SERVER_PROTOCOL'] or HTTP/1.1
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $protocol;
|
public $protocol;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Text explanation of the HTTP Status Code. You only need to set this if
|
||||||
|
* you are using nonstandard status codes. Otherwise, the instance will
|
||||||
|
* set the when you update the status code.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $reasonPhrase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP status code
|
* HTTP status code
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
public $statusCode;
|
protected $statusCode;
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Response instance, optionally passing a status code, body,
|
||||||
|
* and headers.
|
||||||
|
*
|
||||||
|
* @param int $statusCode
|
||||||
|
* @param string $body
|
||||||
|
* @param array $headers
|
||||||
|
*/
|
||||||
public function __construct($statusCode=500, $body=null, $headers=null) {
|
public function __construct($statusCode=500, $body=null, $headers=null) {
|
||||||
|
|
||||||
$this->statusCode = $statusCode;
|
$this->statusCode = $statusCode;
|
||||||
|
|
@ -50,11 +68,57 @@ class Response extends Message {
|
||||||
$this->body = $body;
|
$this->body = $body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($_SERVER['SERVER_PROTOCOL'])) {
|
||||||
|
$this->protocol = $_SERVER['SERVER_PROTOCOL'];
|
||||||
|
} else {
|
||||||
|
$this->protocol = 'HTTP/1.1';
|
||||||
|
}
|
||||||
|
|
||||||
} // __construct()
|
} // __construct()
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
// !Accessors
|
// !Accessors
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return array|string
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function __get($name) {
|
||||||
|
|
||||||
|
switch ($name) {
|
||||||
|
case 'reasonPhrase':
|
||||||
|
return $this->getReasonPhrase();
|
||||||
|
case 'statusCode':
|
||||||
|
return $this->getStatusCode();
|
||||||
|
case 'statusLine':
|
||||||
|
return $this->getStatusLine();
|
||||||
|
default:
|
||||||
|
return parent::__get($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // __get()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @param mixed $value
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function __set($name, $value) {
|
||||||
|
|
||||||
|
switch ($name) {
|
||||||
|
case 'reasonPhrase':
|
||||||
|
$this->setReasonPhrase($value);
|
||||||
|
return;
|
||||||
|
case 'statusCode':
|
||||||
|
$this->setStatusCode($value);
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
parent::__set($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide a new entity body for the respone.
|
* Provide a new entity body for the respone.
|
||||||
* This method also updates the content-length header based on the length
|
* This method also updates the content-length header based on the length
|
||||||
|
|
@ -71,39 +135,40 @@ class Response extends Message {
|
||||||
$this->setHeader('Content-Length', strlen($value));
|
$this->setHeader('Content-Length', strlen($value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getReasonPhrase() {
|
||||||
|
return $this->reasonPhrase;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $statusCodeMessage
|
||||||
|
*/
|
||||||
|
public function setReasonPhrase($statusCodeMessage) {
|
||||||
|
$this->reasonPhrase = $statusCodeMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getStatusCode() {
|
||||||
|
return $this->statusCode;
|
||||||
} // setBody()
|
} // setBody()
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Output the response to the client.
|
* @param int $statusCode
|
||||||
*
|
* @param string $reasonPhrase
|
||||||
* @param bool $headersOnly Do not include the body, only the headers.
|
* @throws \InvalidArgumentException
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function respond($headersOnly=false) {
|
public function setStatusCode($statusCode, $reasonPhrase=null) {
|
||||||
|
|
||||||
// Output the HTTP status code.
|
$this->statusCode = (int) $statusCode;
|
||||||
header($this->getStatusLine($this->statusCode));
|
|
||||||
|
|
||||||
// Output each header.
|
if (is_null($reasonPhrase)) {
|
||||||
foreach ($this->headers as $header => $value) {
|
|
||||||
header($header . ': ' . $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output the entity body.
|
|
||||||
if (!$headersOnly && isset($this->body)) {
|
|
||||||
print $this->body;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // respond()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return HTTP status line, e.g. HTTP/1.1 200 OK.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* @throws \UnexpectedValueException
|
|
||||||
*/
|
|
||||||
protected function getStatusLine() {
|
|
||||||
|
|
||||||
switch ($this->statusCode) {
|
switch ($this->statusCode) {
|
||||||
case 100: $text = 'Continue'; break;
|
case 100: $text = 'Continue'; break;
|
||||||
|
|
@ -143,23 +208,56 @@ class Response extends Message {
|
||||||
case 503: $text = 'Service Unavailable'; break;
|
case 503: $text = 'Service Unavailable'; break;
|
||||||
case 504: $text = 'Gateway Time-out'; break;
|
case 504: $text = 'Gateway Time-out'; break;
|
||||||
case 505: $text = 'HTTP Version not supported'; break;
|
case 505: $text = 'HTTP Version not supported'; break;
|
||||||
default:
|
default: $text = 'Nonstandard'; break;
|
||||||
throw new \UnexpectedValueException('Unknown http status code "' . $this->statusCode . '"');
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->protocol)) {
|
$this->reasonPhrase = $text;
|
||||||
$protocol = $this->protocol;
|
|
||||||
} elseif (isset($_SERVER['SERVER_PROTOCOL'])) {
|
|
||||||
$protocol = $_SERVER['SERVER_PROTOCOL'];
|
|
||||||
} else {
|
} else {
|
||||||
$protocol = 'HTTP/1.1';
|
|
||||||
|
if (is_string($reasonPhrase)) {
|
||||||
|
$this->reasonPhrase = $reasonPhrase;
|
||||||
|
} else {
|
||||||
|
throw new \InvalidArgumentException('$reasonPhrase must be a string (or null to use standard HTTP Reason-Phrase');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $protocol . ' ' . $this->statusCode . ' ' . $text;
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return HTTP status line, e.g. HTTP/1.1 200 OK.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getStatusLine() {
|
||||||
|
return $this->protocol . ' ' . $this->statusCode . ' ' . $this->reasonPhrase;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Output the response to the client.
|
||||||
|
*
|
||||||
|
* @param bool $headersOnly Do not include the body, only the headers.
|
||||||
|
*/
|
||||||
|
public function respond($headersOnly=false) {
|
||||||
|
|
||||||
|
// Output the HTTP status code.
|
||||||
|
header($this->statusLine);
|
||||||
|
|
||||||
|
// Output each header.
|
||||||
|
foreach ($this->headers as $header => $value) {
|
||||||
|
header($header . ': ' . $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output the entity body.
|
||||||
|
if (!$headersOnly && isset($this->body)) {
|
||||||
|
print $this->body;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // respond()
|
||||||
|
|
||||||
} // Response
|
} // Response
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue