/** * Fire up action when event listener triggered * * - if we need back result into Events, must implement- * iEventAware to inject event target into listener, * then you can use setter/getter methods from target- * event. * * ! stop propagation: * $targetEvent->stopPropagation() * * @param \Exception $exception * * @return mixed */ function __invoke($exception = null, $e = null) { if (!($exception instanceof RouteNotMatchException || $this->response->getStatCode() === 404)) { ## unknown error return; } $request = $this->request; $uri = $request->getUri(); $path = $uri->getPath(); if ($path->isAbsolute()) { $path = $path->split(1); } $mime = null; $resolved = $this->assetManager->resolve($path->toString()); if (!$resolved) { ## asset file not resolved! return; } if (pathinfo($resolved, PATHINFO_EXTENSION) == 'css') { $mime = 'text/css'; } if (pathinfo($resolved, PATHINFO_EXTENSION) == 'js') { $mime = 'application/javascript'; } $size = filesize($resolved); $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = $mime ? $mime : finfo_file($finfo, $resolved); finfo_close($finfo); $content = file_get_contents($resolved); /** @var \Poirot\Http\Headers $headers */ $headers = $this->response->getHeaders(); $headers->attach(new HeaderLine('Content-Transfer-Encoding: binary')); $headers->attach(new HeaderLine('Content-Type: ' . $mime)); $headers->attach(new HeaderLine('Content-Length: ' . $size)); $this->response->setStatCode(200); $this->response->setBody($content); $this->response->plg()->phpServer()->send(); die; }
/** * Construct * * @param HttpResponse $response */ function __construct(HttpResponse $response) { $this->rawbody = $response; $this->setRawBody($response->getBody()); /** @var iHeader $h */ foreach ($response->getHeaders() as $h) { $this->meta()->set($h->getLabel(), $h); } $statusPlugin = new ResposeStatusPlugin(['message_object' => $response]); if (!$statusPlugin->isSuccess()) { $this->setException(new \RuntimeException($response->getStatReason(), $response->getStatCode())); } }
/** * @param HttpSocketTransporter $transporter * @param HttpResponse $response * @param Streamable $stream * @param iHttpRequest $request * * @return mixed */ function __invoke($transporter = null, $response = null, $stream = null, $request = null) { $statusCode = $response->getStatCode(); # Handle 100 and 101 responses if ($statusCode == 100 || $statusCode == 101) { ## receive data will continue after events $transporter->reset(); } # HEAD requests and 204 or 304 stat codes are not expected to have a body if ($statusCode == 304 || $statusCode == 204 || $request->getMethod() == HttpRequest::METHOD_HEAD) { ## do not continue with body return ['continue' => false]; } /*$statusPlugin = new Status(['message_object' => $response]); if (!$statusPlugin->isSuccess()) ## always connection will closed, no need to continue return ['continue' => false];*/ }