Ver Fonte

[HttpKernel] changed the semantic of Response::__toString() to something more useful

Fabien Potencier há 15 anos atrás
pai
commit
7661e9a5f7
1 ficheiros alterados com 22 adições e 3 exclusões
  1. 22 3
      src/Symfony/Components/HttpKernel/Response.php

+ 22 - 3
src/Symfony/Components/HttpKernel/Response.php

@@ -88,15 +88,34 @@ class Response
     }
 
     /**
-     * Returns the response content after sending the headers.
+     * Returns the response content as it will be sent (with the headers).
      *
      * @return string The response content
      */
     public function __toString()
     {
-        $this->sendHeaders();
+        $content = '';
+
+        if (!$this->headers->has('Content-Type')) {
+            $this->headers->set('Content-Type', 'text/html');
+        }
+
+        // status
+        $content .= sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\n";
+
+        // headers
+        foreach ($this->headers->all() as $name => $value) {
+            $content .= "$name: $value\n";
+        }
+
+        // cookies
+        foreach ($this->cookies as $cookie) {
+            $content .= sprintf('Set-Cookie: %s=%s; expires=%s; path=%s; domain=%s%s%s', $cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'] ? '; secure' : '', $cookie['httpOnly'] ? '; HttpOnly' : '')."\n";
+        }
+
+        $content .= "\n".$this->getContent();
 
-        return (string) $this->getContent();
+        return $content;
     }
 
     /**