Skip to content

Commit

Permalink
fix: remove all direct config accessors
Browse files Browse the repository at this point in the history
This addresses an issue in pfSense 24.11 where the global  variable has been deprecated
  • Loading branch information
jaredhendrickson13 committed Dec 6, 2024
1 parent 7e15d95 commit 191b321
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class Form {
*/
final public function print_form(): void {
# Print the static pfSense UI and include any custom CSS or JS files
global $config, $user_settings;
global $user_settings;
$pgtitle = $this->title_path;
include 'head.inc';
echo "<link rel='stylesheet' href='$this->custom_css'/>";
Expand Down
18 changes: 6 additions & 12 deletions pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Core/Model.inc
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,7 @@ class Model {
*/
protected static function init_config(string $path) {
# Initialize the configuration array of a specified path.
global $config;
array_init_path($config, $path);
config_init_path($path);
}

/**
Expand All @@ -562,8 +561,7 @@ class Model {
* path keys an empty string and $default is non-null
*/
public static function get_config(string $path, mixed $default = null) {
global $config;
return array_get_path($config, $path, $default);
return config_get_path($path, $default);
}

/**
Expand All @@ -574,8 +572,7 @@ class Model {
* @returns mixed $val or $default if the path prefix does not exist
*/
public static function set_config(string $path, mixed $value, mixed $default = null) {
global $config;
return array_set_path($config, $path, $value, $default);
return config_set_path($path, $value, $default);
}

/**
Expand Down Expand Up @@ -616,8 +613,7 @@ class Model {
* @returns array copy of the removed value or null
*/
public static function del_config(string $path): mixed {
global $config;
return array_del_path($config, $path);
return config_del_path($path);
}

/**
Expand All @@ -630,8 +626,7 @@ class Model {
* non-null value, otherwise false.
*/
public static function is_config_enabled(string $path, string $enable_key = 'enable'): bool {
global $config;
return array_path_enabled($config, $path, $enable_key);
return config_path_enabled($path, $enable_key);
}

/**
Expand Down Expand Up @@ -680,8 +675,7 @@ class Model {
* @param bool $force_parse Force an entire reparse of the config.xml file instead of the cached config.
*/
public static function reload_config(bool $force_parse = false): void {
global $config;
$config = parse_config(parse: $force_parse);
config_set_path("", parse_config(parse: $force_parse));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ class TestCase {
* @throws Error|Exception The exception thrown by failed Tests, typically an AssertionError.
*/
public function run(): void {
# Need direct config access to revert any changes made by Tests
global $config;

# Collect available environment variables
$this->get_envs();

Expand All @@ -64,7 +61,7 @@ class TestCase {
# If this method starts with `test`, run the function.
if (str_starts_with($method, 'test')) {
# Backup the current config so we can undo any changes made during the test
$original_config = unserialize(serialize($config));
$original_config = config_get_path("");

# Set the current method undergoing testing
$this->method = $method;
Expand All @@ -78,14 +75,14 @@ class TestCase {
$this->$method();
} catch (Error | Exception $e) {
# Restore the original configuration, teardown the TestCase and throw the encountered error
$config = $original_config;
config_set_path("", $original_config);
write_config("Restored config after API test '$method'");
$this->teardown();
throw $e;
}

# Restore the config as it was when the test began.
$config = $original_config;
config_set_path("", $original_config);
write_config("Restored config after API test '$method'");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ class RoutingGateway extends Model {
# Ensure the gateway cannot be disabled while it is in use by a system DNS server
# TODO: replace with a Model query when the SystemDNSServer Model is developed
$dnsgw_counter = 1;
while (isset($config['system']["dns{$dnsgw_counter}gw"])) {
if ($this->name->value == $config['system']["dns{$dnsgw_counter}gw"]) {
while ($this->get_config("system/dns{$dnsgw_counter}gw")) {
if ($this->name->value == $this->get_config("system/dns{$dnsgw_counter}gw")) {
throw new ValidationError(
message: 'Gateway cannot be disabled because it is in use by a system DNS ' .
"server with ID `$dnsgw_counter`",
Expand Down

0 comments on commit 191b321

Please sign in to comment.