good bye ApplicationBase!

This commit is contained in:
flash 2018-09-16 01:32:18 +02:00
parent b82e51b38e
commit f8ccb1b06a
2 changed files with 21 additions and 44 deletions

View file

@ -17,8 +17,10 @@ use GeoIp2\Database\Reader as GeoIP;
* Handles the set up procedures.
* @package Misuzu
*/
class Application extends ApplicationBase
final class Application
{
private static $instance = null;
/**
* Whether the application is in debug mode, this should only be set in the constructor and never altered.
* @var bool
@ -71,8 +73,12 @@ class Application extends ApplicationBase
*/
public function __construct(?string $configFile = null, bool $debug = false)
{
if (!empty(self::$instance)) {
throw new UnexpectedValueException('An Application has already been set up.');
}
self::$instance = $this;
$this->startupTime = microtime(true);
parent::__construct();
$this->debugMode = $debug;
$this->config = parse_ini_file($configFile, true, INI_SCANNER_TYPED);
@ -423,4 +429,17 @@ class Application extends ApplicationBase
{
return $this->getPath($this->config['Avatar']['default_path'] ?? 'public/images/no-avatar.png');
}
/**
* Gets the currently active instance of Application
* @return Application
*/
public static function getInstance(): Application
{
if (empty(self::$instance)) {
throw new UnexpectedValueException('No instances.');
}
return self::$instance;
}
}

View file

@ -1,42 +0,0 @@
<?php
namespace Misuzu;
use InvalidArgumentException;
use UnexpectedValueException;
/**
* Contains all non-specific methods, for possibly using Misuzu as a framework for other things.
*/
abstract class ApplicationBase
{
/**
* Things extending ApplicationBase are single instance, this property contains the active one.
* @var ApplicationBase
*/
private static $instance = null;
/**
* Gets the currently active instance of ApplicationBase
* @return ApplicationBase
*/
public static function getInstance(): ApplicationBase
{
if (is_null(self::$instance) || !(self::$instance instanceof ApplicationBase)) {
throw new UnexpectedValueException('Invalid instance type.');
}
return self::$instance;
}
/**
* ApplicationBase constructor.
*/
public function __construct()
{
if (!is_null(self::$instance) || self::$instance instanceof ApplicationBase) {
throw new UnexpectedValueException('An Application has already been set up.');
}
self::$instance = $this;
}
}