hanyuu/src/HanyuuContext.php
2023-10-18 10:34:30 +00:00

127 lines
3.9 KiB
PHP

<?php
namespace Hanyuu;
use Index\Data\IDbConnection;
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\Auth\Auth;
use Hanyuu\Auth\AuthRoutes;
use Hanyuu\Auth\Db\DbAuth;
use Hanyuu\Config\IConfig;
use Hanyuu\Templating\TemplateContext;
use Hanyuu\Users\IUsers;
use Hanyuu\Users\Db\DbUsers;
class HanyuuContext {
private IConfig $config;
private IDbConnection $dbConn;
private IUsers $users;
private Auth $auth;
private ?TemplateContext $tpl = null;
public function __construct(IConfig $config, IDbConnection $dbConn) {
$this->config = $config;
$this->dbConn = $dbConn;
$this->users = new DbUsers($dbConn);
}
public function getSiteName(): string {
return $this->config->getValue('site:name', IConfig::T_STR, 'Hanyuu');
}
public function getUsers(): IUsers {
return $this->users;
}
public function getDatabase(): 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 setUpAuth(): void {
$this->auth = new DbAuth($this->dbConn);
}
public function getAuth(): Auth {
return $this->auth;
}
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;
});
$this->router->get('/coffee', function() { return 418; });
new AuthRoutes($this->router, $this, $this->config->scopeTo('auth'));
}
}