3 && ctype_upper($name[3])) { $name = lcfirst(substr($name, 3)); return $this->hasModule($name); } if ($this->hasModule($name)) { return $this->modules[$name]; } throw new \Exception('Invalid property.'); } protected function __construct($configFile = null) { ExceptionHandler::register(); $this->debug(true); $this->addModule('config', $config = new ConfigManager($configFile)); $this->addModule('database', new Database( $config, $config->get('Database', 'default', 'string', 'default') )); $this->addModule('router', $router = new RouteCollection); $this->addModule('templating', $twig = new TemplateEngine); $this->loadConfigDatabaseConnections(); $twig->addFilter('json_decode'); $twig->addFilter('byte_symbol'); $twig->addFunction('byte_symbol'); $twig->addFunction('session_id'); $twig->addFunction('config', [$config, 'get']); $twig->addFunction('route', [$router, 'url']); $twig->addFunction('git_hash', [Application::class, 'gitCommitHash']); $twig->addFunction('git_branch', [Application::class, 'gitBranch']); $twig->addPath('nova', __DIR__ . '/../views/nova'); } public function __destruct() { if ($this->hasConfig) { $this->config->save(); } ExceptionHandler::unregister(); } private function loadConfigDatabaseConnections(): void { $config = $this->config; $database = $this->database; if ($config->contains('Database', 'connections')) { $connections = explode(' ', $config->get('Database', 'connections')); foreach ($connections as $name) { $section = 'Database.' . $name; if (!$config->contains($section)) { continue; } $database->addConnectionFromConfig($section, $name); } } else { throw new \Exception('No database connections have been configured.'); } } public function debug(bool $mode): void { ExceptionHandler::debug($mode); if ($this->hasTemplating) { $this->templating->debug($mode); } } public function addModule(string $name, $module): void { if ($this->hasModule($name)) { throw new \Exception('This module has already been registered.'); } $this->modules[$name] = $module; } public function hasModule(string $name): bool { return array_key_exists($name, $this->modules) && !is_null($this->modules[$name]); } }