Less obtuse mounting code

This commit is contained in:
Dane Everitt 2020-08-25 19:01:08 -07:00
parent 96fef94578
commit 9d95c5ab32
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 89 additions and 23 deletions

View file

@ -2,6 +2,9 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
use MountNode;
use MountServer;
/** /**
* @property int $id * @property int $id
* @property string $uuid * @property string $uuid
@ -45,11 +48,6 @@ class Mount extends Model
*/ */
protected $attributes = [ protected $attributes = [
'id' => 'int', 'id' => 'int',
'uuid' => 'string',
'name' => 'string',
'description' => 'string',
'source' => 'string',
'target' => 'string',
'read_only' => 'bool', 'read_only' => 'bool',
'user_mountable' => 'bool', 'user_mountable' => 'bool',
]; ];
@ -89,20 +87,18 @@ class Mount extends Model
/** /**
* Returns all nodes that have this mount assigned. * Returns all nodes that have this mount assigned.
* *
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/ */
public function nodes() public function nodes()
{ {
return $this->belongsToMany(Node::class); return $this->hasManyThrough(Server::class, MountNode::class);
} }
/** /**
* Returns all servers that have this mount assigned. * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/ */
public function servers() public function servers()
{ {
return $this->belongsToMany(Server::class); return $this->hasManyThrough(Server::class, MountServer::class);
} }
} }

39
app/Models/MountNode.php Normal file
View file

@ -0,0 +1,39 @@
<?php
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Mount;
use Illuminate\Database\Eloquent\Model;
class MountNode extends Model
{
/**
* @var bool
*/
public $timestamps = false;
/**
* @var bool
*/
public $incrementing = false;
/**
* @var string
*/
protected $table = 'mount_node';
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function node()
{
return $this->belongsTo(Node::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function mount()
{
return $this->belongsTo(Mount::class);
}
}

View file

@ -0,0 +1,39 @@
<?php
use Pterodactyl\Models\Mount;
use Pterodactyl\Models\Server;
use Illuminate\Database\Eloquent\Model;
class MountServer extends Model
{
/**
* @var bool
*/
public $timestamps = false;
/**
* @var bool
*/
public $incrementing = false;
/**
* @var string
*/
protected $table = 'mount_server';
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function server()
{
return $this->belongsTo(Server::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function mount()
{
return $this->belongsTo(Mount::class);
}
}

View file

@ -9,6 +9,7 @@
namespace Pterodactyl\Services\Servers; namespace Pterodactyl\Services\Servers;
use Pterodactyl\Models\Mount;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
@ -71,17 +72,6 @@ class ServerConfigurationStructureService
*/ */
protected function returnCurrentFormat(Server $server) protected function returnCurrentFormat(Server $server)
{ {
$mounts = $server->mounts;
foreach ($mounts as $mount) {
unset($mount->id);
unset($mount->uuid);
unset($mount->name);
unset($mount->description);
$mount->read_only = $mount->read_only == 1;
unset($mount->user_mountable);
unset($mount->pivot);
}
return [ return [
'uuid' => $server->uuid, 'uuid' => $server->uuid,
'suspended' => (bool) $server->suspended, 'suspended' => (bool) $server->suspended,
@ -112,7 +102,9 @@ class ServerConfigurationStructureService
], ],
'mappings' => $server->getAllocationMappings(), 'mappings' => $server->getAllocationMappings(),
], ],
'mounts' => $mounts, 'mounts' => $server->mounts->map(function (Mount $mount) {
return $mount->only('uuid', 'source', 'description', 'read_only');
})->toArray(),
]; ];
} }