flash.moe/src/MakaiContext.php

114 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace Makai;
2023-10-12 18:45:11 +00:00
use Index\Environment;
use Index\Data\IDbConnection;
2023-10-13 22:25:40 +00:00
use Index\Data\Migration\IDbMigrationRepo;
use Index\Data\Migration\DbMigrationManager;
use Index\Data\Migration\FsDbMigrationRepo;
2023-10-13 20:33:17 +00:00
use Index\Security\CSRFP;
2023-10-12 18:45:11 +00:00
use Sasae\SasaeEnvironment;
final class MakaiContext {
2023-10-12 18:45:11 +00:00
private IDbConnection $dbConn;
2023-10-12 22:09:04 +00:00
private SasaeEnvironment $templating;
2023-10-13 20:33:17 +00:00
private CSRFP $csrfp;
2023-10-12 22:09:04 +00:00
private SiteInfo $siteInfo;
private Contacts\Contacts $contacts;
private Projects\Projects $projects;
private SSHKeys\SSHKeys $sshKeys;
2023-10-12 18:45:11 +00:00
public function __construct(IDbConnection $dbConn) {
$this->dbConn = $dbConn;
2023-10-12 22:09:04 +00:00
$this->siteInfo = new SiteInfo;
$this->startTemplating();
$this->contacts = new Contacts\Contacts($dbConn);
$this->projects = new Projects\Projects($dbConn);
$this->sshKeys = new SSHKeys\SSHKeys($dbConn);
}
2023-10-12 22:09:04 +00:00
public function getSiteInfo(): SiteInfo {
return $this->siteInfo;
}
2023-10-12 22:09:04 +00:00
public function getContacts(): Contacts\Contacts {
return $this->contacts;
}
2023-10-12 22:09:04 +00:00
public function getProjects(): Projects\Projects {
return $this->projects;
}
2023-10-12 22:09:04 +00:00
public function getSSHKeys(): SSHKeys\SSHKeys {
return $this->sshKeys;
}
2023-10-13 22:25:40 +00:00
public function getDbConn(): IDbConnection {
2023-10-13 20:33:17 +00:00
return $this->dbConn;
}
2023-10-13 22:25:40 +00:00
public function getDbQueryCount(): int {
$result = $this->dbConn->query('SHOW SESSION STATUS LIKE "Questions"');
return $result->next() ? $result->getInteger(1) : 0;
}
public function createMigrationManager(): DbMigrationManager {
return new DbMigrationManager($this->dbConn, 'fm_' . DbMigrationManager::DEFAULT_TABLE);
}
public function createMigrationRepo(): IDbMigrationRepo {
return new FsDbMigrationRepo(MKI_DIR_MIGRATIONS);
}
2023-10-12 22:09:04 +00:00
public function getTemplating(): SasaeEnvironment {
return $this->templating;
}
public function startTemplating(): void {
$isDebug = Environment::isDebug();
$this->templating = new SasaeEnvironment(
MKI_DIR_TEMPLATES,
cache: $isDebug ? null : ['Makai', GitInfo::hash(true)],
debug: $isDebug,
);
2023-10-13 20:33:17 +00:00
$this->templating->addFunction('csrfp_token', fn() => $this->getCSRFP()->createToken());
2023-10-12 22:09:04 +00:00
$this->templating->addGlobal('globals', [
'siteInfo' => $this->siteInfo,
2023-10-13 19:33:34 +00:00
'assetsInfo' => AssetsInfo::fromCurrent(),
2023-10-12 22:09:04 +00:00
]);
}
2023-10-13 20:33:17 +00:00
public function getCSRFP(): CSRFP {
return $this->csrfp;
}
public function startCSRFP(string $secretKey, string $identity): void {
$this->csrfp = new CSRFP($secretKey, $identity);
}
2023-10-12 22:09:04 +00:00
public function createRouting(): RoutingContext {
$routingCtx = new RoutingContext($this->templating);
$routingCtx->registerDefaultErrorPages();
$routingCtx->register(new HomeRoutes($this->templating, $this->contacts, $this->projects));
$routingCtx->register(new Contacts\ContactsRoutes($this->templating, $this->contacts));
$routingCtx->register(new Projects\ProjectsRoutes($this->templating, $this->projects));
$routingCtx->register(new AssetsRoutes($this->siteInfo));
$routingCtx->register(new NowListeningRoutes($this->templating));
2023-10-13 20:33:17 +00:00
$routingCtx->register(new Whois\WhoisRoutes($this->templating, fn() => $this->csrfp));
2023-10-12 22:09:04 +00:00
$routingCtx->register(new SSHKeys\SSHKeysRoutes($this->sshKeys));
$routingCtx->register(new Tools\AsciiRoutes($this->templating));
$routingCtx->register(new Tools\RandomStringRoutes);
return $routingCtx;
}
}