mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
* Reorganize trap tests * Testing db DRIVER to prevent .env from interfering * New code to detect if Laravel is booted. Hopefully more reliable. * WIP external test process * revert module test helper * Use .env in Eloquent::boot() * Fix test database settings loading * fix undefined classes (didn't find the one I needed) * Fix incorrect Config usages And RrdDefinition return type * fix .env loading * use the right DB * slightly more accurate isConnected * Move db_name to DBSetupTest specifically * restore $_SERVER in AuthSSOTest * missed item * WIP * tear down in the correct order. * some testing cleanups * remove check for duplicate event listener, it's not working right * Don't need this change anymore * Implement Log::event to replace legacy function log_event() * fix port tests * fix up tests * remove pointless TrapTestCase class * fix style * Fix db config not being merged... * skip env check for tests * defer database operations until after Laravel is booted. * don't include dbFaciale... * redundant use
105 lines
3.2 KiB
PHP
105 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
|
use Illuminate\Support\Facades\Blade;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use LibreNMS\Config;
|
|
use LibreNMS\Util\IP;
|
|
use LibreNMS\Util\Validate;
|
|
use Validator;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Bootstrap any application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
$this->app->booted('\LibreNMS\DB\Eloquent::initLegacyListeners');
|
|
$this->app->booted('\LibreNMS\Config::load');
|
|
|
|
$this->bootCustomBladeDirectives();
|
|
$this->bootCustomValidators();
|
|
$this->configureMorphAliases();
|
|
}
|
|
|
|
/**
|
|
* Register any application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
$this->registerFacades();
|
|
$this->registerGeocoder();
|
|
}
|
|
|
|
private function bootCustomBladeDirectives()
|
|
{
|
|
Blade::if('config', function ($key) {
|
|
return \LibreNMS\Config::get($key);
|
|
});
|
|
Blade::if('notconfig', function ($key) {
|
|
return !\LibreNMS\Config::get($key);
|
|
});
|
|
Blade::if('admin', function () {
|
|
return auth()->check() && auth()->user()->isAdmin();
|
|
});
|
|
}
|
|
|
|
private function configureMorphAliases()
|
|
{
|
|
Relation::morphMap([
|
|
'interface' => \App\Models\Port::class,
|
|
'sensor' => \App\Models\Sensor::class,
|
|
'device' => \App\Models\Device::class,
|
|
'device_group' => \App\Models\DeviceGroup::class,
|
|
]);
|
|
}
|
|
|
|
private function registerFacades()
|
|
{
|
|
// replace log manager so we can add the event function
|
|
$this->app->bind('log', function ($app) {
|
|
return new \App\Facades\LogManager($app);
|
|
});
|
|
}
|
|
|
|
private function registerGeocoder()
|
|
{
|
|
$this->app->alias(\LibreNMS\Interfaces\Geocoder::class, 'geocoder');
|
|
$this->app->bind(\LibreNMS\Interfaces\Geocoder::class, function ($app) {
|
|
$engine = Config::get('geoloc.engine');
|
|
|
|
switch ($engine) {
|
|
case 'mapquest':
|
|
Log::debug('MapQuest geocode engine');
|
|
return $app->make(\App\ApiClients\MapquestApi::class);
|
|
case 'bing':
|
|
Log::debug('Bing geocode engine');
|
|
return $app->make(\App\ApiClients\BingApi::class);
|
|
case 'openstreetmap':
|
|
Log::debug('OpenStreetMap geocode engine');
|
|
return $app->make(\App\ApiClients\NominatimApi::class);
|
|
default:
|
|
case 'google':
|
|
Log::debug('Google Maps geocode engine');
|
|
return $app->make(\App\ApiClients\GoogleMapsApi::class);
|
|
}
|
|
});
|
|
}
|
|
|
|
private function bootCustomValidators()
|
|
{
|
|
Validator::extend('ip_or_hostname', function ($attribute, $value, $parameters, $validator) {
|
|
$ip = substr($value, 0, strpos($value, '/') ?: strlen($value)); // allow prefixes too
|
|
return IP::isValid($ip) || Validate::hostname($value);
|
|
}, __('The :attribute must a valid IP address/network or hostname.'));
|
|
}
|
|
}
|