From feb1bc810e557cc869715e9ee41b7ee19edead64 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 3 Jan 2018 22:39:01 +0100 Subject: [PATCH] Add Model base class and load modules when required. --- config/config.example.ini | 2 +- misuzu.php | 2 +- public/index.php | 13 ++++++++ src/Application.php | 70 +++++++++++++++++++++++++++------------ src/Model.php | 8 +++++ src/TemplateEngine.php | 24 ++++++++------ 6 files changed, 86 insertions(+), 33 deletions(-) create mode 100644 src/Model.php diff --git a/config/config.example.ini b/config/config.example.ini index 3c45f302..8fd8fb79 100644 --- a/config/config.example.ini +++ b/config/config.example.ini @@ -16,7 +16,7 @@ charset = utf8mb4 collation = utf8mb4_bin [Database.sqlite_example] -driver = sqlite_example +driver = sqlite database = store/database.db3 prefix = diff --git a/misuzu.php b/misuzu.php index adb1b838..fcb8054a 100644 --- a/misuzu.php +++ b/misuzu.php @@ -5,4 +5,4 @@ require_once 'vendor/autoload.php'; $app = Application::start(__DIR__ . '/config/config.ini'); $app->debug(IO\Directory::exists(__DIR__ . '/vendor/phpunit/phpunit')); -$app->router->add(include_once __DIR__ . '/routes.php'); +$app->startDatabase(); diff --git a/public/index.php b/public/index.php index f1ea1ea1..3376ef1c 100644 --- a/public/index.php +++ b/public/index.php @@ -1,4 +1,17 @@ startRouter(include_once __DIR__ . '/../routes.php'); +$app->startTemplating(); + +echo $app->router->resolve( + RouterRequest::fromServer($_SERVER, $_GET, $_POST, $_COOKIE) +); diff --git a/src/Application.php b/src/Application.php index c57433c1..b3b9504c 100644 --- a/src/Application.php +++ b/src/Application.php @@ -61,27 +61,7 @@ class Application protected function __construct($configFile = null) { ExceptionHandler::register(); - $this->debug(true); - - $this->addModule('config', $config = new ConfigManager($configFile)); - $this->addModule('database', new Database( - $config, - $config->get('Database', 'default', 'string', 'default') - )); - $this->addModule('router', $router = new RouteCollection); - $this->addModule('templating', $twig = new TemplateEngine); - - $this->loadConfigDatabaseConnections(); - - $twig->addFilter('json_decode'); - $twig->addFilter('byte_symbol'); - $twig->addFunction('byte_symbol'); - $twig->addFunction('session_id'); - $twig->addFunction('config', [$config, 'get']); - $twig->addFunction('route', [$router, 'url']); - $twig->addFunction('git_hash', [Application::class, 'gitCommitHash']); - $twig->addFunction('git_branch', [Application::class, 'gitBranch']); - $twig->addPath('nova', __DIR__ . '/../views/nova'); + $this->addModule('config', new ConfigManager($configFile)); } public function __destruct() @@ -93,6 +73,54 @@ class Application ExceptionHandler::unregister(); } + 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(); + } + + public function startTemplating(): void + { + if ($this->hasTemplating) { + throw new \Exception('Templating module has already been started.'); + } + + $this->addModule('templating', $twig = new TemplateEngine); + + $twig->addFilter('json_decode'); + $twig->addFilter('byte_symbol'); + $twig->addFunction('byte_symbol'); + $twig->addFunction('session_id'); + $twig->addFunction('config', [$this->config, 'get']); + $twig->addFunction('route', [$this->router, 'url']); + $twig->addFunction('git_hash', [Application::class, 'gitCommitHash']); + $twig->addFunction('git_branch', [Application::class, 'gitBranch']); + $twig->addPath('nova', __DIR__ . '/../views/nova'); + } + + public function startRouter(array $routes = null): void + { + if ($this->hasRouter) { + throw new \Exception('Router module has already been started.'); + } + + $this->addModule('router', $router = new RouteCollection); + + if ($routes !== null) { + $router->add($routes); + } + } + private function loadConfigDatabaseConnections(): void { $config = $this->config; diff --git a/src/Model.php b/src/Model.php new file mode 100644 index 00000000..0ab74fbc --- /dev/null +++ b/src/Model.php @@ -0,0 +1,8 @@ +loader = new Twig_Loader_Filesystem; $this->twig = new Twig_Environment($this->loader, [ - 'cache' => false, - 'strict_variables' => true, - 'auto_reload' => false, - 'debug' => false, + 'cache' => $cache ?? false, + 'strict_variables' => $strict, + 'auto_reload' => $autoReload, + 'debug' => $debug, ]); } @@ -90,9 +94,9 @@ class TemplateEngine * Sets the cache path and alternatively turns it off. * @param string $path */ - public function cache(bool $path): void + public function cache(?string $path): void { - $this->twig->setCache($path); + $this->twig->setCache($path ?? false); } /** @@ -151,7 +155,7 @@ class TemplateEngine $namespace = $this->findNamespace($path); if ($namespace !== null) { - $path = '@' . $this->findNamespace($path) . '/' . $path; + $path = "@{$this->findNamespace($path)}/{$path}"; } } @@ -202,6 +206,6 @@ class TemplateEngine */ public function exists(string $path, string $namespace): bool { - return $this->loader->exists('@' . $namespace . '/' . self::fixPath($path)); + return $this->loader->exists("@{$namespace}/" . self::fixPath($path)); } }