Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 16 additions & 43 deletions Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace MaplePHP\Handler;

use MaplePHP\Container\Interfaces\ContainerExceptionInterface;
use MaplePHP\Container\Interfaces\NotFoundExceptionInterface;
use MaplePHP\Http\Interfaces\ResponseInterface;
use MaplePHP\Http\Interfaces\RequestInterface;
use MaplePHP\Http\Interfaces\StreamInterface;
use MaplePHP\Handler\Exceptions\EmitterException;
use MaplePHP\Handler\ErrorHandler;
use MaplePHP\Container\Interfaces\ContainerInterface;
use MaplePHP\Output\SwiftRender;

Expand All @@ -27,6 +28,10 @@ class Emitter
private $errorHandler = false;
private $errorHandlerMsg;

/**
* @throws NotFoundExceptionInterface
* @throws ContainerExceptionInterface
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
Expand All @@ -45,7 +50,7 @@ public function __construct(ContainerInterface $container)
* Se a default TTL cache save value (default 0)
* @param int $ttl In seconds
*/
public function setDefaultCacheTtl(int $ttl)
public function setDefaultCacheTtl(int $ttl): void
{
$this->cacheDefaultTtl = $ttl;
}
Expand All @@ -60,8 +65,8 @@ public function view(): SwiftRender
}

/**
* If you set a buffered response string it will get priorities agains all outher response
* @param string $output
* If you set a buffered response string it will get priorities against all other response
* @param string|null $output
* @return void
*/
public function outputBuffer(?string $output): void
Expand All @@ -70,10 +75,10 @@ public function outputBuffer(?string $output): void
}

/**
* Will try to get a output to read
* Will try to get an output to read
* @return string
*/
protected function createResponse()
protected function createResponse(): string
{
$stream = $this->response->getBody();
$this->view->findBind($this->response->getStatusCode());
Expand Down Expand Up @@ -113,10 +118,9 @@ private function buildStream(): StreamInterface
}
$responseBody = $this->createResponse();


//Accurate gzip implementation (major improvment for the apps preformance and load speed)
if (($acceptEnc = $this->request->getHeaderLine("Accept-Encoding")) && strpos($acceptEnc, 'gzip') !== false) {
$responseBody = gzencode($responseBody, 9, FORCE_GZIP);
//Accurate gzip implementation (major improvement for the apps performance and load speed)
if (function_exists('gzencode') && ($acceptEnc = $this->request->getHeaderLine("Accept-Encoding")) && str_contains($acceptEnc, 'gzip')) {
$responseBody = gzencode($responseBody, 9, (defined("FORCE_GZIP") ? FORCE_GZIP : 31));
$this->response = $this->response->withHeader('Content-Encoding', "gzip");
$this->isGzipped = true;
}
Expand All @@ -142,14 +146,12 @@ public function run(ResponseInterface $response, RequestInterface $request): voi
{
$this->response = $response;
$this->request = $request;

$stream = $this->buildStream();
// Look for position instead of size, read has already been triggered once
$size = $stream->tell();

if ($size) {
$this->response = $this->response->withHeader('Content-Length', $size);
// Set cache control if do not exist
// Set cache control if it does not exist
if (!$this->response->hasHeader("Cache-Control")) {
// Clear cache on dynamic content is a good standard to make sure
// that no sensitive content will be cached.
Expand All @@ -160,7 +162,7 @@ public function run(ResponseInterface $response, RequestInterface $request): voi
// Will pre execute above headers (Can only be triggered once per instance)
$this->response->createHeaders();

// Detached Body from a HEAD request method but keep the meta data intact.
// Detached Body from a HEAD request method but keep the metadata intact.
if ($this->request->getMethod() === "HEAD") {
$stream->seek(0);
$stream->write("");
Expand All @@ -175,33 +177,4 @@ public function run(ResponseInterface $response, RequestInterface $request): voi
}
}

/**
* Makes error handling easy
* @param bool $displayError Enables error handlning
* @param bool $niceError Nice error reporting (true) vs EmitterException (false)
* @param bool $logError Enable log message (With warning might log even if
* false with EmitterException, depending on you setup)
* @param string|null $logErrorFile Path to log file
* @return void
*/
public function errorHandler(
bool $displayError,
bool $niceError = true,
bool $logError = false,
?string $logErrorFile = null
): void {
$this->errorHandler = new ErrorHandler($displayError, $logError, $logErrorFile);
$this->errorHandler->setHandler(function ($msg, $number, $hasError, $displayError) use ($niceError) {
if ($hasError) {
$this->errorHandlerMsg = $msg;
if ($displayError) {
if ($niceError) {
$this->view->findBind(500);
} else {
throw new EmitterException($this->errorHandlerMsg, $number);
}
}
}
}, $this->errorHandler::CATCH_ALL);
}
}
8 changes: 4 additions & 4 deletions Interfaces/MiddlewareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace MaplePHP\Handler\Interfaces;

use MaplePHP\Http\Interfaces\ResponseInterface;
use MaplePHP\Http\Interfaces\RequestInterface;

use MaplePHP\Http\Response;

interface MiddlewareInterface
{
public function before(ResponseInterface $response, RequestInterface $request);
public function after(ResponseInterface $response, RequestInterface $request);


}
Loading