2026-10-05 18:10:18 -04:00
<? php
declare ( strict_types = 1 );
namespace Pterodactyl\Services\Extensions ;
use InvalidArgumentException ;
use Pterodactyl\Models\Server ;
use Pterodactyl\Models\User ;
use Pterodactyl\Services\Extensions\Contracts\ManagesExtensionSettings ;
class ExtensionSettingsDefinition implements ManagesExtensionSettings
{
/** @var array<string, ExtensionSettingDefinition> */
private array $definitions ;
/** @param array<int, ExtensionSettingDefinition> $definitions */
public function __construct (
private readonly ExtensionSettings $settings ,
array $definitions ,
) {
$this -> definitions = [];
foreach ( $definitions as $definition ) {
$this -> definitions [ $definition -> key ] = $definition ;
}
}
public function forUser ( User $user ) : self
{
return new self ( $this -> settings -> forUser ( $user ), array_values ( $this -> definitions ));
}
public function forServer ( Server $server ) : self
{
return new self ( $this -> settings -> forServer ( $server ), array_values ( $this -> definitions ));
}
/** The id of the extension these settings belong to. */
public function extension () : string
{
return $this -> settings -> extension ();
}
/**
* The setting's current value. A file setting reads as the public URL of
* its uploaded file, or its default while nothing is uploaded.
*/
public function get ( string $key ) : mixed
{
$definition = $this -> definitions [ $key ] ?? null ;
if ( $definition === null ) {
throw new InvalidArgumentException ( sprintf ( 'Unknown extension setting "%s".' , $key ));
}
if ( $definition -> isFile ()) {
$name = $this -> storedFile ( $definition );
return $definition -> normalize ( $name === null ? $definition -> default : ExtensionSettingFiles :: url ( $this -> extension (), $name ));
}
return $definition -> normalize ( $this -> settings -> get ( $definition -> key , $definition -> default ));
}
2026-10-11 19:44:06 -04:00
/** @return array<string, mixed> */
2026-10-05 18:10:18 -04:00
public function publicSettings () : array
{
$values = [];
foreach ( $this -> definitions as $definition ) {
$values [ $definition -> publicName ()] = $definition -> serializePublic ( $this -> get ( $definition -> key ));
}
return $values ;
}
/**
* Rules for a partial update: every input is optional and, when submitted, must pass
2026-10-06 13:52:09 -04:00
* its definition's rules and those of its field type. File inputs are prohibited:
* files are uploaded and cleared through replaceFile().
2026-10-05 18:10:18 -04:00
*
* @return NormalizedValidationRules
*/
public function validationRules () : array
{
$rules = [];
foreach ( $this -> definitions as $definition ) {
$rules = [ ... $rules , ... $definition -> validationRules ()];
}
return $rules ;
}
2026-10-11 19:44:06 -04:00
/** @param array<string, mixed> $input */
2026-10-05 18:10:18 -04:00
public function update ( array $input ) : void
{
$input = $this -> withoutBlankSecrets ( $input );
$values = [];
$secretKeys = [];
foreach ( $this -> definitions as $definition ) {
if ( $definition -> isFile () || ! array_key_exists ( $definition -> input , $input )) {
continue ;
}
$values [ $definition -> key ] = $definition -> normalize ( $input [ $definition -> input ]);
if ( $definition -> isSecret ()) {
$secretKeys [] = $definition -> key ;
}
}
if ( $secretKeys === []) {
$this -> settings -> setMany ( $values );
return ;
}
$this -> settings -> setManySecrets ( $values , $secretKeys );
}
/**
2026-10-06 13:52:09 -04:00
* Drops secret inputs that would replace the stored value with nothing or with
* its own mask, whatever the field type, so they keep the stored value.
*
2026-10-11 19:44:06 -04:00
* @param array<string, mixed> $input
* @return array<string, mixed>
2026-10-05 18:10:18 -04:00
*/
public function withoutBlankSecrets ( array $input ) : array
{
foreach ( $this -> definitions as $definition ) {
2026-10-06 13:52:09 -04:00
if ( $definition -> isSecret () && in_array ( $input [ $definition -> input ] ?? null , [ '' , null , ExtensionSettingDefinition :: MASK ], true )) {
2026-10-05 18:10:18 -04:00
unset ( $input [ $definition -> input ]);
}
}
return $input ;
}
/**
* Field schema for the auto-rendered admin settings form, in definition
* order, with each entry carrying its current public value.
*
* @return list<ExtensionSettingField>
*/
public function schema () : array
{
return array_values ( array_map (
fn ( ExtensionSettingDefinition $definition ) : array => $definition -> describe ( $this -> get ( $definition -> key )),
$this -> definitions ,
));
}
/** The file setting behind an input name, or null when the input is not a file setting. */
public function fileField ( string $input ) : ? ExtensionSettingDefinition
{
return array_find ( $this -> definitions , fn ( ExtensionSettingDefinition $definition ) : bool => $definition -> input === $input && $definition -> isFile ());
}
/**
* Point a file setting at a newly stored file, or clear it with null.
*
* @return string|null the stored name it pointed at before, which the caller deletes
*/
public function replaceFile ( string $input , ? string $name ) : ? string
{
$definition = $this -> fileField ( $input );
throw_unless ( $definition instanceof ExtensionSettingDefinition , InvalidArgumentException :: class , sprintf ( 'Extension setting "%s" is not a file setting.' , $input ));
throw_if ( $name !== null && ExtensionSettingValueGuard :: fileReference ( $name ) === null , InvalidArgumentException :: class , 'Invalid stored file name.' );
$previous = $this -> storedFile ( $definition );
if ( $name === null ) {
$this -> settings -> forget ( $definition -> key );
} else {
$this -> settings -> set ( $definition -> key , $name );
}
return $previous ;
}
/**
* Values exposed to the extension's frontend bundle as ctx.config: only
* definitions explicitly marked ->frontend(), keyed by public name. These
* are visible to every logged-in user. With $publicOnly, only those also
* marked ->public(), which is what visitors who are not signed in receive.
*
2026-10-11 19:44:06 -04:00
* @return array<string, mixed>
2026-10-05 18:10:18 -04:00
*/
public function frontendConfig ( bool $publicOnly = false ) : array
{
$config = [];
foreach ( $this -> definitions as $definition ) {
if ( ! $definition -> isFrontend () || ( $publicOnly && ! $definition -> isPublic ())) {
continue ;
}
$value = $definition -> serializePublic ( $this -> get ( $definition -> key ));
ExtensionSettingValueGuard :: assertFrontendType ( $value , $definition -> declaredFrontendType ());
$config [ $definition -> publicName ()] = $value ;
}
return $config ;
}
/** @return array<string, 'string'|'number'|'boolean'|'array'|'object'|'null'|'json'> */
public function frontendConfigTypes () : array
{
$types = [];
foreach ( $this -> definitions as $definition ) {
if ( $definition -> isFrontend ()) {
$types [ $definition -> publicName ()] = $definition -> declaredFrontendType ();
}
}
return $types ;
}
/** @return list<string> public names of the frontend settings that guests receive too */
public function publicConfigKeys () : array
{
return array_values ( array_map (
fn ( ExtensionSettingDefinition $definition ) : string => $definition -> publicName (),
array_filter ( $this -> definitions , fn ( ExtensionSettingDefinition $definition ) : bool => $definition -> isPublic ()),
));
}
private function storedFile ( ExtensionSettingDefinition $definition ) : ? string
{
return ExtensionSettingValueGuard :: fileReference ( $this -> settings -> get ( $definition -> key ));
}
}