diff --git a/app/Exceptions/Transformer/InvalidTransformerLevelException.php b/app/Exceptions/Transformer/InvalidTransformerLevelException.php deleted file mode 100644 index 3d4c24248..000000000 --- a/app/Exceptions/Transformer/InvalidTransformerLevelException.php +++ /dev/null @@ -1,9 +0,0 @@ -make($abstract); - - Assert::isInstanceOf($transformer, BaseTransformer::class); - - return $transformer; + return new $abstract; } /** diff --git a/app/Transformers/Api/Application/AdminRoleTransformer.php b/app/Transformers/Api/Application/AdminRoleTransformer.php index 2c7b434c3..1aca9aebc 100644 --- a/app/Transformers/Api/Application/AdminRoleTransformer.php +++ b/app/Transformers/Api/Application/AdminRoleTransformer.php @@ -3,25 +3,15 @@ namespace Pterodactyl\Transformers\Api\Application; use Pterodactyl\Models\AdminRole; +use Pterodactyl\Transformers\Api\Transformer; -class AdminRoleTransformer extends BaseTransformer +class AdminRoleTransformer extends Transformer { - /** - * Return the resource name for the JSONAPI output. - * - * @return string - */ public function getResourceName(): string { return AdminRole::RESOURCE_NAME; } - /** - * Return a transformed AdminRole model that can be consumed by external services. - * - * @param \Pterodactyl\Models\AdminRole $model - * @return array - */ public function transform(AdminRole $model): array { return [ diff --git a/app/Transformers/Api/Application/AllocationTransformer.php b/app/Transformers/Api/Application/AllocationTransformer.php index 82621c42f..ea63013cd 100644 --- a/app/Transformers/Api/Application/AllocationTransformer.php +++ b/app/Transformers/Api/Application/AllocationTransformer.php @@ -2,12 +2,11 @@ namespace Pterodactyl\Transformers\Api\Application; -use Pterodactyl\Models\Node; -use Pterodactyl\Models\Server; use Pterodactyl\Models\Allocation; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; -class AllocationTransformer extends BaseTransformer +class AllocationTransformer extends Transformer { /** * Relationships that can be loaded onto allocation transformations. @@ -16,24 +15,12 @@ class AllocationTransformer extends BaseTransformer */ protected $availableIncludes = ['node', 'server']; - /** - * Return the resource name for the JSONAPI output. - * - * @return string - */ public function getResourceName(): string { return Allocation::RESOURCE_NAME; } - /** - * Return a generic transformed allocation array. - * - * @param \Pterodactyl\Models\Allocation $model - * - * @return array - */ - public function transform(Allocation $model) + public function transform(Allocation $model): array { return [ 'id' => $model->id, @@ -41,7 +28,7 @@ class AllocationTransformer extends BaseTransformer 'alias' => $model->ip_alias, 'port' => $model->port, 'notes' => $model->notes, - 'assigned' => ! is_null($model->server_id), + 'assigned' => !is_null($model->server_id), ]; } @@ -51,18 +38,14 @@ class AllocationTransformer extends BaseTransformer * @param \Pterodactyl\Models\Allocation $allocation * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException - * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function includeNode(Allocation $allocation) { - if (! $this->authorize(AdminAcl::RESOURCE_NODES)) { + if (!$this->authorize(AdminAcl::RESOURCE_NODES)) { return $this->null(); } - return $this->item( - $allocation->node, $this->makeTransformer(NodeTransformer::class), Node::RESOURCE_NAME - ); + return $this->item($allocation->node, new NodeTransformer()); } /** @@ -71,17 +54,13 @@ class AllocationTransformer extends BaseTransformer * @param \Pterodactyl\Models\Allocation $allocation * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException - * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function includeServer(Allocation $allocation) { - if (! $this->authorize(AdminAcl::RESOURCE_SERVERS) || ! $allocation->server) { + if (!$this->authorize(AdminAcl::RESOURCE_SERVERS) || !$allocation->server) { return $this->null(); } - return $this->item( - $allocation->server, $this->makeTransformer(ServerTransformer::class), Server::RESOURCE_NAME - ); + return $this->item($allocation->server, new ServerTransformer()); } } diff --git a/app/Transformers/Api/Application/BaseTransformer.php b/app/Transformers/Api/Application/BaseTransformer.php deleted file mode 100644 index 453b32a22..000000000 --- a/app/Transformers/Api/Application/BaseTransformer.php +++ /dev/null @@ -1,31 +0,0 @@ -makeWith($abstract, $parameters); - - if (!$transformer instanceof self) { - throw new InvalidTransformerLevelException('Calls to ' . __METHOD__ . ' must return a transformer that is an instance of ' . __CLASS__); - } - - return $transformer; - } -} diff --git a/app/Transformers/Api/Application/DatabaseHostTransformer.php b/app/Transformers/Api/Application/DatabaseHostTransformer.php index 4c9e86d44..dd45c3614 100644 --- a/app/Transformers/Api/Application/DatabaseHostTransformer.php +++ b/app/Transformers/Api/Application/DatabaseHostTransformer.php @@ -2,11 +2,11 @@ namespace Pterodactyl\Transformers\Api\Application; -use Pterodactyl\Models\Database; use Pterodactyl\Models\DatabaseHost; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; -class DatabaseHostTransformer extends BaseTransformer +class DatabaseHostTransformer extends Transformer { /** * @var array @@ -15,23 +15,12 @@ class DatabaseHostTransformer extends BaseTransformer 'databases', ]; - /** - * Return the resource name for the JSONAPI output. - * - * @return string - */ public function getResourceName(): string { return DatabaseHost::RESOURCE_NAME; } - /** - * Transform database host into a representation for the application API. - * - * @param \Pterodactyl\Models\DatabaseHost $model - * @return array - */ - public function transform(DatabaseHost $model) + public function transform(DatabaseHost $model): array { return [ 'id' => $model->id, @@ -40,8 +29,8 @@ class DatabaseHostTransformer extends BaseTransformer 'port' => $model->port, 'username' => $model->username, 'node' => $model->node_id, - 'created_at' => $model->created_at->toIso8601String(), - 'updated_at' => $model->updated_at->toIso8601String(), + 'created_at' => self::formatTimestamp($model->created_at), + 'updated_at' => self::formatTimestamp($model->updated_at), ]; } @@ -49,9 +38,6 @@ class DatabaseHostTransformer extends BaseTransformer * Include the databases associated with this host. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Illuminate\Contracts\Container\BindingResolutionException - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeDatabases(DatabaseHost $model) { @@ -59,8 +45,6 @@ class DatabaseHostTransformer extends BaseTransformer return $this->null(); } - $model->loadMissing('databases'); - - return $this->collection($model->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), Database::RESOURCE_NAME); + return $this->collection($model->databases, new ServerDatabaseTransformer()); } } diff --git a/app/Transformers/Api/Application/EggTransformer.php b/app/Transformers/Api/Application/EggTransformer.php index a1348a0ad..b6ebbc97d 100644 --- a/app/Transformers/Api/Application/EggTransformer.php +++ b/app/Transformers/Api/Application/EggTransformer.php @@ -3,12 +3,10 @@ namespace Pterodactyl\Transformers\Api\Application; use Pterodactyl\Models\Egg; -use Pterodactyl\Models\Nest; -use Pterodactyl\Models\Server; -use Pterodactyl\Models\EggVariable; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; -class EggTransformer extends BaseTransformer +class EggTransformer extends Transformer { /** * Relationships that can be loaded onto this transformation. @@ -23,21 +21,12 @@ class EggTransformer extends BaseTransformer 'variables', ]; - /** - * Return the resource name for the JSONAPI output. - */ public function getResourceName(): string { return Egg::RESOURCE_NAME; } - /** - * Transform an Egg model into a representation that can be consumed by - * the application api. - * - * @return array - */ - public function transform(Egg $model) + public function transform(Egg $model): array { return [ 'id' => $model->id, @@ -67,8 +56,8 @@ class EggTransformer extends BaseTransformer 'container' => $model->script_container, 'extends' => $model->copy_script_from, ], - $model->getCreatedAtColumn() => $this->formatTimestamp($model->created_at), - $model->getUpdatedAtColumn() => $this->formatTimestamp($model->updated_at), + 'created_at' => self::formatTimestamp($model->created_at), + 'updated_at' => self::formatTimestamp($model->updated_at), ]; } @@ -76,8 +65,6 @@ class EggTransformer extends BaseTransformer * Include the Nest relationship for the given Egg in the transformation. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeNest(Egg $model) { @@ -85,17 +72,13 @@ class EggTransformer extends BaseTransformer return $this->null(); } - $model->loadMissing('nest'); - - return $this->item($model->getRelation('nest'), $this->makeTransformer(NestTransformer::class), Nest::RESOURCE_NAME); + return $this->item($model->nest, new NestTransformer()); } /** * Include the Servers relationship for the given Egg in the transformation. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeServers(Egg $model) { @@ -103,9 +86,7 @@ class EggTransformer extends BaseTransformer return $this->null(); } - $model->loadMissing('servers'); - - return $this->collection($model->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), Server::RESOURCE_NAME); + return $this->collection($model->servers, new ServerTransformer()); } /** @@ -120,8 +101,6 @@ class EggTransformer extends BaseTransformer return $this->null(); } - $model->loadMissing('configFrom'); - return $this->item($model, function (Egg $model) { return [ 'files' => json_decode($model->inherit_config_files), @@ -144,8 +123,6 @@ class EggTransformer extends BaseTransformer return $this->null(); } - $model->loadMissing('scriptFrom'); - return $this->item($model, function (Egg $model) { return [ 'privileged' => $model->script_is_privileged, @@ -160,8 +137,6 @@ class EggTransformer extends BaseTransformer * Include the variables that are defined for this Egg. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeVariables(Egg $model) { @@ -169,12 +144,6 @@ class EggTransformer extends BaseTransformer return $this->null(); } - $model->loadMissing('variables'); - - return $this->collection( - $model->getRelation('variables'), - $this->makeTransformer(EggVariableTransformer::class), - EggVariable::RESOURCE_NAME - ); + return $this->collection($model->variables, new EggVariableTransformer()); } } diff --git a/app/Transformers/Api/Application/EggVariableTransformer.php b/app/Transformers/Api/Application/EggVariableTransformer.php index 2088806d5..d255a633e 100644 --- a/app/Transformers/Api/Application/EggVariableTransformer.php +++ b/app/Transformers/Api/Application/EggVariableTransformer.php @@ -4,18 +4,16 @@ namespace Pterodactyl\Transformers\Api\Application; use Pterodactyl\Models\Egg; use Pterodactyl\Models\EggVariable; +use Pterodactyl\Transformers\Api\Transformer; -class EggVariableTransformer extends BaseTransformer +class EggVariableTransformer extends Transformer { - /** - * Return the resource name for the JSONAPI output. - */ public function getResourceName(): string { return Egg::RESOURCE_NAME; } - public function transform(EggVariable $model) + public function transform(EggVariable $model): array { return $model->toArray(); } diff --git a/app/Transformers/Api/Application/LocationTransformer.php b/app/Transformers/Api/Application/LocationTransformer.php index eceb0e9e7..dd65b33c3 100644 --- a/app/Transformers/Api/Application/LocationTransformer.php +++ b/app/Transformers/Api/Application/LocationTransformer.php @@ -4,8 +4,9 @@ namespace Pterodactyl\Transformers\Api\Application; use Pterodactyl\Models\Location; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; -class LocationTransformer extends BaseTransformer +class LocationTransformer extends Transformer { /** * List of resources that can be included. @@ -14,17 +15,11 @@ class LocationTransformer extends BaseTransformer */ protected $availableIncludes = ['nodes', 'servers']; - /** - * Return the resource name for the JSONAPI output. - */ public function getResourceName(): string { return Location::RESOURCE_NAME; } - /** - * Return a generic transformed location array. - */ public function transform(Location $model): array { return [ @@ -40,9 +35,6 @@ class LocationTransformer extends BaseTransformer * Return the servers associated with this location. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Illuminate\Contracts\Container\BindingResolutionException - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeServers(Location $location) { @@ -50,18 +42,13 @@ class LocationTransformer extends BaseTransformer return $this->null(); } - $location->loadMissing('servers'); - - return $this->collection($location->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server'); + return $this->collection($location->servers, new ServerTransformer()); } /** * Return the nodes associated with this location. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Illuminate\Contracts\Container\BindingResolutionException - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeNodes(Location $location) { @@ -69,8 +56,6 @@ class LocationTransformer extends BaseTransformer return $this->null(); } - $location->loadMissing('nodes'); - - return $this->collection($location->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), 'node'); + return $this->collection($location->nodes, new NodeTransformer()); } } diff --git a/app/Transformers/Api/Application/MountTransformer.php b/app/Transformers/Api/Application/MountTransformer.php index d192edf49..08a270913 100644 --- a/app/Transformers/Api/Application/MountTransformer.php +++ b/app/Transformers/Api/Application/MountTransformer.php @@ -2,13 +2,11 @@ namespace Pterodactyl\Transformers\Api\Application; -use Pterodactyl\Models\Egg; -use Pterodactyl\Models\Node; use Pterodactyl\Models\Mount; -use Pterodactyl\Models\Server; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; -class MountTransformer extends BaseTransformer +class MountTransformer extends Transformer { /** * List of resources that can be included. @@ -17,22 +15,11 @@ class MountTransformer extends BaseTransformer */ protected $availableIncludes = ['eggs', 'nodes', 'servers']; - /** - * Return the resource name for the JSONAPI output. - * - * @return string - */ public function getResourceName(): string { return Mount::RESOURCE_NAME; } - /** - * Return a transformed Mount model that can be consumed by external services. - * - * @param \Pterodactyl\Models\Mount $model - * @return array - */ public function transform(Mount $model): array { return [ @@ -50,72 +37,42 @@ class MountTransformer extends BaseTransformer /** * Return the eggs associated with this mount. * - * @param \Pterodactyl\Models\Mount $mount - * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException - * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function includeEggs(Mount $mount) { - if (! $this->authorize(AdminAcl::RESOURCE_EGGS)) { + if (!$this->authorize(AdminAcl::RESOURCE_EGGS)) { return $this->null(); } - $mount->loadMissing('eggs'); - - return $this->collection( - $mount->getRelation('eggs'), - $this->makeTransformer(EggTransformer::class), - Egg::RESOURCE_NAME - ); + return $this->collection($mount->eggs, new EggTransformer()); } /** * Return the nodes associated with this mount. * - * @param \Pterodactyl\Models\Mount $mount - * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException - * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function includeNodes(Mount $mount) { - if (! $this->authorize(AdminAcl::RESOURCE_NODES)) { + if (!$this->authorize(AdminAcl::RESOURCE_NODES)) { return $this->null(); } - $mount->loadMissing('nodes'); - - return $this->collection( - $mount->getRelation('nodes'), - $this->makeTransformer(NodeTransformer::class), - Node::RESOURCE_NAME - ); + return $this->collection($mount->nodes, new NodeTransformer()); } /** * Return the servers associated with this mount. * - * @param \Pterodactyl\Models\Mount $mount - * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException - * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function includeServers(Mount $mount) { - if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) { + if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) { return $this->null(); } - $mount->loadMissing('servers'); - - return $this->collection( - $mount->getRelation('servers'), - $this->makeTransformer(ServerTransformer::class), - Server::RESOURCE_NAME - ); + return $this->collection($mount->servers, new ServerTransformer()); } } diff --git a/app/Transformers/Api/Application/NestTransformer.php b/app/Transformers/Api/Application/NestTransformer.php index 506b0276b..160e4474d 100644 --- a/app/Transformers/Api/Application/NestTransformer.php +++ b/app/Transformers/Api/Application/NestTransformer.php @@ -2,12 +2,11 @@ namespace Pterodactyl\Transformers\Api\Application; -use Pterodactyl\Models\Egg; use Pterodactyl\Models\Nest; -use Pterodactyl\Models\Server; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; -class NestTransformer extends BaseTransformer +class NestTransformer extends Transformer { /** * Relationships that can be loaded onto this transformation. @@ -15,29 +14,21 @@ class NestTransformer extends BaseTransformer * @var array */ protected $availableIncludes = [ - 'eggs', 'servers', + 'eggs', + 'servers', ]; - /** - * Return the resource name for the JSONAPI output. - */ public function getResourceName(): string { return Nest::RESOURCE_NAME; } - /** - * Transform a Nest model into a representation that can be consumed by the - * application API. - * - * @return array - */ - public function transform(Nest $model) + public function transform(Nest $model): array { $response = $model->toArray(); - $response[$model->getUpdatedAtColumn()] = $this->formatTimestamp($model->updated_at); - $response[$model->getCreatedAtColumn()] = $this->formatTimestamp($model->created_at); + $response[$model->getUpdatedAtColumn()] = self::formatTimestamp($model->updated_at); + $response[$model->getCreatedAtColumn()] = self::formatTimestamp($model->created_at); return $response; } @@ -46,8 +37,6 @@ class NestTransformer extends BaseTransformer * Include the Eggs relationship on the given Nest model transformation. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeEggs(Nest $model) { @@ -55,17 +44,13 @@ class NestTransformer extends BaseTransformer return $this->null(); } - $model->loadMissing('eggs'); - - return $this->collection($model->getRelation('eggs'), $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME); + return $this->collection($model->eggs, new EggTransformer()); } /** * Include the servers relationship on the given Nest model. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeServers(Nest $model) { @@ -73,8 +58,6 @@ class NestTransformer extends BaseTransformer return $this->null(); } - $model->loadMissing('servers'); - - return $this->collection($model->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), Server::RESOURCE_NAME); + return $this->collection($model->servers, new ServerTransformer()); } } diff --git a/app/Transformers/Api/Application/NodeTransformer.php b/app/Transformers/Api/Application/NodeTransformer.php index d7de33ce6..6e6dcc7d9 100644 --- a/app/Transformers/Api/Application/NodeTransformer.php +++ b/app/Transformers/Api/Application/NodeTransformer.php @@ -3,10 +3,10 @@ namespace Pterodactyl\Transformers\Api\Application; use Pterodactyl\Models\Node; -use League\Fractal\Resource\NullResource; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; -class NodeTransformer extends BaseTransformer +class NodeTransformer extends Transformer { /** * List of resources that can be included. @@ -15,18 +15,11 @@ class NodeTransformer extends BaseTransformer */ protected $availableIncludes = ['allocations', 'database_host', 'location', 'mounts', 'servers']; - /** - * Return the resource name for the JSONAPI output. - */ public function getResourceName(): string { return Node::RESOURCE_NAME; } - /** - * Return a node transformed into a format that can be consumed by the - * external administrative API. - */ public function transform(Node $model): array { $response = $model->toArray(); @@ -48,9 +41,6 @@ class NodeTransformer extends BaseTransformer * Return the allocations associated with this node. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Illuminate\Contracts\Container\BindingResolutionException - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeAllocations(Node $node) { @@ -58,50 +48,27 @@ class NodeTransformer extends BaseTransformer return $this->null(); } - $node->loadMissing('allocations'); - - return $this->collection( - $node->getRelation('allocations'), - $this->makeTransformer(AllocationTransformer::class), - 'allocation' - ); + return $this->collection($node->allocations, new AllocationTransformer()); } /** * Return the database host associated with this node. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Illuminate\Contracts\Container\BindingResolutionException - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeDatabaseHost(Node $node) { - if (!$this->authorize(AdminAcl::RESOURCE_DATABASE_HOSTS)) { + if (!$this->authorize(AdminAcl::RESOURCE_DATABASE_HOSTS) || is_null($node->databaseHost)) { return $this->null(); } - $node->loadMissing('databaseHost'); - - $databaseHost = $node->getRelation('databaseHost'); - if (is_null($databaseHost)) { - return new NullResource(); - } - - return $this->item( - $node->getRelation('databaseHost'), - $this->makeTransformer(DatabaseHostTransformer::class), - 'databaseHost' - ); + return $this->item($node->databaseHost, new DatabaseHostTransformer()); } /** * Return the location associated with this node. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Illuminate\Contracts\Container\BindingResolutionException - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeLocation(Node $node) { @@ -109,22 +76,13 @@ class NodeTransformer extends BaseTransformer return $this->null(); } - $node->loadMissing('location'); - - return $this->item( - $node->getRelation('location'), - $this->makeTransformer(LocationTransformer::class), - 'location' - ); + return $this->item($node->location, new LocationTransformer()); } /** * Return the mounts associated with this node. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Illuminate\Contracts\Container\BindingResolutionException - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeMounts(Node $node) { @@ -132,22 +90,13 @@ class NodeTransformer extends BaseTransformer return $this->null(); } - $node->loadMissing('mounts'); - - return $this->collection( - $node->getRelation('mounts'), - $this->makeTransformer(MountTransformer::class), - 'mount' - ); + return $this->collection($node->mounts, new MountTransformer()); } /** * Return the servers associated with this node. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Illuminate\Contracts\Container\BindingResolutionException - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeServers(Node $node) { @@ -155,12 +104,6 @@ class NodeTransformer extends BaseTransformer return $this->null(); } - $node->loadMissing('servers'); - - return $this->collection( - $node->getRelation('servers'), - $this->makeTransformer(ServerTransformer::class), - 'server' - ); + return $this->collection($node->servers, new ServerTransformer()); } } diff --git a/app/Transformers/Api/Application/ServerDatabaseTransformer.php b/app/Transformers/Api/Application/ServerDatabaseTransformer.php index 95180e2d9..392b33bf9 100644 --- a/app/Transformers/Api/Application/ServerDatabaseTransformer.php +++ b/app/Transformers/Api/Application/ServerDatabaseTransformer.php @@ -5,39 +5,28 @@ namespace Pterodactyl\Transformers\Api\Application; use Pterodactyl\Models\Database; use Pterodactyl\Models\DatabaseHost; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; use Illuminate\Contracts\Encryption\Encrypter; -class ServerDatabaseTransformer extends BaseTransformer +class ServerDatabaseTransformer extends Transformer { /** * @var array */ protected $availableIncludes = ['password', 'host']; - /** - * @var Encrypter - */ - private $encrypter; + protected Encrypter $encrypter; - /** - * Perform dependency injection. - */ public function handle(Encrypter $encrypter) { $this->encrypter = $encrypter; } - /** - * Return the resource name for the JSONAPI output. - */ public function getResourceName(): string { return Database::RESOURCE_NAME; } - /** - * Transform a database model in a representation for the application API. - */ public function transform(Database $model): array { return [ @@ -48,8 +37,8 @@ class ServerDatabaseTransformer extends BaseTransformer 'username' => $model->username, 'remote' => $model->remote, 'max_connections' => $model->max_connections, - 'created_at' => $model->created_at->toIso8601String(), - 'updated_at' => $model->updated_at->toIso8601String(), + 'created_at' => self::formatTimestamp($model->created_at), + 'updated_at' => self::formatTimestamp($model->updated_at), ]; } @@ -71,8 +60,6 @@ class ServerDatabaseTransformer extends BaseTransformer * Return the database host relationship for this server database. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeHost(Database $model) { @@ -80,12 +67,6 @@ class ServerDatabaseTransformer extends BaseTransformer return $this->null(); } - $model->loadMissing('host'); - - return $this->item( - $model->getRelation('host'), - $this->makeTransformer(DatabaseHostTransformer::class), - DatabaseHost::RESOURCE_NAME - ); + return $this->item($model->host, new DatabaseHostTransformer()); } } diff --git a/app/Transformers/Api/Application/ServerTransformer.php b/app/Transformers/Api/Application/ServerTransformer.php index 47b1e6409..fb2c7ceda 100644 --- a/app/Transformers/Api/Application/ServerTransformer.php +++ b/app/Transformers/Api/Application/ServerTransformer.php @@ -4,14 +4,12 @@ namespace Pterodactyl\Transformers\Api\Application; use Pterodactyl\Models\Server; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; use Pterodactyl\Services\Servers\EnvironmentService; -class ServerTransformer extends BaseTransformer +class ServerTransformer extends Transformer { - /** - * @var \Pterodactyl\Services\Servers\EnvironmentService - */ - private $environmentService; + protected EnvironmentService $environmentService; /** * List of resources that can be included. @@ -31,25 +29,16 @@ class ServerTransformer extends BaseTransformer 'transfer', ]; - /** - * Perform dependency injection. - */ public function handle(EnvironmentService $environmentService) { $this->environmentService = $environmentService; } - /** - * Return the resource name for the JSONAPI output. - */ public function getResourceName(): string { return Server::RESOURCE_NAME; } - /** - * Return a generic transformed server array. - */ public function transform(Server $model): array { return [ @@ -83,8 +72,8 @@ class ServerTransformer extends BaseTransformer 'image' => $model->image, 'environment' => $this->environmentService->handle($model), ], - $model->getUpdatedAtColumn() => $this->formatTimestamp($model->updated_at), - $model->getCreatedAtColumn() => $this->formatTimestamp($model->created_at), + 'updated_at' => self::formatTimestamp($model->updated_at), + 'created_at' => self::formatTimestamp($model->created_at), ]; } @@ -92,8 +81,6 @@ class ServerTransformer extends BaseTransformer * Return a generic array of allocations for this server. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeAllocations(Server $server) { @@ -101,17 +88,13 @@ class ServerTransformer extends BaseTransformer return $this->null(); } - $server->loadMissing('allocations'); - - return $this->collection($server->getRelation('allocations'), $this->makeTransformer(AllocationTransformer::class), 'allocation'); + return $this->collection($server->allocations, new AllocationTransformer()); } /** * Return a generic array of data about subusers for this server. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeSubusers(Server $server) { @@ -119,17 +102,13 @@ class ServerTransformer extends BaseTransformer return $this->null(); } - $server->loadMissing('subusers'); - - return $this->collection($server->getRelation('subusers'), $this->makeTransformer(SubuserTransformer::class), 'subuser'); + return $this->collection($server->subusers, new SubuserTransformer()); } /** * Return a generic array of data about subusers for this server. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeUser(Server $server) { @@ -137,17 +116,13 @@ class ServerTransformer extends BaseTransformer return $this->null(); } - $server->loadMissing('user'); - - return $this->item($server->getRelation('user'), $this->makeTransformer(UserTransformer::class), 'user'); + return $this->item($server->user, new UserTransformer()); } /** * Return a generic array with nest information for this server. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeNest(Server $server) { @@ -155,17 +130,13 @@ class ServerTransformer extends BaseTransformer return $this->null(); } - $server->loadMissing('nest'); - - return $this->item($server->getRelation('nest'), $this->makeTransformer(NestTransformer::class), 'nest'); + return $this->item($server->nest, new NestTransformer()); } /** * Return a generic array with egg information for this server. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeEgg(Server $server) { @@ -173,17 +144,13 @@ class ServerTransformer extends BaseTransformer return $this->null(); } - $server->loadMissing('egg'); - - return $this->item($server->getRelation('egg'), $this->makeTransformer(EggTransformer::class), 'egg'); + return $this->item($server->egg, new EggTransformer()); } /** * Return a generic array of data about subusers for this server. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeVariables(Server $server) { @@ -191,17 +158,13 @@ class ServerTransformer extends BaseTransformer return $this->null(); } - $server->loadMissing('variables'); - - return $this->collection($server->getRelation('variables'), $this->makeTransformer(ServerVariableTransformer::class), 'server_variable'); + return $this->collection($server->variables, new ServerVariableTransformer()); } /** * Return a generic array with location information for this server. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeLocation(Server $server) { @@ -209,17 +172,13 @@ class ServerTransformer extends BaseTransformer return $this->null(); } - $server->loadMissing('location'); - - return $this->item($server->getRelation('location'), $this->makeTransformer(LocationTransformer::class), 'location'); + return $this->item($server->location, new LocationTransformer()); } /** * Return a generic array with node information for this server. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeNode(Server $server) { @@ -227,17 +186,13 @@ class ServerTransformer extends BaseTransformer return $this->null(); } - $server->loadMissing('node'); - - return $this->item($server->getRelation('node'), $this->makeTransformer(NodeTransformer::class), 'node'); + return $this->item($server->node, new NodeTransformer()); } /** * Return a generic array with database information for this server. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeDatabases(Server $server) { @@ -245,8 +200,6 @@ class ServerTransformer extends BaseTransformer return $this->null(); } - $server->loadMissing('databases'); - - return $this->collection($server->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), 'databases'); + return $this->collection($server->databases, new ServerDatabaseTransformer()); } } diff --git a/app/Transformers/Api/Application/ServerVariableTransformer.php b/app/Transformers/Api/Application/ServerVariableTransformer.php index aefa318e2..af06b1e97 100644 --- a/app/Transformers/Api/Application/ServerVariableTransformer.php +++ b/app/Transformers/Api/Application/ServerVariableTransformer.php @@ -3,9 +3,11 @@ namespace Pterodactyl\Transformers\Api\Application; use Pterodactyl\Models\EggVariable; +use Pterodactyl\Models\ServerVariable; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; -class ServerVariableTransformer extends BaseTransformer +class ServerVariableTransformer extends Transformer { /** * List of resources that can be included. @@ -22,12 +24,7 @@ class ServerVariableTransformer extends BaseTransformer return ServerVariable::RESOURCE_NAME; } - /** - * Return a generic transformed server variable array. - * - * @return array - */ - public function transform(EggVariable $variable) + public function transform(EggVariable $variable): array { return $variable->toArray(); } @@ -36,8 +33,6 @@ class ServerVariableTransformer extends BaseTransformer * Return the parent service variable data. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeParent(EggVariable $variable) { @@ -45,8 +40,6 @@ class ServerVariableTransformer extends BaseTransformer return $this->null(); } - $variable->loadMissing('variable'); - - return $this->item($variable->getRelation('variable'), $this->makeTransformer(EggVariableTransformer::class), 'variable'); + return $this->item($variable->variable, new EggVariableTransformer()); } } diff --git a/app/Transformers/Api/Application/SubuserTransformer.php b/app/Transformers/Api/Application/SubuserTransformer.php index 08c622666..41c725d1f 100644 --- a/app/Transformers/Api/Application/SubuserTransformer.php +++ b/app/Transformers/Api/Application/SubuserTransformer.php @@ -4,8 +4,9 @@ namespace Pterodactyl\Transformers\Api\Application; use Pterodactyl\Models\Subuser; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; -class SubuserTransformer extends BaseTransformer +class SubuserTransformer extends Transformer { /** * List of resources that can be included. @@ -14,17 +15,11 @@ class SubuserTransformer extends BaseTransformer */ protected $availableIncludes = ['user', 'server']; - /** - * Return the resource name for the JSONAPI output. - */ public function getResourceName(): string { return Subuser::RESOURCE_NAME; } - /** - * Return a transformed Subuser model that can be consumed by external services. - */ public function transform(Subuser $subuser): array { return [ @@ -32,8 +27,8 @@ class SubuserTransformer extends BaseTransformer 'user_id' => $subuser->user_id, 'server_id' => $subuser->server_id, 'permissions' => $subuser->permissions, - 'created_at' => $this->formatTimestamp($subuser->created_at), - 'updated_at' => $this->formatTimestamp($subuser->updated_at), + 'created_at' => self::formatTimestamp($subuser->created_at), + 'updated_at' => self::formatTimestamp($subuser->updated_at), ]; } @@ -41,8 +36,6 @@ class SubuserTransformer extends BaseTransformer * Return a generic item of user for this subuser. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeUser(Subuser $subuser) { @@ -50,17 +43,13 @@ class SubuserTransformer extends BaseTransformer return $this->null(); } - $subuser->loadMissing('user'); - - return $this->item($subuser->getRelation('user'), $this->makeTransformer(UserTransformer::class), 'user'); + return $this->item($subuser->user, new UserTransformer()); } /** * Return a generic item of server for this subuser. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeServer(Subuser $subuser) { @@ -68,8 +57,6 @@ class SubuserTransformer extends BaseTransformer return $this->null(); } - $subuser->loadMissing('server'); - - return $this->item($subuser->getRelation('server'), $this->makeTransformer(ServerTransformer::class), 'server'); + return $this->item($subuser->server, new ServerTransformer()); } } diff --git a/app/Transformers/Api/Application/UserTransformer.php b/app/Transformers/Api/Application/UserTransformer.php index f79aad102..d0439e654 100644 --- a/app/Transformers/Api/Application/UserTransformer.php +++ b/app/Transformers/Api/Application/UserTransformer.php @@ -4,8 +4,9 @@ namespace Pterodactyl\Transformers\Api\Application; use Pterodactyl\Models\User; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; -class UserTransformer extends BaseTransformer +class UserTransformer extends Transformer { /** * List of resources that can be included. @@ -14,17 +15,11 @@ class UserTransformer extends BaseTransformer */ protected $availableIncludes = ['role', 'servers']; - /** - * Return the resource name for the JSONAPI output. - */ public function getResourceName(): string { return User::RESOURCE_NAME; } - /** - * Return a transformed User model that can be consumed by external services. - */ public function transform(User $model): array { return [ @@ -39,8 +34,8 @@ class UserTransformer extends BaseTransformer 'avatar_url' => $model->avatarURL(), 'admin_role_id' => $model->admin_role_id, 'role_name' => $model->adminRoleName(), - 'created_at' => $this->formatTimestamp($model->created_at), - 'updated_at' => $this->formatTimestamp($model->updated_at), + 'created_at' => self::formatTimestamp($model->created_at), + 'updated_at' => self::formatTimestamp($model->updated_at), ]; } @@ -48,9 +43,6 @@ class UserTransformer extends BaseTransformer * Return the role associated with this user. * * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource - * - * @throws \Illuminate\Contracts\Container\BindingResolutionException - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeRole(User $user) { @@ -58,18 +50,13 @@ class UserTransformer extends BaseTransformer return $this->null(); } - $user->loadMissing('adminRole'); - - return $this->item($user->getRelation('adminRole'), $this->makeTransformer(AdminRoleTransformer::class), 'admin_role'); + return $this->item($user->adminRole, new AdminRoleTransformer()); } /** * Return the servers associated with this user. * * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource - * - * @throws \Illuminate\Contracts\Container\BindingResolutionException - * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException */ public function includeServers(User $user) { @@ -77,8 +64,6 @@ class UserTransformer extends BaseTransformer return $this->null(); } - $user->loadMissing('servers'); - - return $this->collection($user->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server'); + return $this->collection($user->servers, new ServerTransformer()); } } diff --git a/tests/Integration/Api/Application/ApplicationApiIntegrationTestCase.php b/tests/Integration/Api/Application/ApplicationApiIntegrationTestCase.php index 8223f95c0..e5a995123 100644 --- a/tests/Integration/Api/Application/ApplicationApiIntegrationTestCase.php +++ b/tests/Integration/Api/Application/ApplicationApiIntegrationTestCase.php @@ -3,13 +3,12 @@ namespace Pterodactyl\Tests\Integration\Api\Application; use Pterodactyl\Models\User; -use PHPUnit\Framework\Assert; use Pterodactyl\Models\ApiKey; use Pterodactyl\Services\Acl\Api\AdminAcl; +use Pterodactyl\Transformers\Api\Transformer; use Pterodactyl\Tests\Integration\IntegrationTestCase; use Illuminate\Foundation\Testing\DatabaseTransactions; use Pterodactyl\Tests\Traits\Integration\CreatesTestModels; -use Pterodactyl\Transformers\Api\Application\BaseTransformer; use Pterodactyl\Tests\Traits\Http\IntegrationJsonRequestAssertions; abstract class ApplicationApiIntegrationTestCase extends IntegrationTestCase @@ -129,16 +128,10 @@ abstract class ApplicationApiIntegrationTestCase extends IntegrationTestCase * * @param string $abstract * - * @return \Pterodactyl\Transformers\Api\Application\BaseTransformer - * @throws \Illuminate\Contracts\Container\BindingResolutionException + * @return \Pterodactyl\Transformers\Api\Transformer */ - protected function getTransformer(string $abstract): BaseTransformer + protected function getTransformer(string $abstract): Transformer { - /** @var \Pterodactyl\Transformers\Api\Application\BaseTransformer $transformer */ - $transformer = $this->app->make($abstract); - - Assert::assertInstanceOf(BaseTransformer::class, $transformer); - - return $transformer; + return new $abstract; } } diff --git a/tests/Integration/IntegrationTestCase.php b/tests/Integration/IntegrationTestCase.php index f12eaaef1..d8cea4b54 100644 --- a/tests/Integration/IntegrationTestCase.php +++ b/tests/Integration/IntegrationTestCase.php @@ -6,7 +6,6 @@ use Carbon\CarbonImmutable; use Pterodactyl\Tests\TestCase; use Illuminate\Database\Eloquent\Model; use Pterodactyl\Tests\Traits\Integration\CreatesTestModels; -use Pterodactyl\Transformers\Api\Application\BaseTransformer; abstract class IntegrationTestCase extends TestCase { @@ -40,7 +39,7 @@ abstract class IntegrationTestCase extends TestCase protected function formatTimestamp(string $timestamp): string { return CarbonImmutable::createFromFormat(CarbonImmutable::DEFAULT_TO_STRING_FORMAT, $timestamp) - ->setTimezone(BaseTransformer::RESPONSE_TIMEZONE) + ->setTimezone('UTC') ->toIso8601String(); } }