Add Model base class and load modules when required.

This commit is contained in:
flash 2018-01-03 22:39:01 +01:00
parent 595a4f3744
commit feb1bc810e
6 changed files with 86 additions and 33 deletions

View file

@ -16,7 +16,7 @@ charset = utf8mb4
collation = utf8mb4_bin collation = utf8mb4_bin
[Database.sqlite_example] [Database.sqlite_example]
driver = sqlite_example driver = sqlite
database = store/database.db3 database = store/database.db3
prefix = prefix =

View file

@ -5,4 +5,4 @@ require_once 'vendor/autoload.php';
$app = Application::start(__DIR__ . '/config/config.ini'); $app = Application::start(__DIR__ . '/config/config.ini');
$app->debug(IO\Directory::exists(__DIR__ . '/vendor/phpunit/phpunit')); $app->debug(IO\Directory::exists(__DIR__ . '/vendor/phpunit/phpunit'));
$app->router->add(include_once __DIR__ . '/routes.php'); $app->startDatabase();

View file

@ -1,4 +1,17 @@
<?php <?php
namespace Misuzu; namespace Misuzu;
use Aitemu\RouterRequest;
require_once __DIR__ . '/../misuzu.php'; require_once __DIR__ . '/../misuzu.php';
ob_start('ob_gzhandler');
$app = Application::getInstance();
$app->startRouter(include_once __DIR__ . '/../routes.php');
$app->startTemplating();
echo $app->router->resolve(
RouterRequest::fromServer($_SERVER, $_GET, $_POST, $_COOKIE)
);

View file

@ -61,27 +61,7 @@ class Application
protected function __construct($configFile = null) protected function __construct($configFile = null)
{ {
ExceptionHandler::register(); ExceptionHandler::register();
$this->debug(true); $this->addModule('config', new ConfigManager($configFile));
$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');
} }
public function __destruct() public function __destruct()
@ -93,6 +73,54 @@ class Application
ExceptionHandler::unregister(); 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 private function loadConfigDatabaseConnections(): void
{ {
$config = $this->config; $config = $this->config;

8
src/Model.php Normal file
View file

@ -0,0 +1,8 @@
<?php
namespace Misuzu;
use Illuminate\Database\Eloquent\Model as BaseModel;
abstract class Model extends BaseModel
{
}

View file

@ -39,14 +39,18 @@ class TemplateEngine
/** /**
* Creates the twig environment and registers the utility filters and functions. * Creates the twig environment and registers the utility filters and functions.
*/ */
public function __construct() public function __construct(
{ ?string $cache = null,
bool $strict = true,
bool $autoReload = false,
bool $debug = false
) {
$this->loader = new Twig_Loader_Filesystem; $this->loader = new Twig_Loader_Filesystem;
$this->twig = new Twig_Environment($this->loader, [ $this->twig = new Twig_Environment($this->loader, [
'cache' => false, 'cache' => $cache ?? false,
'strict_variables' => true, 'strict_variables' => $strict,
'auto_reload' => false, 'auto_reload' => $autoReload,
'debug' => false, 'debug' => $debug,
]); ]);
} }
@ -90,9 +94,9 @@ class TemplateEngine
* Sets the cache path and alternatively turns it off. * Sets the cache path and alternatively turns it off.
* @param string $path * @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); $namespace = $this->findNamespace($path);
if ($namespace !== null) { 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 public function exists(string $path, string $namespace): bool
{ {
return $this->loader->exists('@' . $namespace . '/' . self::fixPath($path)); return $this->loader->exists("@{$namespace}/" . self::fixPath($path));
} }
} }