seria/src/SeriaContext.php
2024-03-30 00:38:44 +00:00

122 lines
3.9 KiB
PHP

<?php
namespace Seria;
use InvalidArgumentException;
use Index\Environment;
use Index\Data\IDbTransactions;
use Index\Data\Migration\{IDbMigrationRepo,DbMigrationManager,FsDbMigrationRepo};
use Index\Security\CSRFP;
use Sasae\SasaeEnvironment;
use Syokuhou\IConfig;
use Seria\Auth\AuthInfo;
use Seria\Torrents\TorrentsContext;
use Seria\Users\UsersContext;
final class SeriaContext {
private IDbTransactions $dbConn;
private IConfig $config;
private ?SasaeEnvironment $templating = null;
private AuthInfo $authInfo;
private SiteInfo $siteInfo;
private CSRFP $csrfp;
private TorrentsContext $torrentsCtx;
private UsersContext $usersCtx;
public function __construct(IDbTransactions $dbConn, IConfig $config) {
$this->dbConn = $dbConn;
$this->config = $config;
$this->authInfo = new AuthInfo;
$this->siteInfo = new SiteInfo($config->scopeTo('site'));
$this->torrentsCtx = new TorrentsContext($dbConn);
$this->usersCtx = new UsersContext($dbConn);
}
public function getDbConn(): IDbTransactions {
return $this->dbConn;
}
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, 'ser_' . DbMigrationManager::DEFAULT_TABLE);
}
public function createMigrationRepo(): IDbMigrationRepo {
return new FsDbMigrationRepo(SERIA_DIR_MIGRATIONS);
}
public function getAuthInfo(): AuthInfo {
return $this->authInfo;
}
public function getSiteInfo(): SiteInfo {
return $this->siteInfo;
}
public function getTorrentsContext(): TorrentsContext {
return $this->torrentsCtx;
}
public function getUsersContext(): UsersContext {
return $this->usersCtx;
}
public function getTemplating(): ?SasaeEnvironment {
return $this->templating;
}
public function getCSRFP(): CSRFP {
return $this->csrfp;
}
public function startCSRFP(string $secretKey, string $identity): void {
$this->csrfp = new CSRFP($secretKey, $identity);
}
public function startTemplating(): void {
$isDebug = Environment::isDebug();
$globals = [
'auth_info' => $this->authInfo,
'site_info' => $this->siteInfo,
'display_timings_info' => $isDebug, // + isFlash
];
$this->templating = new SasaeEnvironment(
SERIA_DIR_TEMPLATES,
cache: $isDebug ? null : ['Seria', GitInfo::hash(true)],
debug: $isDebug,
);
$this->templating->addExtension(new SeriaSasaeExtension($this, $this->siteInfo));
$this->templating->addGlobal('globals', $globals);
}
public function createRouting(): RoutingContext {
$routing = new RoutingContext($this);
$routing->register(new HomeRoutes($this->templating));
$routing->register(new Users\ProfileRoutes($this->authInfo, $this->torrentsCtx, $this->usersCtx, $this->templating));
$routing->register(new Users\SettingsRoutes($this->authInfo, $this->usersCtx, $this->csrfp, $this->templating));
$routing->register(new Torrents\AnnounceRouting($this->torrentsCtx, $this->usersCtx));
$routing->register(new Torrents\TorrentCreateRouting($this->dbConn, $this->authInfo, $this->torrentsCtx, $this->csrfp, $this->templating));
$routing->register(new Torrents\TorrentInfoRouting(
$this->config->scopeTo('announce'),
$this->authInfo,
$this->torrentsCtx,
$this->usersCtx,
$this->csrfp,
$this->templating
));
$routing->register(new Torrents\TorrentListRouting($this->authInfo, $this->torrentsCtx, $this->usersCtx, $this->templating));
return $routing;
}
}