Change database connection setup model a little.
This commit is contained in:
parent
7b54d1141f
commit
e012221046
5 changed files with 117 additions and 53 deletions
|
@ -1,9 +1,5 @@
|
||||||
; Example configuration for Misuzu
|
; Example configuration for Misuzu
|
||||||
|
|
||||||
[Database]
|
|
||||||
default = mysql_example
|
|
||||||
connections = mysql_example sqlite_example postgres_example sqlsrv_example
|
|
||||||
|
|
||||||
[Database.mysql_example]
|
[Database.mysql_example]
|
||||||
driver = mysql
|
driver = mysql
|
||||||
host = localhost
|
host = localhost
|
||||||
|
|
|
@ -4,11 +4,31 @@ namespace Misuzu;
|
||||||
use Aitemu\RouteCollection;
|
use Aitemu\RouteCollection;
|
||||||
use Misuzu\Config\ConfigManager;
|
use Misuzu\Config\ConfigManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the set up procedures.
|
||||||
|
*/
|
||||||
class Application extends ApplicationBase
|
class Application extends ApplicationBase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Whether the application is in debug mode, this should only be set in the constructor and never altered.
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
private $debugMode = false;
|
private $debugMode = false;
|
||||||
|
|
||||||
protected function __construct($configFile = null, bool $debug = false)
|
/**
|
||||||
|
* Array of database connection names, first in the list is assumed to be the default.
|
||||||
|
*/
|
||||||
|
private const DATABASE_CONNECTIONS = [
|
||||||
|
'mysql-main',
|
||||||
|
//'mysql-ayase',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor, called by ApplicationBase::start() which also passes the arguments through.
|
||||||
|
* @param ?string $configFile
|
||||||
|
* @param bool $debug
|
||||||
|
*/
|
||||||
|
protected function __construct(?string $configFile = null, bool $debug = false)
|
||||||
{
|
{
|
||||||
$this->debugMode = $debug;
|
$this->debugMode = $debug;
|
||||||
ExceptionHandler::register();
|
ExceptionHandler::register();
|
||||||
|
@ -25,22 +45,41 @@ class Application extends ApplicationBase
|
||||||
ExceptionHandler::unregister();
|
ExceptionHandler::unregister();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up the database module.
|
||||||
|
*/
|
||||||
public function startDatabase(): void
|
public function startDatabase(): void
|
||||||
{
|
{
|
||||||
if ($this->hasDatabase) {
|
if ($this->hasDatabase) {
|
||||||
throw new \Exception('Database module has already been started.');
|
throw new \Exception('Database module has already been started.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$config = $this->config;
|
$this->addModule('database', new Database($this->config, self::DATABASE_CONNECTIONS[0]));
|
||||||
|
$this->loadDatabaseConnections();
|
||||||
$this->addModule('database', new Database(
|
|
||||||
$config,
|
|
||||||
$config->get('Database', 'default', 'string', 'default')
|
|
||||||
));
|
|
||||||
|
|
||||||
$this->loadConfigDatabaseConnections();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up the required database connections defined in the DATABASE_CONNECTIONS constant.
|
||||||
|
*/
|
||||||
|
private function loadDatabaseConnections(): void
|
||||||
|
{
|
||||||
|
$config = $this->config;
|
||||||
|
$database = $this->database;
|
||||||
|
|
||||||
|
foreach (self::DATABASE_CONNECTIONS as $name) {
|
||||||
|
$section = 'Database.' . $name;
|
||||||
|
|
||||||
|
if (!$config->contains($section)) {
|
||||||
|
throw new \Exception("Database {$name} is not configured.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$database->addConnectionFromConfig($section, $name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up the templating engine module.
|
||||||
|
*/
|
||||||
public function startTemplating(): void
|
public function startTemplating(): void
|
||||||
{
|
{
|
||||||
if ($this->hasTemplating) {
|
if ($this->hasTemplating) {
|
||||||
|
@ -65,6 +104,9 @@ class Application extends ApplicationBase
|
||||||
$twig->addPath('nova', __DIR__ . '/../views/nova');
|
$twig->addPath('nova', __DIR__ . '/../views/nova');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up the router module.
|
||||||
|
*/
|
||||||
public function startRouter(array $routes = null): void
|
public function startRouter(array $routes = null): void
|
||||||
{
|
{
|
||||||
if ($this->hasRouter) {
|
if ($this->hasRouter) {
|
||||||
|
@ -77,30 +119,4 @@ class Application extends ApplicationBase
|
||||||
$router->add($routes);
|
$router->add($routes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @todo Instead of reading a connections variable from the config,
|
|
||||||
* the expected connections should be defined somewhere in this class.
|
|
||||||
*/
|
|
||||||
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.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,44 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Misuzu;
|
namespace Misuzu;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains all non-specific methods, for possibly using Misuzu as a framework for other things.
|
||||||
|
*/
|
||||||
abstract class ApplicationBase
|
abstract class ApplicationBase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Things extending ApplicationBase are single instance, this property contains the active one.
|
||||||
|
* @var ApplicationBase
|
||||||
|
*/
|
||||||
private static $instance = null;
|
private static $instance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds all the loaded modules.
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
private $modules = [];
|
private $modules = [];
|
||||||
|
|
||||||
public static function getInstance(): Application
|
/**
|
||||||
|
* Gets the currently active instance of ApplicationBase
|
||||||
|
* @return ApplicationBase
|
||||||
|
*/
|
||||||
|
public static function getInstance(): ApplicationBase
|
||||||
{
|
{
|
||||||
if (is_null(self::$instance) || !(self::$instance instanceof Application)) {
|
if (is_null(self::$instance) || !(self::$instance instanceof ApplicationBase)) {
|
||||||
throw new \Exception('Invalid instance type.');
|
throw new \Exception('Invalid instance type.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::$instance;
|
return self::$instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function start(...$params): Application
|
/**
|
||||||
|
* Creates an instance of whichever class extends ApplicationBase.
|
||||||
|
* I have no idea how to make a param for the ... thingy so ech.
|
||||||
|
* @return ApplicationBase
|
||||||
|
*/
|
||||||
|
public static function start(...$params): ApplicationBase
|
||||||
{
|
{
|
||||||
if (!is_null(self::$instance) || self::$instance instanceof Application) {
|
if (!is_null(self::$instance) || self::$instance instanceof ApplicationBase) {
|
||||||
throw new \Exception('An Application has already been set up.');
|
throw new \Exception('An Application has already been set up.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,16 +46,30 @@ abstract class ApplicationBase
|
||||||
return self::getInstance();
|
return self::getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets info from the current git commit.
|
||||||
|
* @param string $format Follows the format of the pretty flag on the git log command
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public static function gitCommitInfo(string $format): string
|
public static function gitCommitInfo(string $format): string
|
||||||
{
|
{
|
||||||
return trim(shell_exec(sprintf('git log --pretty="%s" -n1 HEAD', $format)));
|
return trim(shell_exec(sprintf('git log --pretty="%s" -n1 HEAD', $format)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the hash of the current commit.
|
||||||
|
* @param bool $long Whether to fetch the long hash or the shorter one.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public static function gitCommitHash(bool $long = false): string
|
public static function gitCommitHash(bool $long = false): string
|
||||||
{
|
{
|
||||||
return self::gitCommitInfo($long ? '%H' : '%h');
|
return self::gitCommitInfo($long ? '%H' : '%h');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the name of the current branch.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public static function gitBranch(): string
|
public static function gitBranch(): string
|
||||||
{
|
{
|
||||||
return trim(shell_exec('git rev-parse --abbrev-ref HEAD'));
|
return trim(shell_exec('git rev-parse --abbrev-ref HEAD'));
|
||||||
|
@ -54,6 +89,11 @@ abstract class ApplicationBase
|
||||||
throw new \Exception('Invalid property.');
|
throw new \Exception('Invalid property.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a module to this application.
|
||||||
|
* @param string $name
|
||||||
|
* @param mixed $module
|
||||||
|
*/
|
||||||
public function addModule(string $name, $module): void
|
public function addModule(string $name, $module): void
|
||||||
{
|
{
|
||||||
if ($this->hasModule($name)) {
|
if ($this->hasModule($name)) {
|
||||||
|
@ -63,6 +103,11 @@ abstract class ApplicationBase
|
||||||
$this->modules[$name] = $module;
|
$this->modules[$name] = $module;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a module is registered.
|
||||||
|
* @param string $name
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
public function hasModule(string $name): bool
|
public function hasModule(string $name): bool
|
||||||
{
|
{
|
||||||
return array_key_exists($name, $this->modules) && !is_null($this->modules[$name]);
|
return array_key_exists($name, $this->modules) && !is_null($this->modules[$name]);
|
||||||
|
|
|
@ -3,7 +3,6 @@ namespace Misuzu\Controllers;
|
||||||
|
|
||||||
use Misuzu\Application;
|
use Misuzu\Application;
|
||||||
use Misuzu\Database;
|
use Misuzu\Database;
|
||||||
use Misuzu\AyaseUser;
|
|
||||||
|
|
||||||
class HomeController extends Controller
|
class HomeController extends Controller
|
||||||
{
|
{
|
||||||
|
|
|
@ -15,6 +15,12 @@ class Database extends LaravelDatabaseManager
|
||||||
'sqlsrv',
|
'sqlsrv',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
private const DEFAULT_PORT_MYSQL = 3306;
|
||||||
|
private const DEFAULT_PORT_PGSQL = 5432;
|
||||||
|
private const DEFAULT_PORT_MSSQL = 1433;
|
||||||
|
|
||||||
|
private const DEFAULT_HOST = '127.0.0.1';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
ConfigManager $config,
|
ConfigManager $config,
|
||||||
string $default = 'default',
|
string $default = 'default',
|
||||||
|
@ -50,7 +56,9 @@ class Database extends LaravelDatabaseManager
|
||||||
$args = [
|
$args = [
|
||||||
'driver' => $driver,
|
'driver' => $driver,
|
||||||
'database' => $this->configManager->get($section, 'database', 'string', 'misuzu'),
|
'database' => $this->configManager->get($section, 'database', 'string', 'misuzu'),
|
||||||
'prefix' => $this->configManager->get($section, 'prefix', 'string', ''),
|
'prefix' => $this->configManager->contains($section, 'prefix')
|
||||||
|
? $this->configManager->get($section, 'prefix', 'string')
|
||||||
|
: '',
|
||||||
];
|
];
|
||||||
|
|
||||||
switch ($driver) {
|
switch ($driver) {
|
||||||
|
@ -59,11 +67,11 @@ class Database extends LaravelDatabaseManager
|
||||||
|
|
||||||
$args['host'] = $is_unix_socket
|
$args['host'] = $is_unix_socket
|
||||||
? ''
|
? ''
|
||||||
: $this->configManager->get($section, 'host', 'string', '127.0.0.1');
|
: $this->configManager->get($section, 'host', 'string', self::DEFAULT_HOST);
|
||||||
|
|
||||||
$args['port'] = $is_unix_socket
|
$args['port'] = $is_unix_socket
|
||||||
? 3306
|
? self::DEFAULT_PORT_MYSQL
|
||||||
: $this->configManager->get($section, 'port', 'int', 3306);
|
: $this->configManager->get($section, 'port', 'int', self::DEFAULT_PORT_MYSQL);
|
||||||
|
|
||||||
$args['username'] = $this->configManager->get($section, 'username', 'string');
|
$args['username'] = $this->configManager->get($section, 'username', 'string');
|
||||||
$args['password'] = $this->configManager->get($section, 'password', 'string');
|
$args['password'] = $this->configManager->get($section, 'password', 'string');
|
||||||
|
@ -89,11 +97,11 @@ class Database extends LaravelDatabaseManager
|
||||||
|
|
||||||
$args['host'] = $is_unix_socket
|
$args['host'] = $is_unix_socket
|
||||||
? ''
|
? ''
|
||||||
: $this->configManager->get($section, 'host', 'string', '127.0.0.1');
|
: $this->configManager->get($section, 'host', 'string', self::DEFAULT_HOST);
|
||||||
|
|
||||||
$args['port'] = $is_unix_socket
|
$args['port'] = $is_unix_socket
|
||||||
? 5432
|
? self::DEFAULT_PORT_PGSQL
|
||||||
: $this->configManager->get($section, 'port', 'int', 5432);
|
: $this->configManager->get($section, 'port', 'int', self::DEFAULT_PORT_PGSQL);
|
||||||
|
|
||||||
$args['username'] = $this->configManager->get($section, 'username', 'string');
|
$args['username'] = $this->configManager->get($section, 'username', 'string');
|
||||||
$args['password'] = $this->configManager->get($section, 'password', 'string');
|
$args['password'] = $this->configManager->get($section, 'password', 'string');
|
||||||
|
@ -112,8 +120,8 @@ class Database extends LaravelDatabaseManager
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'sqlsrv':
|
case 'sqlsrv':
|
||||||
$args['host'] = $this->configManager->get($section, 'host', 'string', '127.0.0.1');
|
$args['host'] = $this->configManager->get($section, 'host', 'string', self::DEFAULT_HOST);
|
||||||
$args['port'] = $this->configManager->get($section, 'port', 'int', 1433);
|
$args['port'] = $this->configManager->get($section, 'port', 'int', self::DEFAULT_PORT_MSSQL);
|
||||||
$args['username'] = $this->configManager->get($section, 'username', 'string');
|
$args['username'] = $this->configManager->get($section, 'username', 'string');
|
||||||
$args['password'] = $this->configManager->get($section, 'password', 'string');
|
$args['password'] = $this->configManager->get($section, 'password', 'string');
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue