Files

305 lines
12 KiB
PHP
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Services\Extensions;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Builder;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Pterodactyl\Events\Extensions\ExtensionLoadFailed;
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException;
use Pterodactyl\Models\Extension;
use Symfony\Component\Finder\SplFileInfo;
2026-10-05 18:10:18 -04:00
use Throwable;
class ExtensionRepository
{
/** @var Collection<string, ExtensionManifest>|null */
private ?Collection $discovered = null;
/** @var Collection<string, ExtensionManifest>|null */
private ?Collection $enabled = null;
/** @var array<string, string> in-memory discovery errors keyed by directory name */
private array $discoveryErrors = [];
/** @var array<string, ExtensionSettings> */
private array $settings = [];
/** @var array<string, true> Provider failures in this application instance, retained across discovery refreshes. */
private array $failedProviders = [];
public function __construct(
private readonly ExtensionManifestValidator $validator,
private readonly ExtensionAssetPublisher $assets,
private readonly Dispatcher $events,
private readonly ExtensionSettingsRegistry $settingsRegistry,
private readonly ExtensionCompatibility $compatibility,
) {}
public function directory(): string
{
2026-10-11 16:09:53 -04:00
return mb_rtrim(config()->string('extensions.directory'), '/\\');
2026-10-05 18:10:18 -04:00
}
/**
* @return Collection<string, ExtensionManifest>
*/
public function discovered(): Collection
{
if ($this->discovered instanceof Collection) {
return $this->discovered;
}
$manifests = [];
foreach (glob($this->directory().'/*/'.ExtensionManifest::FILENAME) ?: [] as $file) {
$directory = dirname($file);
try {
$manifest = $this->validator->fromDirectory($directory);
if ($manifest->id !== basename($directory)) {
throw new InvalidExtensionException(sprintf('Extension directory "%s" must match its manifest id "%s".', basename($directory), $manifest->id));
}
$manifests[$manifest->id] = $manifest;
} catch (InvalidExtensionException $exception) {
$this->discoveryErrors[basename($directory)] = $exception->getMessage();
Log::warning('[extensions] Skipping extension', ['directory' => $directory, 'exception' => $exception]);
}
}
return $this->discovered = collect($manifests);
}
/**
* @return array<string, string>
*/
public function discoveryErrors(): array
{
return $this->discoveryErrors;
}
public function flushDiscovery(): void
{
$this->discovered = null;
$this->enabled = null;
$this->discoveryErrors = [];
}
/**
* @return Collection<string, Extension>
*/
public function records(): Collection
{
return rescue(function (): Collection {
if (! Schema::hasTable('extensions')) {
return $this->recordCollection([]);
}
$records = [];
foreach (Extension::query()->get() as $extension) {
$records[$extension->identifier] = $extension;
}
return $this->recordCollection($records);
}, fn (): Collection => $this->recordCollection([]));
}
/**
* @return Collection<string, ExtensionManifest>
*/
public function enabled(): Collection
{
if ($this->enabled instanceof Collection) {
return $this->enabled;
}
$configured = $this->configuredEnabled()->reject(fn (ExtensionManifest $manifest): bool => isset($this->failedProviders[$manifest->id]));
$result = $this->compatibility->resolve($configured);
foreach ($result['errors'] as $identifier => $reason) {
$this->recordFailure($identifier, $reason, phase: 'compatibility');
}
return $this->enabled = $result['manifests'];
}
/** Whether an extension's runtime registrations may run right now. */
public function isAvailable(string $identifier): bool
{
return config('extensions.enabled') && $this->enabled()->has($identifier);
}
/** @return Collection<string, ExtensionManifest> */
public function configuredEnabled(): Collection
{
$discovered = $this->discovered();
if ($discovered->isEmpty()) {
return $discovered;
}
$records = $this->records();
return $discovered->filter(function (ExtensionManifest $manifest) use ($records): bool {
$record = $records->get($manifest->id);
return $record !== null && $record->enabled;
});
}
/**
* The enabled frontend extensions for the SPA to load. Signed-in users receive
* each extension's frontend settings; everyone else only those marked ->public().
*
2026-10-11 02:33:18 -04:00
* Guests only need what renders on the sign-in pages: the bundle, its styles and
2026-10-12 07:47:42 -04:00
* translations, and public settings. Versions, screens and view overrides are
2026-10-11 02:33:18 -04:00
* left out so the login page does not describe the panel's extensions, and the
2026-10-13 15:06:23 -04:00
* SPA skips screen and view registrations for an entry that has none. Of the
* themes, only those of the chain the page renders with come along, marked
* `theme`, so their setup runs while they are active.
2026-10-11 02:33:18 -04:00
*
2026-10-13 15:06:23 -04:00
* @param list<string> $themes the ids of the theme chain the page renders with
* @return array<int, array{id: string, version?: string, entry: string, prefix: string|null, translations: string|null, config: object, screens?: list<ExtensionScreenDefinition>, views?: list<string>, theme?: bool, development: array{url: string, version: string, scripts: string}|null}>
2026-10-05 18:10:18 -04:00
*/
2026-10-13 15:06:23 -04:00
public function frontendPayload(bool $authenticated, array $themes = []): array
2026-10-05 18:10:18 -04:00
{
2026-10-13 15:06:23 -04:00
$enabled = $this->enabled()->filter(fn (ExtensionManifest $manifest): bool => $manifest->hasUi() && (! $manifest->isTheme() || in_array($manifest->id, $themes, true)));
2026-10-05 18:10:18 -04:00
$configured = $authenticated ? $enabled : $enabled->filter(fn (ExtensionManifest $manifest): bool => ($this->settingsRegistry->get($manifest->id)?->publicConfigKeys() ?? []) !== []);
if ($configured->isNotEmpty()) {
rescue(fn () => ExtensionSettings::preload(array_values($configured->map(fn (ExtensionManifest $manifest): ExtensionSettings => $this->settings($manifest->id))->all())), report: false);
}
2026-10-11 02:33:18 -04:00
return $enabled->map(function (ExtensionManifest $manifest) use ($authenticated): array {
$entry = [
'id' => $manifest->id,
'entry' => $this->assets->entryUrl($manifest),
'prefix' => $manifest->uiPrefix,
'translations' => $this->translationsRevision($manifest),
'config' => (object) $this->frontendConfig($manifest->id, ! $authenticated),
'development' => $this->assets->developmentPayload($manifest->id),
];
if ($authenticated) {
$entry['version'] = $manifest->version;
$entry['screens'] = $manifest->screens;
2026-10-12 07:47:42 -04:00
$entry['views'] = $manifest->views;
2026-10-11 02:33:18 -04:00
}
2026-10-13 15:06:23 -04:00
if ($manifest->isTheme()) {
$entry['theme'] = true;
}
2026-10-11 02:33:18 -04:00
return $entry;
})
2026-10-05 18:10:18 -04:00
->values()
->all();
}
public function recordFailure(string $identifier, string $reason, ?Throwable $exception = null, string $phase = 'runtime'): void
{
if (in_array($phase, ['register', 'boot'], true)) {
$this->failedProviders[$identifier] = true;
$this->enabled = null;
}
// A failure that repeats on every request (reading field values, for one) is written once.
rescue(fn (): int => Extension::query()
->where('identifier', $identifier)
->where(fn (Builder $query): Builder => $query->whereNull('error')->orWhere('error', '<>', $reason))
2026-10-11 03:10:18 -04:00
->update(['error' => $reason, 'error_phase' => $phase, 'errored_at' => now()]), report: false);
2026-10-05 18:10:18 -04:00
$context = array_filter(['exception' => $exception]);
$exception instanceof Throwable
? Log::error("[extensions] Failed to load \"{$identifier}\": {$reason}", $context)
: Log::warning("[extensions] Skipping \"{$identifier}\": {$reason}", $context);
rescue(fn () => $this->events->dispatch(new ExtensionLoadFailed($identifier, $phase, $reason, $exception)));
}
2026-10-11 03:10:18 -04:00
/**
* Clears the load failures of extensions that have just loaded cleanly. Failures
* at runtime are kept, so they are still there for an administrator to read.
*
* @param list<string> $identifiers
*/
2026-10-05 18:10:18 -04:00
public function clearErrors(array $identifiers): void
{
if ($identifiers === []) {
return;
}
foreach ($identifiers as $identifier) {
if (! isset($this->failedProviders[$identifier])) {
continue;
}
unset($this->failedProviders[$identifier]);
$this->enabled = null;
}
2026-10-11 03:10:18 -04:00
// Runs on every boot, so it only writes when there is a load failure to clear.
rescue(function () use ($identifiers): void {
$loadFailures = Extension::query()
->whereIn('identifier', $identifiers)
->whereNotNull('error')
->where(fn (Builder $query): Builder => $query->whereNull('error_phase')->orWhereIn('error_phase', Extension::LOAD_PHASES));
if ($loadFailures->exists()) {
$loadFailures->update(Extension::NO_ERROR);
}
}, report: false);
2026-10-05 18:10:18 -04:00
}
public function settings(string $identifier): ExtensionSettings
{
// One instance per extension per request so the per-instance row cache
// in ExtensionSettings actually coalesces reads.
return $this->settings[$identifier] ??= new ExtensionSettings($identifier);
}
/**
* Changes whenever the extension's translation files do. The frontend puts it in the
* URL it loads those translations from, which lets the browser cache them for as long
* as core translations and still pick up an installed or upgraded extension at once.
*/
private function translationsRevision(ExtensionManifest $manifest): ?string
{
$directory = $manifest->path('resources', 'lang');
if (! is_dir($directory)) {
return null;
}
$files = array_map(
fn (SplFileInfo $file): string => implode("\0", [$file->getRelativePathname(), $file->getSize(), $file->getMTime()]),
File::allFiles($directory),
);
sort($files);
return hash('xxh128', $manifest->version."\0".implode("\0", $files));
}
2026-10-05 18:10:18 -04:00
/**
* @param array<string, Extension> $records
* @return Collection<string, Extension>
*/
private function recordCollection(array $records): Collection
{
return collect($records);
}
/**
* The ->frontend()-marked settings for one extension, exposed to its bundle
* as ctx.config; with $publicOnly just the ->public() ones, for guests.
* Never allowed to break page rendering.
*
2026-10-11 19:44:06 -04:00
* @return array<string, mixed>
2026-10-05 18:10:18 -04:00
*/
private function frontendConfig(string $identifier, bool $publicOnly): array
{
return rescue(fn (): array => $this->settingsRegistry->get($identifier)?->frontendConfig($publicOnly) ?? [], []);
}
}