2021-08-10 16:40:01 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Server\Constrict;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2021-08-17 16:52:50 +08:00
|
|
|
use Http\Context\Formatter\FileFormatter;
|
2021-08-11 19:14:26 +08:00
|
|
|
use Kiri\Exception\NotFindClassException;
|
2021-08-10 17:49:46 +08:00
|
|
|
use ReflectionException;
|
2021-08-10 16:40:01 +08:00
|
|
|
use Server\ResponseInterface;
|
2021-08-11 19:14:26 +08:00
|
|
|
use Swoole\Server;
|
2021-08-10 16:40:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2021-08-12 15:09:25 +08:00
|
|
|
class ResponseEmitter implements Emitter
|
2021-08-10 16:40:01 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-12 15:08:46 +08:00
|
|
|
* @param \Swoole\Http\Response|\Swoole\Http2\Response $response
|
2021-08-10 16:40:01 +08:00
|
|
|
* @param ResponseInterface $emitter
|
2021-08-10 17:49:46 +08:00
|
|
|
* @throws NotFindClassException
|
2021-08-11 19:14:26 +08:00
|
|
|
* @throws ReflectionException
|
2021-08-10 16:40:01 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-08-12 15:09:25 +08:00
|
|
|
public function sender(mixed $response, ResponseInterface $emitter): void
|
2021-08-10 16:40:01 +08:00
|
|
|
{
|
|
|
|
|
$content = $emitter->configure($response)->getContent();
|
2021-08-10 17:04:27 +08:00
|
|
|
if ($content instanceof FileFormatter) {
|
2021-08-12 15:19:51 +08:00
|
|
|
di(DownloadEmitter::class)->sender($response, $emitter);
|
2021-08-10 17:35:27 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2021-08-12 15:19:51 +08:00
|
|
|
$response->header('Content-Type', $emitter->getResponseFormat());
|
|
|
|
|
$response->end($content->getData());
|
2021-08-10 17:08:33 +08:00
|
|
|
}
|
|
|
|
|
|
2021-08-10 16:40:01 +08:00
|
|
|
}
|