misuzu/src/Application.php

106 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace Misuzu;
use Aitemu\RouteCollection;
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;
}
public static function start(): Application
{
if (!is_null(static::$instance) || static::$instance instanceof Application) {
throw new \Exception('An Application has already been set up.');
}
static::$instance = new Application;
return static::getInstance();
}
2018-01-03 02:12:28 +01:00
public static function gitCommitInfo(string $format): string
{
return trim(shell_exec(sprintf('git log --pretty="%s" -n1 HEAD', $format)));
2018-01-03 02:12:28 +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-03 02:12:28 +01:00
public static function gitBranch(): string
{
2018-01-03 02:12:28 +01:00
return trim(shell_exec('git rev-parse --abbrev-ref HEAD'));
}
2018-01-03 02:12:28 +01:00
private $modules = [];
public function __get($name)
{
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-03 02:12:28 +01:00
if ($this->hasModule($name)) {
return $this->modules[$name];
}
2018-01-03 02:12:28 +01:00
throw new \Exception('Invalid property.');
}
2018-01-03 02:12:28 +01:00
protected function __construct($config = null)
{
2018-01-03 02:12:28 +01:00
ExceptionHandler::register();
2018-01-03 02:12:28 +01:00
$this->addModule('router', new RouteCollection);
$this->addModule('templating', new TemplateEngine);
$this->addModule('config', new ConfigManager($config));
$this->templating->addFilter('json_decode');
$this->templating->addFilter('byte_symbol');
$this->templating->addFunction('byte_symbol');
$this->templating->addFunction('session_id');
$this->templating->addFunction('config', [$this->config, 'get']);
$this->templating->addFunction('route', [$this->router, 'url']);
2018-01-03 03:02:55 +01:00
$this->templating->addPath('nova', __DIR__ . '/../views/nova');
}
2018-01-03 02:12:28 +01:00
public function __destruct()
{
2018-01-03 02:12:28 +01:00
ExceptionHandler::unregister();
}
2018-01-03 02:12:28 +01:00
public function debug(bool $mode): void
{
2018-01-03 02:12:28 +01:00
ExceptionHandler::debug($mode);
2018-01-03 02:12:28 +01:00
if ($this->hasTemplating) {
$this->templating->debug($mode);
}
}
2018-01-03 02:12:28 +01:00
public function addModule(string $name, $module): void
{
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-03 02:12:28 +01:00
public function hasModule(string $name): bool
{
2018-01-03 02:12:28 +01:00
return array_key_exists($name, $this->modules) && !is_null($this->modules[$name]);
}
}