hanyuu/src/HanyuuContext.php

92 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2023-01-06 22:20:13 +00:00
<?php
namespace Hanyuu;
use Index\Environment;
2023-01-06 22:20:13 +00:00
use Index\Data\IDbConnection;
use Index\Data\Migration\{IDbMigrationRepo,DbMigrationManager,FsDbMigrationRepo};
use Sasae\SasaeEnvironment;
use Syokuhou\IConfig;
use Hanyuu\Auth\{Auth,AuthRoutes};
2023-10-20 22:11:43 +00:00
use Hanyuu\Users\Users;
2023-01-06 22:20:13 +00:00
class HanyuuContext {
private IConfig $config;
private IDbConnection $dbConn;
2023-10-20 22:11:43 +00:00
private Users $users;
2023-10-18 10:34:30 +00:00
private Auth $auth;
private ?SasaeEnvironment $templating = null;
private SiteInfo $siteInfo;
2023-01-06 22:20:13 +00:00
2023-01-09 21:44:34 +00:00
public function __construct(IConfig $config, IDbConnection $dbConn) {
2023-01-06 22:20:13 +00:00
$this->config = $config;
2023-01-09 21:44:34 +00:00
$this->dbConn = $dbConn;
2023-10-20 22:11:43 +00:00
$this->users = new Users($dbConn);
2023-01-06 22:20:13 +00:00
$this->siteInfo = new SiteInfo($config->scopeTo('site'));
}
2023-10-20 22:11:43 +00:00
public function getUsers(): Users {
2023-01-09 21:44:34 +00:00
return $this->users;
2023-01-06 22:20:13 +00:00
}
2023-01-09 21:44:34 +00:00
public function getDatabase(): IDbConnection {
2023-01-06 22:20:13 +00:00
return $this->dbConn;
}
public function getDbQueryCount(): int {
$result = $this->dbConn->query('SHOW SESSION STATUS LIKE "Questions"');
return $result->next() ? $result->getInteger(0) : 0;
}
2023-01-07 04:36:22 +00:00
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);
}
2023-10-18 10:34:30 +00:00
public function setUpAuth(): void {
2023-10-20 22:11:43 +00:00
$this->auth = new Auth($this->dbConn);
2023-10-18 10:34:30 +00:00
}
public function getAuth(): Auth {
return $this->auth;
}
public function createRouting(): RoutingContext {
$routingCtx = new RoutingContext($this->getTemplating());
2023-01-06 22:20:13 +00:00
$routingCtx->getRouter()->get('/', function($response, $request) {
return 503;
2023-01-06 22:20:13 +00:00
});
2023-01-09 21:44:34 +00:00
$routingCtx->register(new AuthRoutes($this, $this->config->scopeTo('auth')));
2023-01-09 21:44:34 +00:00
return $routingCtx;
2023-01-06 22:20:13 +00:00
}
}