Use new attribute casting

This commit is contained in:
Lance Pioch 2022-10-24 12:01:26 -04:00
parent e49ba65709
commit 8a526539be
7 changed files with 88 additions and 80 deletions

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
@ -85,25 +86,22 @@ class Allocation extends Model
/**
* Return a hashid encoded string to represent the ID of the allocation.
*/
public function getHashidAttribute(): string
public function hashid(): Attribute
{
return app()->make('hashids')->encode($this->id);
return new Attribute(
get: fn () => app()->make('hashids')->encode($this->id),
);
}
/**
* Accessor to automatically provide the IP alias if defined.
* Accessor and mutator to automatically provide the IP alias if defined.
*/
public function getAliasAttribute(?string $value): string
public function alias(): Attribute
{
return (is_null($this->ip_alias)) ? $this->ip : $this->ip_alias;
}
/**
* Accessor to quickly determine if this allocation has an alias.
*/
public function getHasAliasAttribute(?string $value): bool
{
return !is_null($this->ip_alias);
return new Attribute(
get: fn ($ip) => is_null($this->ip_alias) ? $this->ip : $this->ip_alias,
set: fn ($ip) => ['ip_alias' => $ip],
);
}
public function toString(): string

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -148,113 +149,104 @@ class Egg extends Model
* Returns the install script for the egg; if egg is copying from another
* it will return the copied script.
*/
public function getCopyScriptInstallAttribute(): ?string
public function copyScriptInstall(): Attribute
{
if (!is_null($this->script_install) || is_null($this->copy_script_from)) {
return $this->script_install;
}
return $this->scriptFrom->script_install;
return new Attribute(
get: fn () => (!is_null($this->script_install) || is_null($this->copy_script_from))
? $this->script_install : $this->scriptFrom->script_install,
);
}
/**
* Returns the entry command for the egg; if egg is copying from another
* it will return the copied entry command.
*/
public function getCopyScriptEntryAttribute(): string
public function copyScriptEntry(): Attribute
{
if (!is_null($this->script_entry) || is_null($this->copy_script_from)) {
return $this->script_entry;
}
return $this->scriptFrom->script_entry;
return new Attribute(
get: fn () => (!is_null($this->script_entry) || is_null($this->copy_script_from))
? $this->script_entry : $this->scriptFrom->script_entry,
);
}
/**
* Returns the install container for the egg; if egg is copying from another
* it will return the copied install container.
*/
public function getCopyScriptContainerAttribute(): string
public function copyScriptContainer(): Attribute
{
if (!is_null($this->script_container) || is_null($this->copy_script_from)) {
return $this->script_container;
}
return $this->scriptFrom->script_container;
return new Attribute(
get: fn () => (!is_null($this->script_container) || is_null($this->copy_script_from))
? $this->script_container : $this->scriptFrom->script_container,
);
}
/**
* Return the file configuration for an egg.
*/
public function getInheritConfigFilesAttribute(): ?string
public function inheritConfigFiles(): Attribute
{
if (!is_null($this->config_files) || is_null($this->config_from)) {
return $this->config_files;
}
return $this->configFrom->config_files;
return new Attribute(
get: fn () => (!is_null($this->config_files) || is_null($this->config_from))
? $this->config_files : $this->configFrom->config_files,
);
}
/**
* Return the startup configuration for an egg.
*/
public function getInheritConfigStartupAttribute(): ?string
public function inheritConfigStartup(): Attribute
{
if (!is_null($this->config_startup) || is_null($this->config_from)) {
return $this->config_startup;
}
return $this->configFrom->config_startup;
return new Attribute(
get: fn () => (!is_null($this->config_startup) || is_null($this->config_from))
? $this->config_startup : $this->configFrom->config_startup,
);
}
/**
* Return the log reading configuration for an egg.
*/
public function getInheritConfigLogsAttribute(): ?string
public function inheritConfigLogs(): Attribute
{
if (!is_null($this->config_logs) || is_null($this->config_from)) {
return $this->config_logs;
}
return $this->configFrom->config_logs;
return new Attribute(
get: fn () => (!is_null($this->config_logs) || is_null($this->config_from))
? $this->config_logs : $this->configFrom->config_logs,
);
}
/**
* Return the stop command configuration for an egg.
*/
public function getInheritConfigStopAttribute(): ?string
public function inheritConfigStop(): Attribute
{
if (!is_null($this->config_stop) || is_null($this->config_from)) {
return $this->config_stop;
}
return $this->configFrom->config_stop;
return new Attribute(
get: fn () => (!is_null($this->config_stop) || is_null($this->config_from))
? $this->config_stop : $this->configFrom->config_stop,
);
}
/**
* Returns the features available to this egg from the parent configuration if there are
* no features defined for this egg specifically and there is a parent egg configured.
*/
public function getInheritFeaturesAttribute(): ?array
public function inheritFeatures(): Attribute
{
if (!is_null($this->features) || is_null($this->config_from)) {
return $this->features;
}
return $this->configFrom->features;
return new Attribute(
get: fn () => (!is_null($this->features) || is_null($this->config_from))
? $this->features : $this->configFrom->features,
);
}
/**
* Returns the features available to this egg from the parent configuration if there are
* no features defined for this egg specifically and there is a parent egg configured.
*/
public function getInheritFileDenylistAttribute(): ?array
public function inheritFileDenylist(): Attribute
{
if (is_null($this->config_from)) {
return $this->file_denylist;
}
return $this->configFrom->file_denylist;
return new Attribute(
get: fn () => is_null($this->config_from)
? $this->file_denylist : $this->configFrom->file_denylist,
);
}
/**

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
@ -75,9 +76,11 @@ class EggVariable extends Model
'user_viewable' => 0,
];
public function getRequiredAttribute(): bool
public function required(): Attribute
{
return in_array('required', explode('|', $this->rules));
return Attribute::make(
get: fn ($value, $attributes) => in_array('required', explode('|', $this->rules)),
);
}
public function egg(): HasOne

View file

@ -5,6 +5,7 @@ namespace Pterodactyl\Models;
use Cron\CronExpression;
use Carbon\CarbonImmutable;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Pterodactyl\Contracts\Extensions\HashidsInterface;
@ -133,9 +134,11 @@ class Schedule extends Model
/**
* Return a hashid encoded string to represent the ID of the schedule.
*/
public function getHashidAttribute(): string
public function hashid(): Attribute
{
return Container::getInstance()->make(HashidsInterface::class)->encode($this->id);
return Attribute::make(
get: fn () => Container::getInstance()->make(HashidsInterface::class)->encode($this->id),
);
}
/**

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -55,9 +56,11 @@ class Subuser extends Model
/**
* Return a hashid encoded string to represent the ID of the subuser.
*/
public function getHashidAttribute(): string
public function hashid(): Attribute
{
return app()->make('hashids')->encode($this->id);
return Attribute::make(
get: fn () => app()->make('hashids')->encode($this->id),
);
}
/**

View file

@ -3,6 +3,7 @@
namespace Pterodactyl\Models;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Znck\Eloquent\Traits\BelongsToThrough;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Pterodactyl\Contracts\Extensions\HashidsInterface;
@ -104,9 +105,11 @@ class Task extends Model
/**
* Return a hashid encoded string to represent the ID of the task.
*/
public function getHashidAttribute(): string
public function hashid(): Attribute
{
return Container::getInstance()->make(HashidsInterface::class)->encode($this->id);
return Attribute::make(
get: fn () => Container::getInstance()->make(HashidsInterface::class)->encode($this->id),
);
}
/**

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Pterodactyl\Rules\Username;
use Pterodactyl\Facades\Activity;
use Illuminate\Support\Collection;
@ -209,19 +210,24 @@ class User extends Model implements
}
/**
* Store the username as a lowercase string.
* Get and Set the username as a lowercase string.
*/
public function setUsernameAttribute(string $value)
public function username(): Attribute
{
$this->attributes['username'] = mb_strtolower($value);
return new Attribute(
get: fn () => mb_strtolower($this->username),
set: fn ($username) => mb_strtolower($username),
);
}
/**
* Return a concatenated result for the accounts full name.
*/
public function getNameAttribute(): string
public function name(): Attribute
{
return trim($this->name_first . ' ' . $this->name_last);
return new Attribute(
get: fn () => trim($this->name_first . ' ' . $this->name_last),
);
}
/**