Skip to content

Commit

Permalink
[HttpFoundation] added Request::__toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 15, 2011
1 parent 62d09b8 commit dcb4ef6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -165,6 +165,7 @@ static public function create($uri, $method = 'GET', $parameters = array(), $coo
'REMOTE_ADDR' => '127.0.0.1',
'SCRIPT_NAME' => '',
'SCRIPT_FILENAME' => '',
'SERVER_PROTOCOL' => 'HTTP/1.1',
);

$components = parse_url($uri);
Expand Down Expand Up @@ -280,6 +281,32 @@ public function __clone()
$this->headers = clone $this->headers;
}

/**
* Returns the request as a string.
*
* @return string The request
*/
public function __toString()
{
// status
$content = sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n";

$beautifier = function ($name) {
return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", ucfirst($name));
};

// headers
foreach ($this->headers->all() as $name => $values) {
foreach ($values as $value) {
$content .= sprintf("%s: %s\r\n", $beautifier($name), $value);
}
}

$content .= "\r\n".$this->getContent();

return $content;
}

/**
* Overrides the PHP global variables according to this request instance.
*
Expand Down
10 changes: 6 additions & 4 deletions src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -96,17 +96,19 @@ public function __construct($content = '', $status = 200, $headers = array())
*/
public function __toString()
{
$content = '';

$this->fixContentType();

// status
$content .= sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n";
$content = sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n";

$beautifier = function ($name) {
return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", ucfirst($name));
};

// headers
foreach ($this->headers->all() as $name => $values) {
foreach ($values as $value) {
$content .= "$name: $value\r\n";
$content .= sprintf("%s: %s\r\n", $beautifier($name), $value);
}
}

Expand Down

0 comments on commit dcb4ef6

Please sign in to comment.