diff --git a/misuzu.php b/misuzu.php index fcb8054a..c999710b 100644 --- a/misuzu.php +++ b/misuzu.php @@ -3,6 +3,8 @@ namespace Misuzu; require_once 'vendor/autoload.php'; -$app = Application::start(__DIR__ . '/config/config.ini'); -$app->debug(IO\Directory::exists(__DIR__ . '/vendor/phpunit/phpunit')); +$app = Application::start( + __DIR__ . '/config/config.ini', + IO\Directory::exists(__DIR__ . '/vendor/phpunit/phpunit') +); $app->startDatabase(); diff --git a/src/Application.php b/src/Application.php index a3867e2c..0e9da47f 100644 --- a/src/Application.php +++ b/src/Application.php @@ -4,63 +4,15 @@ namespace Misuzu; use Aitemu\RouteCollection; use Misuzu\Config\ConfigManager; -class Application +class Application extends ApplicationBase { - private static $instance = null; + private $debugMode = false; - public static function getInstance(): Application - { - if (is_null(static::$instance) || !(static::$instance instanceof Application)) { - throw new \Exception('Invalid instance type.'); - } - - return static::$instance; - } - - public static function start(...$params): Application - { - if (!is_null(static::$instance) || static::$instance instanceof Application) { - throw new \Exception('An Application has already been set up.'); - } - - static::$instance = new Application(...$params); - return static::getInstance(); - } - - public static function gitCommitInfo(string $format): string - { - return trim(shell_exec(sprintf('git log --pretty="%s" -n1 HEAD', $format))); - } - - public static function gitCommitHash(bool $long = false): string - { - return self::gitCommitInfo($long ? '%H' : '%h'); - } - - public static function gitBranch(): string - { - return trim(shell_exec('git rev-parse --abbrev-ref HEAD')); - } - - private $modules = []; - - public function __get($name) - { - if (starts_with($name, 'has') && strlen($name) > 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) + protected function __construct($configFile = null, bool $debug = false) { + $this->debugMode = $debug; ExceptionHandler::register(); + ExceptionHandler::debug($this->debugMode); $this->addModule('config', new ConfigManager($configFile)); } @@ -96,15 +48,20 @@ class Application } $this->addModule('templating', $twig = new TemplateEngine); + $twig->debug($this->debugMode); $twig->addFilter('json_decode'); $twig->addFilter('byte_symbol'); + $twig->addFunction('byte_symbol'); $twig->addFunction('session_id'); $twig->addFunction('config', [$this->config, 'get']); $twig->addFunction('route', [$this->router, 'url']); $twig->addFunction('git_hash', [Application::class, 'gitCommitHash']); $twig->addFunction('git_branch', [Application::class, 'gitBranch']); + + $twig->vars(['app' => $this]); + $twig->addPath('nova', __DIR__ . '/../views/nova'); } @@ -146,27 +103,4 @@ class Application 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]); - } } diff --git a/src/ApplicationBase.php b/src/ApplicationBase.php new file mode 100644 index 00000000..1c58a62c --- /dev/null +++ b/src/ApplicationBase.php @@ -0,0 +1,70 @@ + 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.'); + } + + 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]); + } +}