Browse Source

init config model

PHP-GTK3
yggverse 4 months ago
parent
commit
789db97319
  1. 3
      config.json
  2. 5
      src/Entity/App.php
  3. 4
      src/Entity/Tab/History.php
  4. 4
      src/Entity/Tab/Page.php
  5. 45
      src/Model/Config.php
  6. 24
      src/Model/File.php

3
config.json

@ -1,5 +1,3 @@
{
"app":
{ {
"name":"Yoda", "name":"Yoda",
"theme":"Default", "theme":"Default",
@ -227,4 +225,3 @@
} }
} }
} }
}

5
src/Entity/App.php

@ -6,6 +6,7 @@ namespace Yggverse\Yoda\Entity;
class App class App
{ {
public \Yggverse\Yoda\Model\Config $config;
public \Yggverse\Yoda\Model\Database $database; public \Yggverse\Yoda\Model\Database $database;
public \Yggverse\Yoda\Entity\Tab\History $history; public \Yggverse\Yoda\Entity\Tab\History $history;
@ -14,12 +15,10 @@ class App
public \GtkHeaderBar $header; public \GtkHeaderBar $header;
public \GtkNotebook $tabs; public \GtkNotebook $tabs;
public object $config;
public function __construct() public function __construct()
{ {
// Init config // Init config
$this->config = \Yggverse\Yoda\Model\File::getConfig()->app; // @TODO $this->config = new \Yggverse\Yoda\Model\Config;
// Init database // Init database
$this->database = new \Yggverse\Yoda\Model\Database( $this->database = new \Yggverse\Yoda\Model\Database(

4
src/Entity/Tab/History.php

@ -30,8 +30,8 @@ class History
// Init app // Init app
$this->app = $app; $this->app = $app;
// Init config // Init config namespace
$this->config = \Yggverse\Yoda\Model\File::getConfig()->app->tab->history; $this->config = $app->config->tab->history;
// Cleanup expired history // Cleanup expired history
if ($this->config->clean->timeout) if ($this->config->clean->timeout)

4
src/Entity/Tab/Page.php

@ -43,8 +43,8 @@ class Page
// Init app // Init app
$this->app = $app; $this->app = $app;
// Init config // Init config namespace
$this->config = \Yggverse\Yoda\Model\File::getConfig()->app->tab->page; $this->config = $app->config->tab->page;
// Init DNS memory // Init DNS memory
$this->dns = new \Yggverse\Yoda\Model\Memory; $this->dns = new \Yggverse\Yoda\Model\Memory;

45
src/Model/Config.php

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Yggverse\Yoda\Model;
class Config
{
public function __construct(
?string $filename = null
) {
if (empty($filename))
{
$filename = __DIR__ .
DIRECTORY_SEPARATOR . '..' .
DIRECTORY_SEPARATOR . '..' .
DIRECTORY_SEPARATOR . 'config.json';
}
if (!file_exists($filename))
{
throw new \Exception; // @TODO
}
if (!is_readable($filename))
{
throw new \Exception; // @TODO
}
if (!$data = file_get_contents($filename))
{
throw new \Exception; // @TODO
}
if (!$config = @json_decode($data))
{
throw new \Exception; // @TODO
}
foreach ($config as $key => $value)
{
$this->{$key} = $value; // @TODO
}
}
}

24
src/Model/File.php

@ -6,30 +6,6 @@ namespace Yggverse\Yoda\Model;
class File class File
{ {
public static function getConfig(): object
{
$filename = __DIR__ .
DIRECTORY_SEPARATOR . '..' .
DIRECTORY_SEPARATOR . '..' .
DIRECTORY_SEPARATOR . 'config.json';
if (file_exists($filename) && is_readable($filename))
{
$result = json_decode(
file_get_contents(
$filename
)
);
}
if (empty($result))
{
throw new \Exception(); // @TODO
}
return $result;
}
public static function getTheme(string $name): string public static function getTheme(string $name): string
{ {
$filename = __DIR__ . $filename = __DIR__ .

Loading…
Cancel
Save