mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Move Config loading to a service provider (#13927)
* Move Config loading to a service provider That way other service providers can depend on it Move various random listener registrations into the EventServiceProvider Various startup cleanup * Config::persist Set live variable before persisting incase db update fail * Disable strict mode for legacy code (init.php) * Disable debug after os test data is gathered * remove Eloquent::boot it is never used * remove Eloquent::version * lint fixes * style fixes * there is no c_echo here
This commit is contained in:
@@ -25,59 +25,19 @@
|
||||
|
||||
namespace LibreNMS\DB;
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Events\StatementPrepared;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
use Illuminate\Support\Arr;
|
||||
use DB;
|
||||
use Illuminate\Database\Connection;
|
||||
use LibreNMS\Util\Laravel;
|
||||
use PDOException;
|
||||
|
||||
class Eloquent
|
||||
{
|
||||
/** @var Capsule static reference to capsule */
|
||||
private static $capsule;
|
||||
|
||||
public static function boot()
|
||||
{
|
||||
// boot Eloquent outside of Laravel
|
||||
if (! Laravel::isBooted() && is_null(self::$capsule)) {
|
||||
$install_dir = realpath(__DIR__ . '/../../');
|
||||
|
||||
Dotenv::createMutable($install_dir)->load();
|
||||
|
||||
$db_config = include $install_dir . '/config/database.php';
|
||||
$settings = $db_config['connections'][$db_config['default']];
|
||||
|
||||
self::$capsule = new Capsule;
|
||||
self::$capsule->addConnection($settings);
|
||||
self::$capsule->setEventDispatcher(new Dispatcher());
|
||||
self::$capsule->setAsGlobal();
|
||||
self::$capsule->bootEloquent();
|
||||
}
|
||||
|
||||
self::initLegacyListeners();
|
||||
self::setStrictMode(false); // set non-strict mode if for legacy code
|
||||
}
|
||||
|
||||
public static function initLegacyListeners()
|
||||
{
|
||||
if (self::isConnected()) {
|
||||
// set FETCH_ASSOC for queries that required by setting the global variable $PDO_FETCH_ASSOC (for dbFacile)
|
||||
self::DB()->getEventDispatcher()->listen(StatementPrepared::class, function ($event) {
|
||||
global $PDO_FETCH_ASSOC;
|
||||
if ($PDO_FETCH_ASSOC) {
|
||||
$event->statement->setFetchMode(\PDO::FETCH_ASSOC);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the strict mode for the current connection (will not persist)
|
||||
*
|
||||
* @param bool $strict
|
||||
*/
|
||||
public static function setStrictMode($strict = true)
|
||||
public static function setStrictMode(bool $strict = true): void
|
||||
{
|
||||
if (self::isConnected() && self::getDriver() == 'mysql') {
|
||||
if ($strict) {
|
||||
@@ -88,14 +48,16 @@ class Eloquent
|
||||
}
|
||||
}
|
||||
|
||||
public static function isConnected($name = null)
|
||||
public static function isConnected(?string $name = null): bool
|
||||
{
|
||||
try {
|
||||
$conn = self::DB($name);
|
||||
if ($conn) {
|
||||
return ! is_null($conn->getPdo());
|
||||
$conn->getPdo();
|
||||
|
||||
return true;
|
||||
}
|
||||
} catch (\PDOException $e) {
|
||||
} catch (PDOException $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -105,31 +67,39 @@ class Eloquent
|
||||
/**
|
||||
* Access the Database Manager for Fluent style queries. Like the Laravel DB facade.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $name
|
||||
* @return \Illuminate\Database\Connection|null
|
||||
*/
|
||||
public static function DB($name = null)
|
||||
public static function DB(?string $name = null): ?Connection
|
||||
{
|
||||
// check if Laravel is booted
|
||||
if (Laravel::isBooted()) {
|
||||
return \DB::connection($name);
|
||||
return DB::connection($name);
|
||||
}
|
||||
|
||||
if (is_null(self::$capsule)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return self::$capsule->getDatabaseManager()->connection($name);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getDriver()
|
||||
public static function getDriver(): ?string
|
||||
{
|
||||
$connection = config('database.default');
|
||||
|
||||
return config("database.connections.{$connection}.driver");
|
||||
}
|
||||
|
||||
public static function setConnection($name, $db_host = null, $db_user = '', $db_pass = '', $db_name = '', $db_port = null, $db_socket = null)
|
||||
/**
|
||||
* Set the active connection, used during install
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $db_host
|
||||
* @param string $db_user
|
||||
* @param string $db_pass
|
||||
* @param string $db_name
|
||||
* @param int|string $db_port
|
||||
* @param string $db_socket
|
||||
* @return void
|
||||
*/
|
||||
public static function setConnection($name, $db_host = null, $db_user = '', $db_pass = '', $db_name = '', $db_port = null, $db_socket = null): void
|
||||
{
|
||||
\Config::set("database.connections.$name", [
|
||||
'driver' => 'mysql',
|
||||
@@ -147,9 +117,4 @@ class Eloquent
|
||||
]);
|
||||
\Config::set('database.default', $name);
|
||||
}
|
||||
|
||||
public static function version($name = null)
|
||||
{
|
||||
return Arr::first(self::DB($name)->selectOne('select version()'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user