Path: blob/1.0-develop/app/Transformers/Api/Application/ServerDatabaseTransformer.php
10283 views
<?php12namespace Pterodactyl\Transformers\Api\Application;34use Pterodactyl\Models\Database;5use League\Fractal\Resource\Item;6use Pterodactyl\Models\DatabaseHost;7use League\Fractal\Resource\NullResource;8use Pterodactyl\Services\Acl\Api\AdminAcl;9use Illuminate\Contracts\Encryption\Encrypter;1011class ServerDatabaseTransformer extends BaseTransformer12{13protected array $availableIncludes = ['password', 'host'];1415private Encrypter $encrypter;1617/**18* Perform dependency injection.19*/20public function handle(Encrypter $encrypter)21{22$this->encrypter = $encrypter;23}2425/**26* Return the resource name for the JSONAPI output.27*/28public function getResourceName(): string29{30return Database::RESOURCE_NAME;31}3233/**34* Transform a database model in a representation for the application API.35*/36public function transform(Database $model): array37{38return [39'id' => $model->id,40'server' => $model->server_id,41'host' => $model->database_host_id,42'database' => $model->database,43'username' => $model->username,44'remote' => $model->remote,45'max_connections' => $model->max_connections,46'created_at' => $model->created_at->toAtomString(),47'updated_at' => $model->updated_at->toAtomString(),48];49}5051/**52* Include the database password in the request.53*/54public function includePassword(Database $model): Item55{56return $this->item($model, function (Database $model) {57return [58'password' => $this->encrypter->decrypt($model->password),59];60}, 'database_password');61}6263/**64* Return the database host relationship for this server database.65*66* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException67*/68public function includeHost(Database $model): Item|NullResource69{70if (!$this->authorize(AdminAcl::RESOURCE_DATABASE_HOSTS)) {71return $this->null();72}7374$model->loadMissing('host');7576return $this->item(77$model->getRelation('host'),78$this->makeTransformer(DatabaseHostTransformer::class),79DatabaseHost::RESOURCE_NAME80);81}82}838485