replace some dirname horrors with a separate unit-tested method

This commit is contained in:
Andrew Dolgov 2023-10-27 22:07:28 +03:00
parent 8c9c69921f
commit 5920ac814c
No known key found for this signature in database
GPG Key ID: 1A56B4FA25D4AF2A
4 changed files with 26 additions and 10 deletions

View File

@ -319,7 +319,7 @@ class Config {
* @return array<string, mixed>|string * @return array<string, mixed>|string
*/ */
private function _get_version(bool $as_string = true) { private function _get_version(bool $as_string = true) {
$root_dir = dirname(__DIR__); $root_dir = self::get_self_dir();
if (empty($this->version)) { if (empty($this->version)) {
$this->version["status"] = -1; $this->version["status"] = -1;
@ -413,7 +413,7 @@ class Config {
private function _get_migrations() : Db_Migrations { private function _get_migrations() : Db_Migrations {
if (empty($this->migrations)) { if (empty($this->migrations)) {
$this->migrations = new Db_Migrations(); $this->migrations = new Db_Migrations();
$this->migrations->initialize(dirname(__DIR__) . "/sql", "ttrss_version", true, self::SCHEMA_VERSION); $this->migrations->initialize(self::get_self_dir() . "/sql", "ttrss_version", true, self::SCHEMA_VERSION);
} }
return $this->migrations; return $this->migrations;
@ -703,4 +703,9 @@ class Config {
static function get_user_agent(): string { static function get_user_agent(): string {
return sprintf(self::get(self::HTTP_USER_AGENT), self::get_version()); return sprintf(self::get(self::HTTP_USER_AGENT), self::get_version());
} }
static function get_self_dir() : string {
return dirname(__DIR__); # we're in classes/Config.php
}
} }

View File

@ -459,10 +459,10 @@ class PluginHost {
$span->addEvent("$class_file: load"); $span->addEvent("$class_file: load");
// try system plugin directory first // try system plugin directory first
$file = dirname(__DIR__) . "/plugins/$class_file/init.php"; $file = Config::get_self_dir() . "/plugins/$class_file/init.php";
if (!file_exists($file)) { if (!file_exists($file)) {
$file = dirname(__DIR__) . "/plugins.local/$class_file/init.php"; $file = Config::get_self_dir() . "/plugins.local/$class_file/init.php";
if (!file_exists($file)) { if (!file_exists($file)) {
continue; continue;

View File

@ -1061,7 +1061,7 @@ class Pref_Prefs extends Handler_Protected {
* @return array<int, array{'plugin': string, 'rv': array{'stdout': false|string, 'stderr': false|string, 'git_status': int, 'need_update': bool}|null}> * @return array<int, array{'plugin': string, 'rv': array{'stdout': false|string, 'stderr': false|string, 'git_status': int, 'need_update': bool}|null}>
*/ */
static function _get_updated_plugins(): array { static function _get_updated_plugins(): array {
$root_dir = dirname(dirname(__DIR__)); # we're in classes/pref/ $root_dir = Config::get_self_dir();
$plugin_dirs = array_filter(glob("$root_dir/plugins.local/*"), "is_dir"); $plugin_dirs = array_filter(glob("$root_dir/plugins.local/*"), "is_dir");
$rv = []; $rv = [];
@ -1185,7 +1185,7 @@ class Pref_Prefs extends Handler_Protected {
$plugin_name = basename(clean($_REQUEST['plugin'])); $plugin_name = basename(clean($_REQUEST['plugin']));
$status = 0; $status = 0;
$plugin_dir = dirname(dirname(__DIR__)) . "/plugins.local/$plugin_name"; $plugin_dir = Config::get_self_dir() . "/plugins.local/$plugin_name";
if (is_dir($plugin_dir)) { if (is_dir($plugin_dir)) {
$status = $this->_recursive_rmdir($plugin_dir); $status = $this->_recursive_rmdir($plugin_dir);
@ -1199,7 +1199,7 @@ class Pref_Prefs extends Handler_Protected {
if ($_SESSION["access_level"] >= UserHelper::ACCESS_LEVEL_ADMIN && Config::get(Config::ENABLE_PLUGIN_INSTALLER)) { if ($_SESSION["access_level"] >= UserHelper::ACCESS_LEVEL_ADMIN && Config::get(Config::ENABLE_PLUGIN_INSTALLER)) {
$plugin_name = basename(clean($_REQUEST['plugin'])); $plugin_name = basename(clean($_REQUEST['plugin']));
$all_plugins = $this->_get_available_plugins(); $all_plugins = $this->_get_available_plugins();
$plugin_dir = dirname(dirname(__DIR__)) . "/plugins.local"; $plugin_dir = Config::get_self_dir() . "/plugins.local";
$work_dir = "$plugin_dir/plugin-installer"; $work_dir = "$plugin_dir/plugin-installer";
@ -1306,7 +1306,7 @@ class Pref_Prefs extends Handler_Protected {
function checkForPluginUpdates(): void { function checkForPluginUpdates(): void {
if ($_SESSION["access_level"] >= UserHelper::ACCESS_LEVEL_ADMIN && Config::get(Config::CHECK_FOR_UPDATES) && Config::get(Config::CHECK_FOR_PLUGIN_UPDATES)) { if ($_SESSION["access_level"] >= UserHelper::ACCESS_LEVEL_ADMIN && Config::get(Config::CHECK_FOR_UPDATES) && Config::get(Config::CHECK_FOR_PLUGIN_UPDATES)) {
$plugin_name = $_REQUEST["name"] ?? ""; $plugin_name = $_REQUEST["name"] ?? "";
$root_dir = dirname(dirname(__DIR__)); # we're in classes/pref/ $root_dir = Config::get_self_dir();
$rv = empty($plugin_name) ? self::_get_updated_plugins() : [ $rv = empty($plugin_name) ? self::_get_updated_plugins() : [
["plugin" => $plugin_name, "rv" => self::_plugin_needs_update($root_dir, $plugin_name)], ["plugin" => $plugin_name, "rv" => self::_plugin_needs_update($root_dir, $plugin_name)],
@ -1324,8 +1324,7 @@ class Pref_Prefs extends Handler_Protected {
$plugins = array_filter($plugins, 'strlen'); $plugins = array_filter($plugins, 'strlen');
} }
# we're in classes/pref/ $root_dir = Config::get_self_dir();
$root_dir = dirname(dirname(__DIR__));
$rv = []; $rv = [];

12
tests/SelfDirTest.php Normal file
View File

@ -0,0 +1,12 @@
<?php
use PHPUnit\Framework\TestCase;
final class ConfigTest extends TestCase {
public function test_get_self_dir(): void {
$this->assertEquals(
dirname(__DIR__), # we're in (app)/tests/
Config::get_self_dir()
);
}
}