hanyuu/src/HanyuuContext.php

111 lines
3.6 KiB
PHP

<?php
namespace Hanyuu;
use Index\Data\IDbConnection;
use Index\Data\DbTools;
use Index\Data\Migration\IDbMigrationRepo;
use Index\Data\Migration\DbMigrationManager;
use Index\Data\Migration\FsDbMigrationRepo;
use Index\Http\HttpFx;
use Index\Http\HttpRequest;
use Index\Routing\IRouter;
use Hanyuu\Config\IConfig;
use Hanyuu\Templating\TemplateContext;
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;
private ?TemplateContext $tpl = null;
public function __construct(IConfig $config) {
$this->config = $config;
}
public function getSiteName(): string {
return $this->config->getValue('site:name', IConfig::T_STR, 'Hanyuu');
}
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 createMigrationManager(): DbMigrationManager {
return new DbMigrationManager($this->dbConn, 'hau_' . DbMigrationManager::DEFAULT_TABLE);
}
public function createMigrationRepo(): IDbMigrationRepo {
return new FsDbMigrationRepo(HAU_DIR_MIGRATIONS);
}
public function getTemplating(): TemplateContext {
if($this->tpl === null) {
$this->tpl = new TemplateContext(HAU_DIR_TEMPLATES);
$this->tpl->setGlobal('hau', $this);
}
return $this->tpl;
}
public function renderTemplate(...$args): string {
return $this->getTemplating()->render(...$args);
}
public function getRouter(): IRouter {
return $this->router->getRouter();
}
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($this->renderTemplate('errors/400'));
});
$this->router->addErrorHandler(401, function($response) {
$response->setContent($this->renderTemplate('errors/401'));
});
$this->router->addErrorHandler(403, function($response) {
$response->setContent($this->renderTemplate('errors/403'));
});
$this->router->addErrorHandler(404, function($response) {
$response->setContent($this->renderTemplate('errors/404'));
});
$this->router->addErrorHandler(500, function($response) {
$response->setContent(file_get_contents(HAU_DIR_TEMPLATES . '/errors/500.html'));
});
$this->router->addErrorHandler(503, function($response) {
$response->setContent(file_get_contents(HAU_DIR_TEMPLATES . '/errors/503.html'));
});
}
private function registerHttpRoutes(): void {
$this->router->get('/', function($response, $request) {
return 503;
});
}
}