Files

78 lines
2.7 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Http\ViewComposers;
2026-10-12 07:47:42 -04:00
use Illuminate\Http\Request;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
2026-11-05 14:23:59 -05:00
use Pterodactyl\Actions\Settings\ReplaceBrandLogo;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Services\Extensions\ExtensionHeadTags;
2026-10-07 17:15:57 -04:00
use Pterodactyl\Services\Extensions\ExtensionRepository;
use Pterodactyl\Services\Helpers\AssetHashService;
2026-10-12 07:47:42 -04:00
use Pterodactyl\Services\Themes\ThemeResolver;
2026-10-05 18:10:18 -04:00
use Throwable;
class AssetComposer
{
/**
* AssetComposer constructor.
*/
2026-10-05 18:10:18 -04:00
public function __construct(
private readonly AssetHashService $assetHashService,
2026-10-07 17:15:57 -04:00
private readonly ExtensionRepository $extensions,
2026-10-05 18:10:18 -04:00
private readonly ExtensionHeadTags $extensionHeadTags,
2026-10-12 07:47:42 -04:00
private readonly ThemeResolver $themes,
private readonly Request $request,
2026-10-05 18:10:18 -04:00
) {}
/**
* Provide access to the asset service in the views.
*/
2022-10-14 10:59:20 -06:00
public function compose(View $view): void
{
2026-10-12 07:47:42 -04:00
$theme = $this->themes->document($this->request);
$view->with('asset', $this->assetHashService);
2026-10-05 18:10:18 -04:00
// Rendered lazily by the layout; resolving the tags never throws.
$view->with('extensionHead', $this->extensionHeadTags);
2026-10-12 07:47:42 -04:00
$view->with('theme', $theme);
2019-12-15 18:05:44 -08:00
$view->with('siteConfiguration', [
2026-10-05 18:10:18 -04:00
'name' => config('app.name', 'Pterodactyl'),
2026-11-05 14:23:59 -05:00
'logo' => ReplaceBrandLogo::url(),
2026-10-05 18:10:18 -04:00
'locale' => config('app.locale', 'en'),
2026-10-20 21:09:33 +02:00
'userAvatarsEnabled' => config('pterodactyl.client_features.avatars.enabled', false),
2019-12-15 18:05:44 -08:00
'recaptcha' => [
'enabled' => config('recaptcha.enabled', false),
2026-10-05 18:10:18 -04:00
'siteKey' => config('recaptcha.website_key', ''),
2019-12-15 18:05:44 -08:00
],
2026-10-12 07:47:42 -04:00
// Safe mode loads no extension bundles at all.
2026-10-13 15:06:23 -04:00
'extensions' => $theme->safeMode ? [] : $this->extensionPayload(array_column($theme->chain, 'id')),
2026-10-12 07:47:42 -04:00
'theme' => $theme->bootstrap(),
2019-12-15 18:05:44 -08:00
]);
}
2026-10-05 18:10:18 -04:00
/**
* The enabled frontend extensions the SPA boot loader should import, with their
2026-10-13 15:06:23 -04:00
* frontend settings for signed-in users and only the public ones for guests, and
* the themes of the chain the page renders with. Never allowed to break page
* rendering - worst case the SPA sees no extensions.
2026-10-05 18:10:18 -04:00
*
2026-10-13 15:06:23 -04:00
* @param list<string> $themes
2026-10-11 02:33:18 -04:00
* @return array<int, array{id: string, version?: string, entry: string}>
2026-10-05 18:10:18 -04:00
*/
2026-10-13 15:06:23 -04:00
private function extensionPayload(array $themes): array
2026-10-05 18:10:18 -04:00
{
if (! config('extensions.enabled')) {
return [];
}
try {
2026-10-13 15:06:23 -04:00
return $this->extensions->frontendPayload(authenticated: Auth::check(), themes: $themes);
2026-10-05 18:10:18 -04:00
} catch (Throwable) {
return [];
}
}
}