Skip to content

Commit

Permalink
Merge pull request #6556 from kenjis/fix-ResponseInterface
Browse files Browse the repository at this point in the history
fix: ResponseInterface (1)
  • Loading branch information
kenjis authored Sep 23, 2022
2 parents 25c5d8b + 9fbd5f8 commit a7d5d26
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 64 deletions.
10 changes: 7 additions & 3 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ class CodeIgniter
* to keep from setting headers/cookies/etc
*
* @var bool
*
* @deprecated No longer used.
*/
protected $useSafeOutput = false;

Expand Down Expand Up @@ -340,7 +342,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
return $response;
}

$this->response->pretend($this->useSafeOutput)->send();
$this->response->send();
$this->callExit(EXIT_SUCCESS);

return;
Expand Down Expand Up @@ -371,6 +373,8 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
* not complain when ini_set() function is used.
*
* @return $this
*
* @deprecated No longer used.
*/
public function useSafeOutput(bool $safe = true)
{
Expand Down Expand Up @@ -433,7 +437,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache

// If a ResponseInterface instance is returned then send it back to the client and stop
if ($possibleResponse instanceof ResponseInterface) {
return $returnResponse ? $possibleResponse : $possibleResponse->pretend($this->useSafeOutput)->send();
return $returnResponse ? $possibleResponse : $possibleResponse->send();
}

if ($possibleResponse instanceof Request) {
Expand Down Expand Up @@ -1057,7 +1061,7 @@ public function spoofRequestMethod()
*/
protected function sendResponse()
{
$this->response->pretend($this->useSafeOutput)->send();
$this->response->send();
}

/**
Expand Down
33 changes: 33 additions & 0 deletions system/HTTP/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ interface MessageInterface
*/
public function setBody($data);

/**
* Gets the body of the message.
*
* @return string|null
*
* @TODO Incompatible return type with PSR-7
*/
public function getBody();

/**
* Appends data to the body of the current message.
*
Expand All @@ -48,6 +57,17 @@ public function populateHeaders(): void;
*/
public function headers(): array;

/**
* Checks if a header exists by the given case-insensitive name.
*
* @param string $name Case-insensitive header field name.
*
* @return bool Returns true if any header names match the given header
* name using a case-insensitive string comparison. Returns false if
* no matching header name is found in the message.
*/
public function hasHeader(string $name): bool;

/**
* Returns a single Header object. If multiple headers with the same
* name exist, then will return an array of header objects.
Expand All @@ -58,6 +78,19 @@ public function headers(): array;
*/
public function header($name);

/**
* Retrieves a comma-separated string of the values for a single header.
*
* This method returns all of the header values of the given
* case-insensitive header name as a string concatenated together using
* a comma.
*
* NOTE: Not all header values may be appropriately represented using
* comma concatenation. For such headers, use getHeader() instead
* and supply your own delimiter when concatenating.
*/
public function getHeaderLine(string $name): string;

/**
* Sets a header and it's value.
*
Expand Down
3 changes: 3 additions & 0 deletions system/HTTP/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,13 @@ public function __construct($config)

/**
* Turns "pretend" mode on or off to aid in testing.
*
* Note that this is not a part of the interface so
* should not be relied on outside of internal testing.
*
* @return $this
*
* @testTag only available to test code
*/
public function pretend(bool $pretend = true)
{
Expand Down
28 changes: 15 additions & 13 deletions system/HTTP/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
* - Status code and reason phrase
* - Headers
* - Message body
*
* @mixin RedirectResponse
*/
interface ResponseInterface
interface ResponseInterface extends MessageInterface
{
/**
* Constants for status codes.
Expand Down Expand Up @@ -130,7 +128,7 @@ public function getStatusCode(): int;
* provided status code; if none is provided, will
* default to the IANA name.
*
* @return self
* @return $this
*
* @throws InvalidArgumentException For invalid status code arguments.
*/
Expand All @@ -153,7 +151,7 @@ public function getReason(): string;
/**
* Sets the date header
*
* @return ResponseInterface
* @return $this
*/
public function setDate(DateTime $date);

Expand All @@ -164,6 +162,8 @@ public function setDate(DateTime $date);
* preferably, an instance of DateTime.
*
* @param DateTime|string $date
*
* @return $this
*/
public function setLastModified($date);

Expand All @@ -172,7 +172,7 @@ public function setLastModified($date);
*
* @see http://tools.ietf.org/html/rfc5988
*
* @return Response
* @return $this
*
* @todo Recommend moving to Pager
*/
Expand All @@ -182,7 +182,7 @@ public function setLink(PagerInterface $pager);
* Sets the Content Type header for this response with the mime type
* and, optionally, the charset.
*
* @return ResponseInterface
* @return $this
*/
public function setContentType(string $mime, string $charset = 'UTF-8');

Expand All @@ -202,7 +202,7 @@ public function setJSON($body, bool $unencoded = false);
/**
* Returns the current body, converted to JSON is it isn't already.
*
* @return mixed|string
* @return bool|string|null
*
* @throws InvalidArgumentException If the body property is not array.
*/
Expand All @@ -220,7 +220,7 @@ public function setXML($body);
/**
* Retrieves the current body into XML and returns it.
*
* @return mixed|string
* @return bool|string|null
*
* @throws InvalidArgumentException If the body property is not array.
*/
Expand All @@ -235,6 +235,8 @@ public function getXML();
/**
* Sets the appropriate headers to ensure this response
* is not cached by the browsers.
*
* @return $this
*/
public function noCache();

Expand Down Expand Up @@ -262,7 +264,7 @@ public function noCache();
* - proxy-revalidate
* - no-transform
*
* @return ResponseInterface
* @return $this
*/
public function setCache(array $options = []);

Expand All @@ -273,21 +275,21 @@ public function setCache(array $options = []);
/**
* Sends the output to the browser.
*
* @return ResponseInterface
* @return $this
*/
public function send();

/**
* Sends the headers of this HTTP request to the browser.
*
* @return Response
* @return $this
*/
public function sendHeaders();

/**
* Sends the Body of the message to the browser.
*
* @return Response
* @return $this
*/
public function sendBody();

Expand Down
6 changes: 3 additions & 3 deletions system/HTTP/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function setJSON($body, bool $unencoded = false)
/**
* Returns the current body, converted to JSON is it isn't already.
*
* @return mixed|string
* @return bool|string|null
*
* @throws InvalidArgumentException If the body property is not array.
*/
Expand Down Expand Up @@ -284,7 +284,7 @@ public function setXML($body)
/**
* Retrieves the current body into XML and returns it.
*
* @return mixed|string
* @return bool|string|null
*
* @throws InvalidArgumentException If the body property is not array.
*/
Expand Down Expand Up @@ -430,7 +430,7 @@ public function setLastModified($date)
/**
* Sends the output to the browser.
*
* @return Response
* @return $this
*/
public function send()
{
Expand Down
Loading

0 comments on commit a7d5d26

Please sign in to comment.