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 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 20:33:17 +00:00
|
|
|
public function getDatabase(): IDbConnection {
|
|
|
|
return $this->dbConn;
|
|
|
|
}
|
|
|
|
|
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;
|
2022-09-26 23:06:12 +00:00
|
|
|
}
|
|
|
|
}
|