2018-01-02 21:26:33 +01:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2018-01-02 21:57:06 +01:00
|
|
|
use Aitemu\RouteCollection;
|
2018-01-02 21:26:33 +01:00
|
|
|
use Misuzu\Config\ConfigManager;
|
|
|
|
|
2018-01-04 21:52:26 +01:00
|
|
|
/**
|
|
|
|
* Handles the set up procedures.
|
|
|
|
*/
|
2018-01-04 21:01:55 +01:00
|
|
|
class Application extends ApplicationBase
|
2018-01-02 21:26:33 +01:00
|
|
|
{
|
2018-01-04 21:52:26 +01:00
|
|
|
/**
|
|
|
|
* Whether the application is in debug mode, this should only be set in the constructor and never altered.
|
|
|
|
* @var bool
|
|
|
|
*/
|
2018-01-04 21:01:55 +01:00
|
|
|
private $debugMode = false;
|
2018-01-02 21:26:33 +01:00
|
|
|
|
2018-01-04 21:52:26 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
2018-01-02 21:57:06 +01:00
|
|
|
{
|
2018-01-04 21:01:55 +01:00
|
|
|
$this->debugMode = $debug;
|
2018-01-03 02:12:28 +01:00
|
|
|
ExceptionHandler::register();
|
2018-01-04 21:01:55 +01:00
|
|
|
ExceptionHandler::debug($this->debugMode);
|
2018-01-03 22:39:01 +01:00
|
|
|
$this->addModule('config', new ConfigManager($configFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if ($this->hasConfig) {
|
|
|
|
$this->config->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
ExceptionHandler::unregister();
|
|
|
|
}
|
|
|
|
|
2018-01-04 21:52:26 +01:00
|
|
|
/**
|
|
|
|
* Sets up the database module.
|
|
|
|
*/
|
2018-01-03 22:39:01 +01:00
|
|
|
public function startDatabase(): void
|
|
|
|
{
|
|
|
|
if ($this->hasDatabase) {
|
|
|
|
throw new \Exception('Database module has already been started.');
|
|
|
|
}
|
|
|
|
|
2018-01-04 21:52:26 +01:00
|
|
|
$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
|
|
|
|
{
|
2018-01-03 22:39:01 +01:00
|
|
|
$config = $this->config;
|
2018-01-04 21:52:26 +01:00
|
|
|
$database = $this->database;
|
|
|
|
|
|
|
|
foreach (self::DATABASE_CONNECTIONS as $name) {
|
|
|
|
$section = 'Database.' . $name;
|
2018-01-02 21:57:06 +01:00
|
|
|
|
2018-01-04 21:52:26 +01:00
|
|
|
if (!$config->contains($section)) {
|
|
|
|
throw new \Exception("Database {$name} is not configured.");
|
|
|
|
}
|
2018-01-03 20:50:29 +01:00
|
|
|
|
2018-01-04 21:52:26 +01:00
|
|
|
$database->addConnectionFromConfig($section, $name);
|
|
|
|
}
|
2018-01-03 22:39:01 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 21:52:26 +01:00
|
|
|
/**
|
|
|
|
* Sets up the templating engine module.
|
|
|
|
*/
|
2018-01-03 22:39:01 +01:00
|
|
|
public function startTemplating(): void
|
|
|
|
{
|
|
|
|
if ($this->hasTemplating) {
|
|
|
|
throw new \Exception('Templating module has already been started.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addModule('templating', $twig = new TemplateEngine);
|
2018-01-04 21:01:55 +01:00
|
|
|
$twig->debug($this->debugMode);
|
2018-01-03 17:49:20 +01:00
|
|
|
|
|
|
|
$twig->addFilter('json_decode');
|
|
|
|
$twig->addFilter('byte_symbol');
|
2018-01-04 21:01:55 +01:00
|
|
|
|
2018-01-03 17:49:20 +01:00
|
|
|
$twig->addFunction('byte_symbol');
|
|
|
|
$twig->addFunction('session_id');
|
2018-01-03 22:39:01 +01:00
|
|
|
$twig->addFunction('config', [$this->config, 'get']);
|
|
|
|
$twig->addFunction('route', [$this->router, 'url']);
|
2018-01-03 17:49:20 +01:00
|
|
|
$twig->addFunction('git_hash', [Application::class, 'gitCommitHash']);
|
|
|
|
$twig->addFunction('git_branch', [Application::class, 'gitBranch']);
|
2018-01-04 21:01:55 +01:00
|
|
|
|
|
|
|
$twig->vars(['app' => $this]);
|
|
|
|
|
2018-01-03 17:49:20 +01:00
|
|
|
$twig->addPath('nova', __DIR__ . '/../views/nova');
|
2018-01-02 21:57:06 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 21:52:26 +01:00
|
|
|
/**
|
|
|
|
* Sets up the router module.
|
|
|
|
*/
|
2018-01-03 22:39:01 +01:00
|
|
|
public function startRouter(array $routes = null): void
|
2018-01-02 21:26:33 +01:00
|
|
|
{
|
2018-01-03 22:39:01 +01:00
|
|
|
if ($this->hasRouter) {
|
|
|
|
throw new \Exception('Router module has already been started.');
|
2018-01-03 20:50:29 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 22:39:01 +01:00
|
|
|
$this->addModule('router', $router = new RouteCollection);
|
|
|
|
|
|
|
|
if ($routes !== null) {
|
|
|
|
$router->add($routes);
|
|
|
|
}
|
2018-01-02 21:26:33 +01:00
|
|
|
}
|
|
|
|
}
|