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;
|
2023-04-24 10:54:56 +08:00
|
|
|
use Kiri\Di\Interface\ResponseEmitterInterface;
|
2023-08-16 01:01:47 +08:00
|
|
|
use Kiri\Server\Events\OnAfterRequest;
|
|
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2023-04-15 23:31:16 +08:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2023-08-16 01:01:47 +08:00
|
|
|
use ReflectionException;
|
2023-04-15 23:31:16 +08:00
|
|
|
|
|
|
|
|
|
2023-04-24 14:23:24 +08:00
|
|
|
class SwooleHttpResponseEmitter implements ResponseEmitterInterface
|
2023-04-15 23:29:27 +08:00
|
|
|
{
|
|
|
|
|
|
2023-04-15 23:31:16 +08:00
|
|
|
|
2023-08-16 01:01:47 +08:00
|
|
|
/**
|
|
|
|
|
* @param Response $proxy
|
|
|
|
|
* @param object $response
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
2023-04-15 23:31:16 +08:00
|
|
|
public function sender(ResponseInterface $proxy, object $response): void
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement sender() method.
|
|
|
|
|
$this->writeParams($proxy, $response);
|
|
|
|
|
|
2023-04-16 15:57:50 +08:00
|
|
|
$proxy->end($response);
|
2023-08-16 01:01:47 +08:00
|
|
|
|
|
|
|
|
event(new OnAfterRequest());
|
|
|
|
|
}
|
2023-04-15 23:31:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Response $proxy
|
2023-04-19 10:51:08 +08:00
|
|
|
* @param object $response
|
2023-04-15 23:31:16 +08:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function writeParams(ResponseInterface $proxy, object $response): void
|
|
|
|
|
{
|
|
|
|
|
$response->setStatusCode($proxy->getStatusCode());
|
2023-04-16 14:07:35 +08:00
|
|
|
foreach ($proxy->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 14:07:35 +08:00
|
|
|
foreach ($proxy->getCookieParams() as $cookie) {
|
2023-04-15 23:31:16 +08:00
|
|
|
$response->setCookie(...$cookie);
|
|
|
|
|
}
|
2023-10-11 13:14:37 +08:00
|
|
|
|
|
|
|
|
$request = \request();
|
|
|
|
|
|
|
|
|
|
$response->header('Run-Time', microtime(true) - +$request->getServerParam('request_time_float'));
|
2023-04-15 23:31:16 +08:00
|
|
|
$response->header('Server', 'swoole');
|
|
|
|
|
$response->header('Swoole-Version', swoole_version());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-15 23:29:27 +08:00
|
|
|
}
|