mirror of
https://github.com/YGGverse/Pulsar.git
synced 2026-04-01 10:15:32 +00:00
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:
parent
a4a34c8ad1
commit
46bbf48f5f
3 changed files with 57 additions and 21 deletions
40
src/Model/Config.php
Normal file
40
src/Model/Config.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,7 +16,17 @@ class Database
|
||||||
$this->_database = new \PDO(
|
$this->_database = new \PDO(
|
||||||
sprintf(
|
sprintf(
|
||||||
'sqlite:%s',
|
'sqlite:%s',
|
||||||
$database
|
realpath(
|
||||||
|
str_starts_with(
|
||||||
|
$database,
|
||||||
|
DIRECTORY_SEPARATOR
|
||||||
|
) ? $database
|
||||||
|
: __DIR__ .
|
||||||
|
DIRECTORY_SEPARATOR . '..'.
|
||||||
|
DIRECTORY_SEPARATOR . '..'.
|
||||||
|
DIRECTORY_SEPARATOR . 'config'.
|
||||||
|
DIRECTORY_SEPARATOR . $database
|
||||||
|
)
|
||||||
),
|
),
|
||||||
$username,
|
$username,
|
||||||
$password
|
$password
|
||||||
|
|
|
||||||
|
|
@ -19,29 +19,15 @@ require_once __DIR__ .
|
||||||
if (empty($argv[1])) throw new \Exception;
|
if (empty($argv[1])) throw new \Exception;
|
||||||
|
|
||||||
// Init config
|
// Init config
|
||||||
$config = json_decode(
|
$config = new \Yggverse\Pulsar\Model\Config(
|
||||||
file_get_contents(
|
$argv[1]
|
||||||
str_starts_with(
|
);
|
||||||
$argv[1],
|
|
||||||
DIRECTORY_SEPARATOR
|
$config = $config->get(); // registry only
|
||||||
) ? $argv[1] // absolute
|
|
||||||
: __DIR__ . // relative
|
|
||||||
DIRECTORY_SEPARATOR . '..'.
|
|
||||||
DIRECTORY_SEPARATOR . 'config'.
|
|
||||||
DIRECTORY_SEPARATOR . $argv[1]
|
|
||||||
)
|
|
||||||
); if (!$config) throw new \Exception;
|
|
||||||
|
|
||||||
// Init database
|
// Init database
|
||||||
$database = new \Yggverse\Pulsar\Model\Database(
|
$database = new \Yggverse\Pulsar\Model\Database(
|
||||||
str_starts_with(
|
$config->database->location,
|
||||||
$config->database->location,
|
|
||||||
DIRECTORY_SEPARATOR
|
|
||||||
) ? $config->database->location
|
|
||||||
: __DIR__ .
|
|
||||||
DIRECTORY_SEPARATOR . '..'.
|
|
||||||
DIRECTORY_SEPARATOR . 'config'.
|
|
||||||
DIRECTORY_SEPARATOR . $config->database->location,
|
|
||||||
$config->database->username,
|
$config->database->username,
|
||||||
$config->database->password
|
$config->database->password
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue