diff --git a/src/Model/Config.php b/src/Model/Config.php new file mode 100644 index 0000000..47e5224 --- /dev/null +++ b/src/Model/Config.php @@ -0,0 +1,40 @@ +_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; + } +} \ No newline at end of file diff --git a/src/Model/Database.php b/src/Model/Database.php index e94916d..208fb79 100644 --- a/src/Model/Database.php +++ b/src/Model/Database.php @@ -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 diff --git a/src/crawler.php b/src/crawler.php index bdddf44..076fd14 100644 --- a/src/crawler.php +++ b/src/crawler.php @@ -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 );