freescout-itk-prometheus/Providers/ItkPrometheusServiceProvider.php
2024-09-23 12:46:21 +02:00

124 lines
2.8 KiB
PHP

<?php
namespace Modules\ItkPrometheus\Providers;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Factory;
use Modules\ItkPrometheus\Service\PrometheusService;
use Prometheus\CollectorRegistry;
use Prometheus\Storage\Redis;
define('ITK_PROMETHEUS_MODULE', 'itkprometheus');
require_once __DIR__ . '/../vendor/autoload.php';
class ItkPrometheusServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = FALSE;
/**
* Boot the application events.
*
* @return void
*/
public function boot(): void {
$this->registerConfig();
$this->registerViews();
$this->registerFactories();
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
}
/**
* Register the service provider.
*
* @return void
*/
public function register():void {
$this->registerTranslations();
$this->app->singleton(CollectorRegistry::class, function () {
Redis::setDefaultOptions(
Arr::only(config('itkprometheus.redisconnection'), [
'host',
'password',
'username',
])
);
$this->app->singleton(PrometheusService::class, PrometheusService::class);
return CollectorRegistry::getDefault();
});
}
/**
* Register config.
*
* @return void
*/
protected function registerConfig(): void {
$this->publishes([
__DIR__ . '/../Config/config.php' => config_path('itkprometheus.php'),
], 'config');
$this->mergeConfigFrom(
__DIR__ . '/../Config/config.php', 'itkprometheus'
);
}
/**
* Register views.
*
* @return void
*/
public function registerViews(): void {
$viewPath = resource_path('views/modules/itkprometheus');
$sourcePath = __DIR__ . '/../Resources/views';
$this->publishes([
$sourcePath => $viewPath,
], 'views');
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/itkprometheus';
}, \Config::get('view.paths')), [$sourcePath]), 'itkprometheus');
}
/**
* Register translations.
*
* @return void
*/
public function registerTranslations(): void {
$this->loadJsonTranslationsFrom(__DIR__ . '/../Resources/lang');
}
/**
* Register an additional directory of factories.
* @source https://github.com/sebastiaanluca/laravel-resource-flow/blob/develop/src/Modules/ModuleServiceProvider.php#L66
*
* @return void
*/
public function registerFactories():void {
if (!app()->environment('production')) {
app(Factory::class)->load(__DIR__ . '/../Database/factories');
}
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides(): array {
return [];
}
}