111
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Http\Context\Formatter\FileFormatter;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
@@ -17,22 +18,38 @@ class ResponseEmitter implements Emitter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Http\Response|\Swoole\Http2\Response $response
|
||||
* @param ResponseInterface $emitter
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sender(mixed $response, ResponseInterface $emitter): void
|
||||
{
|
||||
$content = $emitter->configure($response)->getContent();
|
||||
if ($content instanceof FileFormatter) {
|
||||
di(DownloadEmitter::class)->sender($response, $emitter);
|
||||
return;
|
||||
}
|
||||
$response->header('Content-Type', $emitter->getResponseFormat());
|
||||
$response->end($content->getData());
|
||||
}
|
||||
/**
|
||||
* @var \Server\Constrict\DownloadEmitter
|
||||
*/
|
||||
#[Inject(DownloadEmitter::class)]
|
||||
public DownloadEmitter $downloadEmitter;
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Http\Response|\Swoole\Http2\Response $response
|
||||
* @param ResponseInterface $emitter
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sender(mixed $response, ResponseInterface $emitter): void
|
||||
{
|
||||
if (!empty($this->headers) && is_array($this->headers)) {
|
||||
foreach ($this->headers as $name => $values) {
|
||||
$response->header($name, implode(';', $values));
|
||||
}
|
||||
$this->headers = [];
|
||||
}
|
||||
if (!empty($this->cookies) && is_array($this->cookies)) {
|
||||
foreach ($this->cookies as $name => $cookie) {
|
||||
$response->cookie($name, ...$cookie);
|
||||
}
|
||||
$this->cookies = [];
|
||||
}
|
||||
$response->setStatusCode($emitter->getStatusCode());
|
||||
$response->header('Run-Time', time());
|
||||
$response->end($emitter->getBody());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user