Skip to content

Commit

Permalink
Add ProviderFactoryServiceProvider with .env var to specify SystemInf…
Browse files Browse the repository at this point in the history
…o outgoing_ips
  • Loading branch information
uphlewis committed Apr 5, 2023
1 parent 9137e60 commit 45925bb
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ LOG_CHANNEL=stack

DB_CONNECTION=sqlite
DB_DATABASE="database.sqlite"

OUTGOING_IPS=
65 changes: 65 additions & 0 deletions app/Providers/ProviderFactoryServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Providers;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
use Psr\Log\LoggerInterface;
use Upmind\ProvisionBase\Provider\DataSet\SystemInfo;
use Upmind\ProvisionBase\ProviderFactory;
use Upmind\ProvisionBase\Registry\Registry;

class ProviderFactoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->singleton(SystemInfo::class, function ($app) {
return new SystemInfo([
'outgoing_ips' => $this->getOutgoingIps(),
]);
});

$this->app->singleton(ProviderFactory::class, function ($app) {
return new ProviderFactory(
$app->make(Registry::class),
$app->make(Filesystem::class),
$app->make(LoggerInterface::class),
$app->make(SystemInfo::class),
);
});
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}

/**
* @return string[]
*/
protected function getOutgoingIps(): array
{
// Get IPs from config
if ($ips = array_filter(array_map('trim', explode(',', config('app.outgoing_ips') ?: '')))) {
return $ips;
}

// Try to determine IP automatically
return Arr::wrap(
isset($_SERVER['SERVER_ADDR'])
? $_SERVER['SERVER_ADDR']
: gethostbyname(gethostname() ?: php_uname('n'))
);
}
}
7 changes: 6 additions & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,

App\Providers\ProviderFactoryServiceProvider::class,
],

/*
Expand Down Expand Up @@ -228,4 +228,9 @@

],

/**
* Application Config Settings
*/
'outgoing_ips' => env('OUTGOING_IPS'),

];

0 comments on commit 45925bb

Please sign in to comment.