Skip to content

Commit

Permalink
Convert Config to a singleton (#16386)
Browse files Browse the repository at this point in the history
Continuation of #14364 by @Jellyfrog
This time, make the old class a shim for the facade.  Will update references in a separate PR.
  • Loading branch information
murrant authored Nov 22, 2024
1 parent ac220e4 commit 4191af5
Show file tree
Hide file tree
Showing 16 changed files with 711 additions and 458 deletions.
439 changes: 28 additions & 411 deletions LibreNMS/Config.php

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions LibreNMS/IRCBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace LibreNMS;

use App\Facades\LibrenmsConfig;
use App\Models\Device;
use App\Models\Eventlog;
use App\Models\Port;
Expand Down Expand Up @@ -730,9 +731,9 @@ private function _reload($params)

return $this->loadExternal();
}
$new_config = Config::load();
LibrenmsConfig::reload();
$this->respond('Reloading configuration & defaults');
if ($new_config != $this->config) {
if (LibrenmsConfig::getAll() != $this->config) {
$this->__construct();

return;
Expand Down
6 changes: 3 additions & 3 deletions LibreNMS/Util/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

namespace LibreNMS\Util;

use App\Facades\LibrenmsConfig;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Str;
use LibreNMS\Config;
use LibreNMS\Traits\RuntimeClassCache;
use Symfony\Component\Process\Process;

Expand All @@ -42,7 +42,7 @@ class Git
public function __construct(int $cache = 0)
{
$this->runtimeCacheExternalTTL = $cache;
$this->install_dir = Config::get('install_dir', realpath(__DIR__ . '/../..'));
$this->install_dir = realpath(__DIR__ . '/../..');
}

public static function make(int $cache = 0): Git
Expand Down Expand Up @@ -196,7 +196,7 @@ private function remoteCommit(): array
return $this->cacheGet('remoteCommit', function () {
if ($this->isAvailable()) {
try {
return (array) Http::client()->get(Config::get('github_api') . 'commits/master')->json();
return (array) Http::client()->get(LibrenmsConfig::get('github_api') . 'commits/master')->json();
} catch (ConnectionException $e) {
}
}
Expand Down
14 changes: 8 additions & 6 deletions LibreNMS/Util/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

namespace LibreNMS\Util;

use App\ConfigRepository;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use LibreNMS\Config;
use LibreNMS\DB\Eloquent;
use Symfony\Component\Process\Process;

Expand All @@ -38,20 +38,22 @@ class Version

/** @var Git convenience instance */
public $git;
private ConfigRepository $config;

public function __construct()
public function __construct(ConfigRepository $config)
{
$this->config = $config;
$this->git = Git::make();
}

public static function get(): Version
{
return new static;
return new static(app('librenms-config'));
}

public function release(): string
{
return Config::get('update_channel') == 'master' ? 'master' : self::VERSION;
return $this->config->get('update_channel') == 'master' ? 'master' : self::VERSION;
}

public function date(string $format = 'c'): string
Expand Down Expand Up @@ -135,7 +137,7 @@ public function python(): string

public function rrdtool(): string
{
$process = new Process([Config::get('rrdtool', 'rrdtool'), '--version']);
$process = new Process([$this->config->get('rrdtool', 'rrdtool'), '--version']);
$process->run();
preg_match('/^RRDtool ([\w.]+) /', $process->getOutput(), $matches);

Expand All @@ -144,7 +146,7 @@ public function rrdtool(): string

public function netSnmp(): string
{
$process = new Process([Config::get('snmpget', 'snmpget'), '-V']);
$process = new Process([$this->config->get('snmpget', 'snmpget'), '-V']);

$process->run();
preg_match('/[\w.]+$/', $process->getErrorOutput(), $matches);
Expand Down
Loading

0 comments on commit 4191af5

Please sign in to comment.