2018-01-02 21:26:33 +01:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2018-01-02 21:57:06 +01:00
|
|
|
use Aitemu\RouteCollection;
|
2018-01-02 21:26:33 +01:00
|
|
|
use Misuzu\Config\ConfigManager;
|
|
|
|
|
|
|
|
class Application
|
|
|
|
{
|
|
|
|
private static $instance = null;
|
|
|
|
|
|
|
|
public static function getInstance(): Application
|
|
|
|
{
|
|
|
|
if (is_null(static::$instance) || !(static::$instance instanceof Application)) {
|
|
|
|
throw new \Exception('Invalid instance type.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return static::$instance;
|
|
|
|
}
|
|
|
|
|
2018-01-03 20:50:29 +01:00
|
|
|
public static function start(...$params): Application
|
2018-01-02 21:26:33 +01:00
|
|
|
{
|
|
|
|
if (!is_null(static::$instance) || static::$instance instanceof Application) {
|
|
|
|
throw new \Exception('An Application has already been set up.');
|
|
|
|
}
|
|
|
|
|
2018-01-03 20:50:29 +01:00
|
|
|
static::$instance = new Application(...$params);
|
2018-01-02 21:26:33 +01:00
|
|
|
return static::getInstance();
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
public static function gitCommitInfo(string $format): string
|
2018-01-02 21:26:33 +01:00
|
|
|
{
|
2018-01-03 02:15:43 +01:00
|
|
|
return trim(shell_exec(sprintf('git log --pretty="%s" -n1 HEAD', $format)));
|
2018-01-03 02:12:28 +01:00
|
|
|
}
|
2018-01-02 21:26:33 +01:00
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
public static function gitCommitHash(bool $long = false): string
|
|
|
|
{
|
|
|
|
return self::gitCommitInfo($long ? '%H' : '%h');
|
2018-01-02 21:26:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
public static function gitBranch(): string
|
2018-01-02 21:26:33 +01:00
|
|
|
{
|
2018-01-03 02:12:28 +01:00
|
|
|
return trim(shell_exec('git rev-parse --abbrev-ref HEAD'));
|
2018-01-02 21:26:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
private $modules = [];
|
|
|
|
|
|
|
|
public function __get($name)
|
2018-01-02 21:26:33 +01:00
|
|
|
{
|
2018-01-03 02:12:28 +01:00
|
|
|
if (starts_with($name, 'has') && strlen($name) > 3 && ctype_upper($name[3])) {
|
|
|
|
$name = lcfirst(substr($name, 3));
|
|
|
|
return $this->hasModule($name);
|
|
|
|
}
|
2018-01-02 21:26:33 +01:00
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
if ($this->hasModule($name)) {
|
|
|
|
return $this->modules[$name];
|
2018-01-02 21:26:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
throw new \Exception('Invalid property.');
|
2018-01-02 21:57:06 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 17:49:20 +01:00
|
|
|
protected function __construct($configFile = null)
|
2018-01-02 21:57:06 +01:00
|
|
|
{
|
2018-01-03 02:12:28 +01:00
|
|
|
ExceptionHandler::register();
|
2018-01-03 20:50:29 +01:00
|
|
|
$this->debug(true);
|
2018-01-02 21:57:06 +01:00
|
|
|
|
2018-01-03 20:50:29 +01:00
|
|
|
$this->addModule('config', $config = new ConfigManager($configFile));
|
|
|
|
$this->addModule('database', new Database(
|
|
|
|
$config,
|
|
|
|
$config->get('Database', 'default', 'string', 'default')
|
|
|
|
));
|
2018-01-03 17:49:20 +01:00
|
|
|
$this->addModule('router', $router = new RouteCollection);
|
|
|
|
$this->addModule('templating', $twig = new TemplateEngine);
|
2018-01-03 20:50:29 +01:00
|
|
|
|
|
|
|
$this->loadConfigDatabaseConnections();
|
2018-01-03 17:49:20 +01:00
|
|
|
|
|
|
|
$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');
|
2018-01-02 21:57:06 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
public function __destruct()
|
2018-01-02 21:26:33 +01:00
|
|
|
{
|
2018-01-03 20:50:29 +01:00
|
|
|
if ($this->hasConfig) {
|
|
|
|
$this->config->save();
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
ExceptionHandler::unregister();
|
2018-01-02 21:26:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 20:50:29 +01:00
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
public function debug(bool $mode): void
|
2018-01-02 21:26:33 +01:00
|
|
|
{
|
2018-01-03 02:12:28 +01:00
|
|
|
ExceptionHandler::debug($mode);
|
2018-01-02 21:26:33 +01:00
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
if ($this->hasTemplating) {
|
|
|
|
$this->templating->debug($mode);
|
|
|
|
}
|
2018-01-02 21:26:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
public function addModule(string $name, $module): void
|
2018-01-02 21:26:33 +01:00
|
|
|
{
|
2018-01-03 02:12:28 +01:00
|
|
|
if ($this->hasModule($name)) {
|
|
|
|
throw new \Exception('This module has already been registered.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->modules[$name] = $module;
|
2018-01-02 21:26:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 02:12:28 +01:00
|
|
|
public function hasModule(string $name): bool
|
2018-01-02 21:26:33 +01:00
|
|
|
{
|
2018-01-03 02:12:28 +01:00
|
|
|
return array_key_exists($name, $this->modules) && !is_null($this->modules[$name]);
|
2018-01-02 21:26:33 +01:00
|
|
|
}
|
|
|
|
}
|