82 lines
2.8 KiB
PHP
82 lines
2.8 KiB
PHP
|
<?php
|
||
|
namespace Hanyuu;
|
||
|
|
||
|
use RuntimeException;
|
||
|
use Index\Data\IDbConnection;
|
||
|
use Index\Data\DbTools;
|
||
|
use Index\Http\HttpFx;
|
||
|
use Index\Http\HttpRequest;
|
||
|
use Index\Routing\IRouter;
|
||
|
use Hanyuu\Config\IConfig;
|
||
|
|
||
|
class HanyuuContext {
|
||
|
private const DB_INIT = 'SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';';
|
||
|
|
||
|
private IConfig $config;
|
||
|
private IDbConnection $dbConn;
|
||
|
|
||
|
public function __construct(IConfig $config) {
|
||
|
$this->config = $config;
|
||
|
}
|
||
|
|
||
|
public function connectDb(?IDbConnection $dbConn = null): void {
|
||
|
$dbConn ??= DbTools::create($this->config->getValue('database:dsn', IConfig::T_STR, 'null'));
|
||
|
$dbConn->execute(self::DB_INIT);
|
||
|
$this->dbConn = $dbConn;
|
||
|
}
|
||
|
|
||
|
public function getDb(): IDbConnection {
|
||
|
return $this->dbConn;
|
||
|
}
|
||
|
|
||
|
public function getDbQueryCount(): int {
|
||
|
$result = $this->dbConn->query('SHOW SESSION STATUS LIKE "Questions"');
|
||
|
return $result->next() ? $result->getInteger(0) : 0;
|
||
|
}
|
||
|
|
||
|
public function setUpHttp(): void {
|
||
|
$this->router = new HttpFx;
|
||
|
$this->router->use('/', function($response) {
|
||
|
$response->setPoweredBy('Hanyuu');
|
||
|
});
|
||
|
|
||
|
$this->registerErrorPages();
|
||
|
$this->registerHttpRoutes();
|
||
|
}
|
||
|
|
||
|
public function dispatchHttp(?HttpRequest $request = null): void {
|
||
|
$this->router->dispatch($request);
|
||
|
}
|
||
|
|
||
|
private function registerErrorPages(): void {
|
||
|
/*$this->router->addErrorHandler(400, function($response) {
|
||
|
$response->setContent(Template::renderRaw('errors.400'));
|
||
|
});
|
||
|
$this->router->addErrorHandler(403, function($response) {
|
||
|
$response->setContent(Template::renderRaw('errors.403'));
|
||
|
});
|
||
|
$this->router->addErrorHandler(404, function($response) {
|
||
|
$response->setContent(Template::renderRaw('errors.404'));
|
||
|
});
|
||
|
$this->router->addErrorHandler(500, function($response) {
|
||
|
$response->setContent(file_get_contents(MSZ_TEMPLATES . '/500.html'));
|
||
|
});
|
||
|
$this->router->addErrorHandler(503, function($response) {
|
||
|
$response->setContent(file_get_contents(MSZ_TEMPLATES . '/503.html'));
|
||
|
});*/
|
||
|
}
|
||
|
|
||
|
private function registerHttpRoutes(): void {
|
||
|
$this->router->get('/', function($response, $request) {
|
||
|
$siteName = $this->config->getValue('site:name', IConfig::T_STR, 'Hanyuu');
|
||
|
|
||
|
return "<!doctype html>\r\n"
|
||
|
. "<title>{$siteName} ID</title>\r\n"
|
||
|
. "<center>\r\n"
|
||
|
. " <h1>Under Construction</h1>\r\n"
|
||
|
. " <img src=\"//static.flash.moe/images/me-tan-2.png\" alt=\"\"/>\r\n"
|
||
|
. "</center>\r\n";
|
||
|
});
|
||
|
}
|
||
|
}
|