Add Model base class and load modules when required.
This commit is contained in:
parent
595a4f3744
commit
feb1bc810e
6 changed files with 86 additions and 33 deletions
|
@ -16,7 +16,7 @@ charset = utf8mb4
|
|||
collation = utf8mb4_bin
|
||||
|
||||
[Database.sqlite_example]
|
||||
driver = sqlite_example
|
||||
driver = sqlite
|
||||
database = store/database.db3
|
||||
prefix =
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -1,4 +1,17 @@
|
|||
<?php
|
||||
namespace Misuzu;
|
||||
|
||||
use Aitemu\RouterRequest;
|
||||
|
||||
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)
|
||||
);
|
||||
|
|
|
@ -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;
|
||||
|
|
8
src/Model.php
Normal file
8
src/Model.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
namespace Misuzu;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model as BaseModel;
|
||||
|
||||
abstract class Model extends BaseModel
|
||||
{
|
||||
}
|
|
@ -39,14 +39,18 @@ class TemplateEngine
|
|||
/**
|
||||
* 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->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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue