Files
kiri-router/src/SwowHttpResponseEmitter.php
T

85 lines
1.9 KiB
PHP
Raw Normal View History

2023-04-24 10:54:56 +08:00
<?php
namespace Kiri\Router;
2023-10-17 21:18:52 +08:00
use Kiri\Di\Inject\Container;
2023-04-24 10:54:56 +08:00
use Kiri\Di\Interface\ResponseEmitterInterface;
2023-10-17 21:18:52 +08:00
use Kiri\Events\EventDispatch;
use Kiri\Events\EventProvider;
2023-08-16 01:01:47 +08:00
use Kiri\Server\Events\OnAfterRequest;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2023-04-24 10:54:56 +08:00
use Psr\Http\Message\ResponseInterface;
2023-08-16 01:01:47 +08:00
use ReflectionException;
2023-10-17 21:18:52 +08:00
use SplPriorityQueue;
2023-04-24 10:54:56 +08:00
2023-04-24 14:23:24 +08:00
class SwowHttpResponseEmitter implements ResponseEmitterInterface
2023-04-24 10:54:56 +08:00
{
2023-10-17 21:18:52 +08:00
/**
* @var EventProvider
*/
#[Container(EventProvider::class)]
public EventProvider $provider;
/**
* @var EventDispatch
*/
#[Container(EventDispatch::class)]
public EventDispatch $dispatch;
/**
* @var SplPriorityQueue
*/
protected SplPriorityQueue $events;
/**
* @var OnAfterRequest
*/
protected OnAfterRequest $afterRequest;
/**
* @return void
*/
public function init(): void
{
$this->afterRequest = new OnAfterRequest();
$this->events = $this->provider->getListenersForEvent($this->afterRequest);
}
2023-04-24 10:54:56 +08:00
2023-08-16 01:01:47 +08:00
/**
* @param Response $proxy
* @param object $response
2023-10-17 14:50:46 +08:00
* @param object $request
2023-08-16 01:01:47 +08:00
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
2023-11-22 09:26:18 +08:00
public function response(ResponseInterface $proxy, object $response, object $request): void
2023-04-24 10:54:56 +08:00
{
// TODO: Implement sender() method.
$proxy->withHeader('Server', 'Swow');
2023-10-17 14:50:46 +08:00
$proxy->withHeader('Run-Time', $this->getRunTime($request));
$response->sendHttpResponse($proxy);
2023-08-16 01:01:47 +08:00
2023-10-17 21:18:52 +08:00
$this->dispatch->execute($this->events, $this->afterRequest);
2023-08-16 01:01:47 +08:00
}
2023-04-24 10:54:56 +08:00
2023-10-17 14:50:46 +08:00
/**
* @param object $request
* @return float
*/
protected function getRunTime(object $request): float
{
return microtime(true) - +$request->getServerParam('request_time_float');
}
2023-04-24 10:54:56 +08:00
}