diff --git a/config/config.example.ini b/config/config.example.ini index 8fd8fb79..98deffca 100644 --- a/config/config.example.ini +++ b/config/config.example.ini @@ -1,9 +1,5 @@ ; Example configuration for Misuzu -[Database] -default = mysql_example -connections = mysql_example sqlite_example postgres_example sqlsrv_example - [Database.mysql_example] driver = mysql host = localhost diff --git a/src/Application.php b/src/Application.php index 0e9da47f..f57b61d2 100644 --- a/src/Application.php +++ b/src/Application.php @@ -4,11 +4,31 @@ namespace Misuzu; use Aitemu\RouteCollection; use Misuzu\Config\ConfigManager; +/** + * Handles the set up procedures. + */ 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; - 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; ExceptionHandler::register(); @@ -25,22 +45,41 @@ class Application extends ApplicationBase ExceptionHandler::unregister(); } + /** + * Sets up the database module. + */ public function startDatabase(): void { if ($this->hasDatabase) { throw new \Exception('Database module has already been started.'); } - $config = $this->config; - - $this->addModule('database', new Database( - $config, - $config->get('Database', 'default', 'string', 'default') - )); - - $this->loadConfigDatabaseConnections(); + $this->addModule('database', new Database($this->config, self::DATABASE_CONNECTIONS[0])); + $this->loadDatabaseConnections(); } + /** + * 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 { if ($this->hasTemplating) { @@ -65,6 +104,9 @@ class Application extends ApplicationBase $twig->addPath('nova', __DIR__ . '/../views/nova'); } + /** + * Sets up the router module. + */ public function startRouter(array $routes = null): void { if ($this->hasRouter) { @@ -77,30 +119,4 @@ class Application extends ApplicationBase $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.'); - } - } } diff --git a/src/ApplicationBase.php b/src/ApplicationBase.php index 1c58a62c..33f7c76f 100644 --- a/src/ApplicationBase.php +++ b/src/ApplicationBase.php @@ -1,23 +1,44 @@ hasModule($name)) { @@ -63,6 +103,11 @@ abstract class ApplicationBase $this->modules[$name] = $module; } + /** + * Checks if a module is registered. + * @param string $name + * @return bool + */ public function hasModule(string $name): bool { return array_key_exists($name, $this->modules) && !is_null($this->modules[$name]); diff --git a/src/Controllers/HomeController.php b/src/Controllers/HomeController.php index a41c0dd9..c19d8843 100644 --- a/src/Controllers/HomeController.php +++ b/src/Controllers/HomeController.php @@ -3,7 +3,6 @@ namespace Misuzu\Controllers; use Misuzu\Application; use Misuzu\Database; -use Misuzu\AyaseUser; class HomeController extends Controller { diff --git a/src/Database.php b/src/Database.php index e658e61b..71e2e878 100644 --- a/src/Database.php +++ b/src/Database.php @@ -15,6 +15,12 @@ class Database extends LaravelDatabaseManager '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( ConfigManager $config, string $default = 'default', @@ -50,7 +56,9 @@ class Database extends LaravelDatabaseManager $args = [ 'driver' => $driver, '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) { @@ -59,11 +67,11 @@ class Database extends LaravelDatabaseManager $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 - ? 3306 - : $this->configManager->get($section, 'port', 'int', 3306); + ? self::DEFAULT_PORT_MYSQL + : $this->configManager->get($section, 'port', 'int', self::DEFAULT_PORT_MYSQL); $args['username'] = $this->configManager->get($section, 'username', 'string'); $args['password'] = $this->configManager->get($section, 'password', 'string'); @@ -89,11 +97,11 @@ class Database extends LaravelDatabaseManager $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 - ? 5432 - : $this->configManager->get($section, 'port', 'int', 5432); + ? self::DEFAULT_PORT_PGSQL + : $this->configManager->get($section, 'port', 'int', self::DEFAULT_PORT_PGSQL); $args['username'] = $this->configManager->get($section, 'username', 'string'); $args['password'] = $this->configManager->get($section, 'password', 'string'); @@ -112,8 +120,8 @@ class Database extends LaravelDatabaseManager break; case 'sqlsrv': - $args['host'] = $this->configManager->get($section, 'host', 'string', '127.0.0.1'); - $args['port'] = $this->configManager->get($section, 'port', 'int', 1433); + $args['host'] = $this->configManager->get($section, 'host', 'string', self::DEFAULT_HOST); + $args['port'] = $this->configManager->get($section, 'port', 'int', self::DEFAULT_PORT_MSSQL); $args['username'] = $this->configManager->get($section, 'username', 'string'); $args['password'] = $this->configManager->get($section, 'password', 'string');