hanyuu/src/HanyuuContext.php

92 lines
2.6 KiB
PHP

<?php
namespace Hanyuu;
use Index\Environment;
use Index\Data\IDbConnection;
use Index\Data\Migration\{IDbMigrationRepo,DbMigrationManager,FsDbMigrationRepo};
use Sasae\SasaeEnvironment;
use Syokuhou\IConfig;
use Hanyuu\Auth\{Auth,AuthRoutes};
use Hanyuu\Users\Users;
class HanyuuContext {
private IConfig $config;
private IDbConnection $dbConn;
private Users $users;
private Auth $auth;
private ?SasaeEnvironment $templating = null;
private SiteInfo $siteInfo;
public function __construct(IConfig $config, IDbConnection $dbConn) {
$this->config = $config;
$this->dbConn = $dbConn;
$this->users = new Users($dbConn);
$this->siteInfo = new SiteInfo($config->scopeTo('site'));
}
public function getUsers(): Users {
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(): SasaeEnvironment {
if($this->templating === null) {
$isDebug = Environment::isDebug();
$this->templating = new SasaeEnvironment(
HAU_DIR_TEMPLATES,
cache: null,//$isDebug ? null : ['Hanyuu', GitInfo::hash(true)],
debug: $isDebug,
);
$this->templating->addGlobal('globals', [
'siteInfo' => $this->siteInfo,
//'assetsInfo' => AssetsInfo::fromCurrent(),
]);
}
return $this->templating;
}
public function renderTemplate(...$args): string {
return $this->getTemplating()->render(...$args);
}
public function setUpAuth(): void {
$this->auth = new Auth($this->dbConn);
}
public function getAuth(): Auth {
return $this->auth;
}
public function createRouting(): RoutingContext {
$routingCtx = new RoutingContext($this->getTemplating());
$routingCtx->getRouter()->get('/', function($response, $request) {
return 503;
});
$routingCtx->register(new AuthRoutes($this, $this->config->scopeTo('auth')));
return $routingCtx;
}
}