2022-09-26 23:06:12 +00:00
|
|
|
<?php
|
|
|
|
namespace Makai;
|
|
|
|
|
2023-10-12 18:45:11 +00:00
|
|
|
use Index\Environment;
|
2022-09-26 23:06:12 +00:00
|
|
|
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;
|
2022-09-26 23:06:12 +00:00
|
|
|
|
|
|
|
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;
|
2022-09-26 23:06:12 +00:00
|
|
|
|
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);
|
2022-09-26 23:06:12 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 22:09:04 +00:00
|
|
|
public function getSiteInfo(): SiteInfo {
|
|
|
|
return $this->siteInfo;
|
|
|
|
}
|
2022-09-26 23:06:12 +00:00
|
|
|
|
2023-10-12 22:09:04 +00:00
|
|
|
public function getContacts(): Contacts\Contacts {
|
|
|
|
return $this->contacts;
|
2022-09-26 23:06:12 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 22:09:04 +00:00
|
|
|
public function getProjects(): Projects\Projects {
|
|
|
|
return $this->projects;
|
2022-09-26 23:06:12 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 22:09:04 +00:00
|
|
|
public function getSSHKeys(): SSHKeys\SSHKeys {
|
|
|
|
return $this->sshKeys;
|
2022-09-26 23:06:12 +00:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2023-10-14 00:41:19 +00:00
|
|
|
$routingCtx->register(new DeveloperRoutes($this->templating, $this->contacts, $this->projects));
|
2023-10-12 22:09:04 +00:00
|
|
|
$routingCtx->register(new AssetsRoutes($this->siteInfo));
|
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;
|
2022-09-26 23:06:12 +00:00
|
|
|
}
|
|
|
|
}
|