72 lines
2.3 KiB
PHP
Executable File
72 lines
2.3 KiB
PHP
Executable File
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App;
|
|
|
|
final class App
|
|
{
|
|
private static ?self $instance = null;
|
|
|
|
private Request $request;
|
|
private SessionManager $session;
|
|
private Assets $assets;
|
|
private I18n $i18n;
|
|
private Flash $flash;
|
|
private ?\PDO $pdo;
|
|
private ?\PDO $basePdo;
|
|
private ModuleManager $modules;
|
|
|
|
private function __construct(private Config $config)
|
|
{
|
|
$this->request = new Request();
|
|
$this->session = new SessionManager($config);
|
|
$this->assets = new Assets($config);
|
|
$this->i18n = new I18n($config, 'en');
|
|
$this->flash = new Flash($this->session);
|
|
$this->pdo = Database::createPdo($config);
|
|
$this->basePdo = Database::createBasePdo($config);
|
|
$modulesPath = $this->config->modulesPath;
|
|
if ($modulesPath === '' || !is_dir($modulesPath)) {
|
|
$candidates = [
|
|
__DIR__ . '/../../modules',
|
|
__DIR__ . '/../modules',
|
|
dirname(__DIR__, 3) . '/modules',
|
|
];
|
|
foreach ($candidates as $candidate) {
|
|
if (is_dir($candidate)) {
|
|
$modulesPath = $candidate;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
$this->modules = new ModuleManager($this->basePdo, $modulesPath);
|
|
$this->modules->bootEnabled();
|
|
}
|
|
|
|
public static function init(Config $config): self
|
|
{
|
|
if (self::$instance === null) {
|
|
self::$instance = new self($config);
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public static function get(): self
|
|
{
|
|
if (self::$instance === null) {
|
|
throw new \RuntimeException('App not initialized. Call App::init() in bootstrap.');
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function config(): Config { return $this->config; }
|
|
public function request(): Request { return $this->request; }
|
|
public function session(): SessionManager { return $this->session; }
|
|
public function assets(): Assets { return $this->assets; }
|
|
public function i18n(): I18n { return $this->i18n; }
|
|
public function flash(): Flash { return $this->flash; }
|
|
public function pdo(): ?\PDO { return $this->pdo; }
|
|
public function basePdo(): ?\PDO { return $this->basePdo; }
|
|
public function modules(): ModuleManager { return $this->modules; }
|
|
}
|