/**
* Convert a CakePHP response into a PSR7 one.
*
* @param CakeResponse $response The CakePHP response to convert
* @return PsrResponse $response The equivalent PSR7 response.
*/
public static function toPsr(CakeResponse $response)
{
$status = $response->statusCode();
$headers = $response->header();
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = $response->type();
}
$body = $response->body();
$stream = 'php://memory';
if (is_string($body)) {
$stream = new Stream('php://memory', 'wb');
$stream->write($response->body());
}
if (is_callable($body)) {
$stream = new CallbackStream($body);
}
// This is horrible, but CakePHP doesn't have a getFile() method just yet.
$fileProp = new \ReflectionProperty($response, '_file');
$fileProp->setAccessible(true);
$file = $fileProp->getValue($response);
if ($file) {
$stream = new Stream($file->path, 'rb');
}
return new DiactorosResponse($stream, $status, $headers);
}