Browse Source

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

main
yggverse 7 months ago
parent
commit
46bbf48f5f
  1. 40
      src/Model/Config.php
  2. 12
      src/Model/Database.php
  3. 26
      src/crawler.php

40
src/Model/Config.php

@ -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;
}
}

12
src/Model/Database.php

@ -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

26
src/crawler.php

@ -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…
Cancel
Save