Skip to content

Commit

Permalink
Refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
withinboredom committed Feb 28, 2023
1 parent fe111f6 commit 9aaf8a0
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(
protected array $dependencyInjection = [],
bool $registerErrorHandler = true
) {
if($registerErrorHandler) {
if ($registerErrorHandler) {
set_error_handler(function ($errno, $errstr, $errfile, $errline): bool {
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
Expand All @@ -63,11 +63,9 @@ public function __construct(

public function run(): void
{
if(!headers_sent()) {
header('Vary: Accept-Language, Accept-Encoding, Accept');
header('X-Frame-Options: DENY');
header('X-Content-Type-Options: nosniff');
}
$this->setHeader('Vary', 'Accept-Language, Accept-Encoding, Accept');
$this->setHeader('X-Frame-Options', 'DENY');
$this->setHeader('X-Content-Type-Options', 'nosniff');

$this->createContainer();

Expand All @@ -77,9 +75,7 @@ public function run(): void
*/
$language = $this->container->get(LanguageAcceptor::class);
$language->loadLanguage();
if(!headers_sent()) {
header('Content-Language: ' . $language->currentLanguage);
}
$this->setHeader('Content-Language', $language->currentLanguage);
$router = new MagicRouter($this->container, $this->indexClass);
$response = $router->go();

Expand All @@ -91,7 +87,7 @@ public function run(): void

if (!empty($cacheResult) && method_exists($cacheResult, 'render')) {
if ($this->debug) {
header('Cache-Tag-Rendered: ' . $cacheResult->tag);
$this->setHeader('Cache-Tag-Rendered', $cacheResult->tag);
}
$cacheResult->render($router->lastEtag);
if ($cacheResult->etagRequired && !empty($router->lastEtag)) {
Expand All @@ -112,6 +108,13 @@ public function run(): void
http_response_code(404);
}

protected function setHeader(string $name, string $value): void
{
if (!headers_sent()) {
header($name . ': ' . $value);
}
}

protected function createContainer(): ContainerInterface
{
$builder = new ContainerBuilder();
Expand Down

0 comments on commit 9aaf8a0

Please sign in to comment.