2023-04-15 23:29:27 +08:00
|
|
|
<?php
|
2023-04-16 01:24:30 +08:00
|
|
|
declare(strict_types=1);
|
2023-04-15 23:29:27 +08:00
|
|
|
|
|
|
|
|
namespace Kiri\Router;
|
|
|
|
|
|
2023-04-15 23:31:16 +08:00
|
|
|
use Exception;
|
|
|
|
|
use Kiri\Di\Interface\ResponseEmitter;
|
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HttpResponseEmitter implements ResponseEmitter
|
2023-04-15 23:29:27 +08:00
|
|
|
{
|
|
|
|
|
|
2023-04-15 23:31:16 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Response $proxy
|
|
|
|
|
* @param object $response
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function sender(ResponseInterface $proxy, object $response): void
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement sender() method.
|
|
|
|
|
$this->writeParams($proxy, $response);
|
|
|
|
|
|
|
|
|
|
$proxy->write($response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Response $proxy
|
2023-04-16 03:23:51 +08:00
|
|
|
* @param \Swoole\Http\Response $response
|
2023-04-15 23:31:16 +08:00
|
|
|
* @return void
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function writeParams(ResponseInterface $proxy, object $response): void
|
|
|
|
|
{
|
|
|
|
|
$response->setStatusCode($proxy->getStatusCode());
|
2023-04-16 03:23:51 +08:00
|
|
|
/** @var Response $resp */
|
|
|
|
|
$resp = \Kiri::service()->get('response');
|
|
|
|
|
foreach ($resp->getHeaders() as $name => $header) {
|
2023-04-16 03:31:35 +08:00
|
|
|
$response->header($name, $header);
|
2023-04-15 23:31:16 +08:00
|
|
|
}
|
2023-04-16 03:27:43 +08:00
|
|
|
foreach ($resp->getCookieParams() as $cookie) {
|
2023-04-15 23:31:16 +08:00
|
|
|
$response->setCookie(...$cookie);
|
|
|
|
|
}
|
|
|
|
|
$response->header('Server', 'swoole');
|
|
|
|
|
$response->header('Swoole-Version', swoole_version());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-15 23:29:27 +08:00
|
|
|
}
|