Skip to content

Commit

Permalink
[HttpFoundation] Add chainability to the Response class
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Mar 15, 2012
1 parent 7a54fe4 commit 873da43
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
90 changes: 89 additions & 1 deletion src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -143,6 +143,24 @@ public function __construct($content = '', $status = 200, $headers = array())
}
}

/**
* Factory method for chainability
*
* Example:
*
* return Response::create($body, 200)
* ->setSharedMaxAge(300);
*
* @param string $content The response content
* @param integer $status The response status code
* @param array $headers An array of response headers
* @return Response
*/
public static function create($content = '', $status = 200, $headers = array())
{
return new static($content, $status, $headers);
}

/**
* Returns the Response as an HTTP string.
*
Expand Down Expand Up @@ -221,12 +239,14 @@ public function prepare(Request $request)

/**
* Sends HTTP headers.
*
* @return Response
*/
public function sendHeaders()
{
// headers have already been sent by the developer
if (headers_sent()) {
return;
return $this;
}

// status
Expand All @@ -243,19 +263,27 @@ public function sendHeaders()
foreach ($this->headers->getCookies() as $cookie) {
setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
}

return $this;
}

/**
* Sends content for the current web response.
*
* @return Response
*/
public function sendContent()
{
echo $this->content;

return $this;
}

/**
* Sends HTTP headers and content.
*
* @return Response
*
* @api
*/
public function send()
Expand All @@ -266,6 +294,8 @@ public function send()
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}

return $this;
}

/**
Expand All @@ -274,6 +304,7 @@ public function send()
* Valid types are strings, numbers, and objects that implement a __toString() method.
*
* @param mixed $content
* @return Response
*
* @api
*/
Expand All @@ -284,6 +315,8 @@ public function setContent($content)
}

$this->content = (string) $content;

return $this;
}

/**
Expand All @@ -302,12 +335,15 @@ public function getContent()
* Sets the HTTP protocol version (1.0 or 1.1).
*
* @param string $version The HTTP protocol version
* @return Response
*
* @api
*/
public function setProtocolVersion($version)
{
$this->version = $version;

return $this;
}

/**
Expand All @@ -327,6 +363,7 @@ public function getProtocolVersion()
*
* @param integer $code HTTP status code
* @param string $text HTTP status text
* @return Response
*
* @throws \InvalidArgumentException When the HTTP status code is not valid
*
Expand All @@ -340,6 +377,8 @@ public function setStatusCode($code, $text = null)
}

$this->statusText = false === $text ? '' : (null === $text ? self::$statusTexts[$this->statusCode] : $text);

return $this;
}

/**
Expand All @@ -358,12 +397,15 @@ public function getStatusCode()
* Sets the response charset.
*
* @param string $charset Character set
* @return Response
*
* @api
*/
public function setCharset($charset)
{
$this->charset = $charset;

return $this;
}

/**
Expand Down Expand Up @@ -438,25 +480,33 @@ public function isValidateable()
*
* It makes the response ineligible for serving other clients.
*
* @return Response
*
* @api
*/
public function setPrivate()
{
$this->headers->removeCacheControlDirective('public');
$this->headers->addCacheControlDirective('private');

return $this;
}

/**
* Marks the response as "public".
*
* It makes the response eligible for serving other clients.
*
* @return Response
*
* @api
*/
public function setPublic()
{
$this->headers->addCacheControlDirective('public');
$this->headers->removeCacheControlDirective('private');

return $this;
}

/**
Expand Down Expand Up @@ -494,13 +544,16 @@ public function getDate()
* Sets the Date header.
*
* @param \DateTime $date A \DateTime instance
* @return Response
*
* @api
*/
public function setDate(\DateTime $date)
{
$date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');

return $this;
}

/**
Expand All @@ -520,13 +573,17 @@ public function getAge()
/**
* Marks the response stale by setting the Age header to be equal to the maximum age of the response.
*
* @return Response
*
* @api
*/
public function expire()
{
if ($this->isFresh()) {
$this->headers->set('Age', $this->getMaxAge());
}

return $this;
}

/**
Expand All @@ -547,6 +604,7 @@ public function getExpires()
* If passed a null value, it removes the header.
*
* @param \DateTime $date A \DateTime instance
* @return Response
*
* @api
*/
Expand All @@ -559,6 +617,8 @@ public function setExpires(\DateTime $date = null)
$date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Expires', $date->format('D, d M Y H:i:s').' GMT');
}

return $this;
}

/**
Expand Down Expand Up @@ -595,12 +655,15 @@ public function getMaxAge()
* This methods sets the Cache-Control max-age directive.
*
* @param integer $value Number of seconds
* @return Response
*
* @api
*/
public function setMaxAge($value)
{
$this->headers->addCacheControlDirective('max-age', $value);

return $this;
}

/**
Expand All @@ -609,13 +672,16 @@ public function setMaxAge($value)
* This methods sets the Cache-Control s-maxage directive.
*
* @param integer $value Number of seconds
* @return Response
*
* @api
*/
public function setSharedMaxAge($value)
{
$this->setPublic();
$this->headers->addCacheControlDirective('s-maxage', $value);

return $this;
}

/**
Expand Down Expand Up @@ -645,12 +711,15 @@ public function getTtl()
* This method adjusts the Cache-Control/s-maxage directive.
*
* @param integer $seconds Number of seconds
* @return Response
*
* @api
*/
public function setTtl($seconds)
{
$this->setSharedMaxAge($this->getAge() + $seconds);

return $this;
}

/**
Expand All @@ -659,12 +728,15 @@ public function setTtl($seconds)
* This method adjusts the Cache-Control/max-age directive.
*
* @param integer $seconds Number of seconds
* @return Response
*
* @api
*/
public function setClientTtl($seconds)
{
$this->setMaxAge($this->getAge() + $seconds);

return $this;
}

/**
Expand All @@ -685,6 +757,7 @@ public function getLastModified()
* If passed a null value, it removes the header.
*
* @param \DateTime $date A \DateTime instance
* @return Response
*
* @api
*/
Expand All @@ -697,6 +770,8 @@ public function setLastModified(\DateTime $date = null)
$date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Last-Modified', $date->format('D, d M Y H:i:s').' GMT');
}

return $this;
}

/**
Expand All @@ -716,6 +791,7 @@ public function getEtag()
*
* @param string $etag The ETag unique identifier
* @param Boolean $weak Whether you want a weak ETag or not
* @return Response
*
* @api
*/
Expand All @@ -730,6 +806,8 @@ public function setEtag($etag = null, $weak = false)

$this->headers->set('ETag', (true === $weak ? 'W/' : '').$etag);
}

return $this;
}

/**
Expand All @@ -738,6 +816,7 @@ public function setEtag($etag = null, $weak = false)
* Available options are: etag, last_modified, max_age, s_maxage, private, and public.
*
* @param array $options An array of cache options
* @return Response
*
* @api
*/
Expand Down Expand Up @@ -778,6 +857,8 @@ public function setCache(array $options)
$this->setPublic();
}
}

return $this;
}

/**
Expand All @@ -786,6 +867,8 @@ public function setCache(array $options)
* This sets the status, removes the body, and discards any headers
* that MUST NOT be included in 304 responses.
*
* @return Response
*
* @see http://tools.ietf.org/html/rfc2616#section-10.3.5
*
* @api
Expand All @@ -799,6 +882,8 @@ public function setNotModified()
foreach (array('Allow', 'Content-Encoding', 'Content-Language', 'Content-Length', 'Content-MD5', 'Content-Type', 'Last-Modified') as $header) {
$this->headers->remove($header);
}

return $this;
}

/**
Expand Down Expand Up @@ -834,12 +919,15 @@ public function getVary()
*
* @param string|array $headers
* @param Boolean $replace Whether to replace the actual value of not (true by default)
* @return Response
*
* @api
*/
public function setVary($headers, $replace = true)
{
$this->headers->set('Vary', $headers, $replace);

return $this;
}

/**
Expand Down

0 comments on commit 873da43

Please sign in to comment.