webcron/lib/Framework/Kernel.php

141 lines
3.3 KiB
PHP
Raw Normal View History

2021-04-06 22:20:18 +02:00
<?php
namespace JeroenED\Framework;
2021-04-06 22:44:39 +02:00
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
2021-04-06 22:20:18 +02:00
use http\Exception\InvalidArgumentException;
2021-04-06 23:19:51 +02:00
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
2021-04-06 22:20:18 +02:00
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouteCollection;
2021-04-06 23:19:51 +02:00
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Dotenv\Dotenv;
2021-04-06 22:20:18 +02:00
class Kernel
{
private string $configDir;
private string $projectDir;
private string $templateDir;
2021-08-02 13:28:14 +02:00
private string $cacheDir;
2021-04-07 13:31:57 +02:00
private Router $router;
2022-03-21 11:30:30 +01:00
private ?Connection $dbCon = NULL;
2021-04-06 22:20:18 +02:00
/**
* @return string
*/
public function getConfigDir(): string
{
return $this->configDir;
}
/**
* @param string $configDir
*/
public function setConfigDir(string $configDir): void
{
$this->configDir = $configDir;
}
/**
* @return string
*/
public function getProjectDir(): string
{
return $this->projectDir;
}
/**
* @param string $projectDir
*/
public function setProjectDir(string $projectDir): void
{
$this->projectDir = $projectDir;
}
/**
* @return string
*/
public function getTemplateDir(): string
{
return $this->templateDir;
2021-04-07 13:31:57 +02:00
}
/**
2021-04-06 22:20:18 +02:00
* @param string $templateDir
*/
public function setTemplateDir(string $templateDir): void
{
$this->templateDir = $templateDir;
}
2021-08-02 13:28:14 +02:00
/**
* @return string
*/
public function getCacheDir(): string
{
return $this->cacheDir;
}
/**
* @param string $cacheDir
*/
public function setCacheDir(string $cacheDir): void
{
$this->cacheDir = $cacheDir;
}
2021-04-07 13:31:57 +02:00
/**
* @return Router
*/
public function getRouter(): Router
{
return $this->router;
}
2021-04-06 22:20:18 +02:00
public function handle(): Response
{
2021-04-07 13:31:57 +02:00
$this->router = new Router();
$this->router->parseRoutes($this->getConfigDir(), 'routes.yaml');
2021-04-06 22:20:18 +02:00
$request = $this->parseRequest();
2022-01-31 15:25:08 +01:00
if($request->isSecure()) {
ini_set('session.cookie_httponly', true);
ini_set('session.cookie_secure', true);
}
session_start();
2021-04-07 13:31:57 +02:00
return $this->router->route($request, $this);
2021-04-06 22:20:18 +02:00
}
2021-05-24 18:36:16 +02:00
public function parseDotEnv(string $path): void
2021-04-06 22:20:18 +02:00
{
$dotenv = new Dotenv();
$dotenv->loadEnv($path);
}
private function parseRequest(): Request
{
2021-05-26 15:25:02 +02:00
Request::setTrustedProxies(explode(',', $_ENV['TRUSTED_PROXIES']), Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO);
2021-04-07 13:31:57 +02:00
$request = Request::createFromGlobals();
return $request;
2021-04-06 22:20:18 +02:00
}
2022-03-21 11:30:30 +01:00
public function getNewDbCon(): Connection {
if(!is_null($this->dbCon)) {
$this->dbCon->close();
$this->dbCon = null;
}
2022-03-21 11:30:30 +01:00
$this->dbCon = DriverManager::getConnection(['url' => $_ENV['DATABASE']]);
return $this->dbCon;
}
2021-04-06 22:44:39 +02:00
public function getDbCon(): Connection
{
2022-03-21 11:30:30 +01:00
if(is_null($this->dbCon)) $this->dbCon = DriverManager::getConnection(['url' => $_ENV['DATABASE']]);
return $this->dbCon;
2021-04-06 22:44:39 +02:00
}
2021-04-06 22:20:18 +02:00
}