From d74457a1405ea000838738a8a1896111d1606eb5 Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 15 Dec 2023 01:39:11 +0000 Subject: [PATCH] Switched Uiharu config to Syokuhou format. --- .gitignore | 1 + public/index.php | 2 +- src/Config.php | 25 ------------------------- src/Lookup/YouTubeLookup.php | 5 ++++- src/UihContext.php | 7 +++++-- uiharu.php | 22 ++++++---------------- 6 files changed, 17 insertions(+), 45 deletions(-) delete mode 100644 src/Config.php diff --git a/.gitignore b/.gitignore index 285e197..36f6dc9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .DS_Store /.debug /config.ini +/uiharu.cfg /public/robots.txt /lib/index-dev /vendor diff --git a/public/index.php b/public/index.php index 92353dd..cdd2d68 100644 --- a/public/index.php +++ b/public/index.php @@ -10,7 +10,7 @@ $ctx->registerLookup(new \Uiharu\Lookup\EEPROMLookup('eeprom', 'eeprom.flashii.n if(UIH_DEBUG) $ctx->registerLookup(new \Uiharu\Lookup\EEPROMLookup('devrom', 'eeprom.edgii.net', ['i.edgii.net'])); -$ctx->registerLookup(new \Uiharu\Lookup\YouTubeLookup); +$ctx->registerLookup(new \Uiharu\Lookup\YouTubeLookup($cfg->scopeTo('google'))); $ctx->registerLookup(new \Uiharu\Lookup\NicoNicoLookup); // this should always come AFTER other lookups involved http(s) diff --git a/src/Config.php b/src/Config.php deleted file mode 100644 index 6b274ad..0000000 --- a/src/Config.php +++ /dev/null @@ -1,25 +0,0 @@ -isWeb()) return false; @@ -69,7 +72,7 @@ final class YouTubeLookup implements \Uiharu\ILookup { } private function lookupVideo(string $videoId): ?object { - $curl = curl_init("https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id={$videoId}&key=" . Config::get('Google', 'apiKey')); + $curl = curl_init("https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id={$videoId}&key=" . $this->config->getString('api_key')); curl_setopt_array($curl, [ CURLOPT_AUTOREFERER => false, CURLOPT_CERTINFO => false, diff --git a/src/UihContext.php b/src/UihContext.php index 9eb69a8..fb26c0d 100644 --- a/src/UihContext.php +++ b/src/UihContext.php @@ -3,15 +3,18 @@ namespace Uiharu; use Index\Data\IDbConnection; use Index\Http\HttpFx; +use Syokuhou\IConfig; final class UihContext { private IDbConnection $database; + private IConfig $config; private HttpFx $router; private array $apis = []; private array $lookups = []; - public function __construct(IDbConnection $database) { + public function __construct(IDbConnection $database, IConfig $config) { $this->database = $database; + $this->config = $config; } public function getDatabase(): IDbConnection { @@ -28,7 +31,7 @@ final class UihContext { if($origin === $_SERVER['HTTP_HOST']) return true; - $allowed = Config::get('CORS', 'origins', []); + $allowed = $this->config->getArray('cors:origins'); if(empty($allowed)) return true; diff --git a/uiharu.php b/uiharu.php index 53c2acc..0167c06 100644 --- a/uiharu.php +++ b/uiharu.php @@ -2,8 +2,8 @@ namespace Uiharu; use Index\Environment; -use Index\Data\ConnectionFailedException; use Index\Data\DbTools; +use Syokuhou\SharpConfig; define('UIH_STARTUP', microtime(true)); define('UIH_ROOT', __DIR__); @@ -11,7 +11,7 @@ define('UIH_DEBUG', is_file(UIH_ROOT . '/.debug')); define('UIH_PUBLIC', UIH_ROOT . '/public'); define('UIH_SOURCE', UIH_ROOT . '/src'); define('UIH_LIBRARY', UIH_ROOT . '/lib'); -define('UIH_VERSION', '20231021'); +define('UIH_VERSION', '20231215'); require_once UIH_ROOT . '/vendor/autoload.php'; @@ -20,19 +20,9 @@ Environment::setDebug(UIH_DEBUG); mb_internal_encoding('utf-8'); date_default_timezone_set('utc'); -$configPath = UIH_ROOT . '/config.ini'; -if(!is_file($configPath)) - die('Uiharu configuration is missing.'); +$cfg = SharpConfig::fromFile(UIH_ROOT . '/uiharu.cfg'); -Config::load($configPath); -if(!Config::has('Database', 'dsn')) - die('Uiharu database is not configured.'); +$db = DbTools::create($cfg->getString('database:dsn', 'null:')); +$db->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';'); -try { - $db = DbTools::create(Config::get('Database', 'dsn')); -} catch(ConnectionFailedException $ex) { - echo '

Unable to connect to database

'; - die($ex->getMessage()); -} - -$ctx = new UihContext($db); +$ctx = new UihContext($db, $cfg);