Files
panel-pelican-dev/app/Providers/AppServiceProvider.php

132 lines
4.1 KiB
PHP
Raw Normal View History

<?php
2024-03-12 22:39:16 -04:00
namespace App\Providers;
2024-03-19 20:42:40 -04:00
use App\Extensions\Themes\Theme;
2024-03-12 22:39:16 -04:00
use App\Models;
2024-03-19 20:42:40 -04:00
use App\Models\ApiKey;
2024-03-16 15:11:10 -04:00
use App\Models\Node;
2024-03-19 20:42:40 -04:00
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Pagination\Paginator;
2024-03-19 20:42:40 -04:00
use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\Facades\Cache;
2024-03-19 20:42:40 -04:00
use Illuminate\Support\Facades\Http;
2017-09-24 22:34:30 -05:00
use Illuminate\Support\Facades\Schema;
2024-03-19 20:42:40 -04:00
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
2024-03-19 20:42:40 -04:00
use Illuminate\Support\Str;
use Laravel\Sanctum\Sanctum;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
2023-02-23 12:30:16 -07:00
public function boot(): void
{
2017-09-24 22:34:30 -05:00
Schema::defaultStringLength(191);
View::share('appVersion', $this->versionData()['version'] ?? 'undefined');
View::share('appIsGit', $this->versionData()['is_git'] ?? false);
Paginator::useBootstrap();
// If the APP_URL value is set with https:// make sure we force it here. Theoretically
// this should just work with the proxy logic, but there are a lot of cases where it
// doesn't, and it triggers a lot of support requests, so lets just head it off here.
if (Str::startsWith(config('app.url') ?? '', 'https://')) {
URL::forceScheme('https');
}
Relation::enforceMorphMap([
'allocation' => Models\Allocation::class,
'api_key' => Models\ApiKey::class,
'backup' => Models\Backup::class,
'database' => Models\Database::class,
'egg' => Models\Egg::class,
'egg_variable' => Models\EggVariable::class,
'schedule' => Models\Schedule::class,
'server' => Models\Server::class,
'ssh_key' => Models\UserSSHKey::class,
'task' => Models\Task::class,
'user' => Models\User::class,
]);
2024-03-16 15:11:10 -04:00
Http::macro(
'daemon',
2024-03-16 18:53:20 -04:00
fn (Node $node, array $headers = []) => Http::acceptJson()
2024-03-23 12:31:31 -04:00
->asJson()
2024-03-16 18:53:20 -04:00
->withToken($node->getDecryptedKey())
->withHeaders($headers)
2024-03-16 15:11:10 -04:00
->withOptions(['verify' => (bool) app()->environment('production')])
2024-03-16 18:53:20 -04:00
->timeout(config('panel.guzzle.timeout'))
->connectTimeout(config('panel.guzzle.connect_timeout'))
2024-03-16 15:11:10 -04:00
->baseUrl($node->getConnectionAddress())
);
2024-03-19 20:42:40 -04:00
$this->bootAuth();
$this->bootBroadcast();
}
2017-12-30 16:33:00 -06:00
/**
* Register application service providers.
*/
2023-02-23 12:30:16 -07:00
public function register(): void
2017-12-30 16:33:00 -06:00
{
2024-03-19 21:02:19 -04:00
// Only load the settings service provider if the environment is configured to allow it.
2024-03-12 22:39:16 -04:00
if (!config('panel.load_environment_only', false) && $this->app->environment() !== 'testing') {
2017-12-30 16:33:00 -06:00
$this->app->register(SettingsServiceProvider::class);
}
$this->app->singleton('extensions.themes', function () {
2021-01-23 12:33:34 -08:00
return new Theme();
});
2017-12-30 16:33:00 -06:00
}
/**
* Return version information for the footer.
*/
protected function versionData(): array
{
return Cache::remember('git-version', 5, function () {
if (file_exists(base_path('.git/HEAD'))) {
$head = explode(' ', file_get_contents(base_path('.git/HEAD')));
2017-07-23 17:55:38 -05:00
if (array_key_exists(1, $head)) {
$path = base_path('.git/' . trim($head[1]));
}
}
if (isset($path) && file_exists($path)) {
return [
'version' => substr(file_get_contents($path), 0, 8),
'is_git' => true,
];
}
return [
'version' => config('app.version'),
'is_git' => false,
];
});
}
2024-03-19 20:42:40 -04:00
public function bootAuth(): void
{
Sanctum::usePersonalAccessTokenModel(ApiKey::class);
}
public function bootBroadcast(): void
{
Broadcast::routes();
/*
* Authenticate the user's personal channel...
*/
Broadcast::channel('App.User.*', function ($user, $userId) {
return (int) $user->id === (int) $userId;
});
}
}