74 lines
2.3 KiB
PHP
74 lines
2.3 KiB
PHP
<?php
|
|
namespace Awaki;
|
|
|
|
use Index\Data\IDbConnection;
|
|
use Index\Data\Migration\IDbMigrationRepo;
|
|
use Index\Data\Migration\DbMigrationManager;
|
|
use Index\Data\Migration\FsDbMigrationRepo;
|
|
use Index\Http\HttpFx;
|
|
use Index\Http\HttpRequest;
|
|
use Index\Routing\IRouter;
|
|
|
|
// theme colours: #99403d, #592824, #d18c83
|
|
// totally didn't just eyedrop musujime
|
|
|
|
class AwakiContext {
|
|
private const DB_INIT = 'SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';';
|
|
|
|
private IDbConnection $dbConn;
|
|
private HttpFx $router;
|
|
private array $urls;
|
|
|
|
public function __construct(IDbConnection $dbConn, array $urls) {
|
|
$this->dbConn = $dbConn;
|
|
$this->urls = $urls;
|
|
$dbConn->execute(self::DB_INIT);
|
|
}
|
|
|
|
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, 'awk_' . DbMigrationManager::DEFAULT_TABLE);
|
|
}
|
|
|
|
public function createMigrationRepo(): IDbMigrationRepo {
|
|
return new FsDbMigrationRepo(AWK_DIR_DBM);
|
|
}
|
|
|
|
public function getRouter(): IRouter {
|
|
return $this->router->getRouter();
|
|
}
|
|
|
|
public function setUpHttp(): void {
|
|
$this->router = new HttpFx;
|
|
$this->router->use('/', function($response) {
|
|
$response->setPoweredBy('Awaki');
|
|
});
|
|
|
|
$this->registerErrorPages();
|
|
$this->registerHttpRoutes();
|
|
}
|
|
|
|
public function dispatchHttp(?HttpRequest $request = null): void {
|
|
$this->router->dispatch($request);
|
|
}
|
|
|
|
private function registerErrorPages(): void {
|
|
$this->router->addErrorHandler(404, function($response) {
|
|
$response->accelRedirect('/err404.html');
|
|
$response->setTypeHTML();
|
|
});
|
|
$this->router->addErrorHandler(500, function($response) {
|
|
$response->accelRedirect('/err500.html');
|
|
$response->setTypeHTML();
|
|
});
|
|
}
|
|
|
|
private function registerHttpRoutes(): void {
|
|
new RedirectorRoutes($this->router, $this->dbConn, $this->urls);
|
|
}
|
|
}
|