init config as the class object, delegate db location logic to the database class, use realpath conversion for filename entities

This commit is contained in:
yggverse 2024-05-04 18:57:26 +03:00
parent a4a34c8ad1
commit 46bbf48f5f
3 changed files with 57 additions and 21 deletions

40
src/Model/Config.php Normal file
View file

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Yggverse\Pulsar\Model;
class Config
{
private object $_config;
public function __construct(
string $filename
) {
$this->_config = json_decode(
file_get_contents(
realpath(
str_starts_with(
$filename,
DIRECTORY_SEPARATOR
) ? $filename // absolute
: __DIR__ . // relative
DIRECTORY_SEPARATOR . '..'.
DIRECTORY_SEPARATOR . '..'.
DIRECTORY_SEPARATOR . 'config'.
DIRECTORY_SEPARATOR . $filename
)
)
);
if (!$this->_config)
{
throw new \Exception;
}
}
public function get(): object
{
return $this->_config;
}
}

View file

@ -16,7 +16,17 @@ class Database
$this->_database = new \PDO(
sprintf(
'sqlite:%s',
$database
realpath(
str_starts_with(
$database,
DIRECTORY_SEPARATOR
) ? $database
: __DIR__ .
DIRECTORY_SEPARATOR . '..'.
DIRECTORY_SEPARATOR . '..'.
DIRECTORY_SEPARATOR . 'config'.
DIRECTORY_SEPARATOR . $database
)
),
$username,
$password

View file

@ -19,29 +19,15 @@ require_once __DIR__ .
if (empty($argv[1])) throw new \Exception;
// Init config
$config = json_decode(
file_get_contents(
str_starts_with(
$argv[1],
DIRECTORY_SEPARATOR
) ? $argv[1] // absolute
: __DIR__ . // relative
DIRECTORY_SEPARATOR . '..'.
DIRECTORY_SEPARATOR . 'config'.
DIRECTORY_SEPARATOR . $argv[1]
)
); if (!$config) throw new \Exception;
$config = new \Yggverse\Pulsar\Model\Config(
$argv[1]
);
$config = $config->get(); // registry only
// Init database
$database = new \Yggverse\Pulsar\Model\Database(
str_starts_with(
$config->database->location,
DIRECTORY_SEPARATOR
) ? $config->database->location
: __DIR__ .
DIRECTORY_SEPARATOR . '..'.
DIRECTORY_SEPARATOR . 'config'.
DIRECTORY_SEPARATOR . $config->database->location,
$config->database->location,
$config->database->username,
$config->database->password
);