Split Application class up with an abstract base.

This commit is contained in:
flash 2018-01-04 21:01:55 +01:00
parent c7186d040b
commit 7b54d1141f
3 changed files with 84 additions and 78 deletions

View file

@ -3,6 +3,8 @@ namespace Misuzu;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
$app = Application::start(__DIR__ . '/config/config.ini'); $app = Application::start(
$app->debug(IO\Directory::exists(__DIR__ . '/vendor/phpunit/phpunit')); __DIR__ . '/config/config.ini',
IO\Directory::exists(__DIR__ . '/vendor/phpunit/phpunit')
);
$app->startDatabase(); $app->startDatabase();

View file

@ -4,63 +4,15 @@ namespace Misuzu;
use Aitemu\RouteCollection; use Aitemu\RouteCollection;
use Misuzu\Config\ConfigManager; use Misuzu\Config\ConfigManager;
class Application class Application extends ApplicationBase
{ {
private static $instance = null; private $debugMode = false;
public static function getInstance(): Application protected function __construct($configFile = null, bool $debug = false)
{
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)
{ {
$this->debugMode = $debug;
ExceptionHandler::register(); ExceptionHandler::register();
ExceptionHandler::debug($this->debugMode);
$this->addModule('config', new ConfigManager($configFile)); $this->addModule('config', new ConfigManager($configFile));
} }
@ -96,15 +48,20 @@ class Application
} }
$this->addModule('templating', $twig = new TemplateEngine); $this->addModule('templating', $twig = new TemplateEngine);
$twig->debug($this->debugMode);
$twig->addFilter('json_decode'); $twig->addFilter('json_decode');
$twig->addFilter('byte_symbol'); $twig->addFilter('byte_symbol');
$twig->addFunction('byte_symbol'); $twig->addFunction('byte_symbol');
$twig->addFunction('session_id'); $twig->addFunction('session_id');
$twig->addFunction('config', [$this->config, 'get']); $twig->addFunction('config', [$this->config, 'get']);
$twig->addFunction('route', [$this->router, 'url']); $twig->addFunction('route', [$this->router, 'url']);
$twig->addFunction('git_hash', [Application::class, 'gitCommitHash']); $twig->addFunction('git_hash', [Application::class, 'gitCommitHash']);
$twig->addFunction('git_branch', [Application::class, 'gitBranch']); $twig->addFunction('git_branch', [Application::class, 'gitBranch']);
$twig->vars(['app' => $this]);
$twig->addPath('nova', __DIR__ . '/../views/nova'); $twig->addPath('nova', __DIR__ . '/../views/nova');
} }
@ -146,27 +103,4 @@ class Application
throw new \Exception('No database connections have been configured.'); 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]);
}
} }

70
src/ApplicationBase.php Normal file
View file

@ -0,0 +1,70 @@
<?php
namespace Misuzu;
abstract class ApplicationBase
{
private static $instance = null;
private $modules = [];
public static function getInstance(): Application
{
if (is_null(self::$instance) || !(self::$instance instanceof Application)) {
throw new \Exception('Invalid instance type.');
}
return self::$instance;
}
public static function start(...$params): Application
{
if (!is_null(self::$instance) || self::$instance instanceof Application) {
throw new \Exception('An Application has already been set up.');
}
self::$instance = new static(...$params);
return self::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'));
}
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.');
}
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]);
}
}