2024-05-15 00:31:24 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-09-15 23:05:38 -04:00
|
|
|
use App\Exceptions\InvalidVersionNumberException;
|
2024-05-31 17:44:52 -04:00
|
|
|
use App\Models\Scopes\DisabledScope;
|
2024-07-26 09:35:09 -04:00
|
|
|
use App\Models\Scopes\PublishedScope;
|
2024-09-15 23:05:38 -04:00
|
|
|
use App\Support\Version;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2024-08-29 15:46:10 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2024-07-26 02:19:42 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2024-09-15 23:05:38 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
|
2024-05-15 00:31:24 -04:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
|
|
|
|
class ModVersion extends Model
|
|
|
|
{
|
2024-09-12 13:19:52 -04:00
|
|
|
use HasFactory;
|
|
|
|
use SoftDeletes;
|
2024-05-15 00:31:24 -04:00
|
|
|
|
2024-09-16 15:43:25 -04:00
|
|
|
/**
|
|
|
|
* Update the parent mod's updated_at timestamp when the mod version is updated.
|
|
|
|
*/
|
|
|
|
protected $touches = ['mod'];
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* Post boot method to configure the model.
|
|
|
|
*/
|
2024-05-31 17:44:52 -04:00
|
|
|
protected static function booted(): void
|
|
|
|
{
|
|
|
|
static::addGlobalScope(new DisabledScope);
|
2024-09-15 23:05:38 -04:00
|
|
|
|
2024-07-26 09:35:09 -04:00
|
|
|
static::addGlobalScope(new PublishedScope);
|
2024-09-15 23:05:38 -04:00
|
|
|
|
|
|
|
static::saving(function (ModVersion $model) {
|
|
|
|
// Extract the version sections from the version string.
|
|
|
|
try {
|
|
|
|
$version = new Version($model->version);
|
|
|
|
|
|
|
|
$model->version_major = $version->getMajor();
|
|
|
|
$model->version_minor = $version->getMinor();
|
|
|
|
$model->version_patch = $version->getPatch();
|
|
|
|
$model->version_pre_release = $version->getPreRelease();
|
|
|
|
} catch (InvalidVersionNumberException $e) {
|
|
|
|
$model->version_major = 0;
|
|
|
|
$model->version_minor = 0;
|
|
|
|
$model->version_patch = 0;
|
|
|
|
$model->version_pre_release = '';
|
|
|
|
}
|
|
|
|
});
|
2024-05-31 17:44:52 -04:00
|
|
|
}
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
|
|
|
* The relationship between a mod version and mod.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
|
|
|
* @return BelongsTo<Mod, ModVersion>
|
2024-07-20 19:52:36 -04:00
|
|
|
*/
|
2024-05-15 00:31:24 -04:00
|
|
|
public function mod(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Mod::class);
|
|
|
|
}
|
|
|
|
|
2024-07-26 02:19:42 -04:00
|
|
|
/**
|
|
|
|
* The relationship between a mod version and its dependencies.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
|
|
|
* @return HasMany<ModDependency>
|
2024-07-26 02:19:42 -04:00
|
|
|
*/
|
2024-08-29 15:46:10 -04:00
|
|
|
public function dependencies(): HasMany
|
2024-07-26 02:19:42 -04:00
|
|
|
{
|
2024-09-11 15:09:27 -04:00
|
|
|
return $this->hasMany(ModDependency::class)
|
|
|
|
->chaperone();
|
2024-08-29 15:46:10 -04:00
|
|
|
}
|
2024-08-22 17:04:07 -04:00
|
|
|
|
2024-08-29 15:46:10 -04:00
|
|
|
/**
|
|
|
|
* The relationship between a mod version and its resolved dependencies.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
|
|
|
* @return BelongsToMany<ModVersion>
|
2024-08-29 15:46:10 -04:00
|
|
|
*/
|
|
|
|
public function resolvedDependencies(): BelongsToMany
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(ModVersion::class, 'mod_resolved_dependencies', 'mod_version_id', 'resolved_mod_version_id')
|
|
|
|
->withPivot('dependency_id')
|
|
|
|
->withTimestamps();
|
|
|
|
}
|
2024-08-22 17:04:07 -04:00
|
|
|
|
2024-08-29 15:46:10 -04:00
|
|
|
/**
|
|
|
|
* The relationship between a mod version and its each of it's resolved dependencies' latest versions.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
|
|
|
* @return BelongsToMany<ModVersion>
|
2024-08-29 15:46:10 -04:00
|
|
|
*/
|
|
|
|
public function latestResolvedDependencies(): BelongsToMany
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(ModVersion::class, 'mod_resolved_dependencies', 'mod_version_id', 'resolved_mod_version_id')
|
|
|
|
->withPivot('dependency_id')
|
|
|
|
->join('mod_versions as latest_versions', function ($join) {
|
|
|
|
$join->on('latest_versions.id', '=', 'mod_versions.id')
|
|
|
|
->whereRaw('latest_versions.version = (SELECT MAX(mv.version) FROM mod_versions mv WHERE mv.mod_id = mod_versions.mod_id)');
|
|
|
|
})
|
|
|
|
->with('mod:id,name,slug')
|
|
|
|
->withTimestamps();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-09-15 23:05:38 -04:00
|
|
|
* The relationship between a mod version and its latest SPT version.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
2024-09-15 23:05:38 -04:00
|
|
|
* @return HasOneThrough<SptVersion>
|
2024-08-29 15:46:10 -04:00
|
|
|
*/
|
2024-09-15 23:05:38 -04:00
|
|
|
public function latestSptVersion(): HasOneThrough
|
2024-08-29 15:46:10 -04:00
|
|
|
{
|
2024-09-15 23:05:38 -04:00
|
|
|
return $this->hasOneThrough(SptVersion::class, ModVersionSptVersion::class, 'mod_version_id', 'id', 'id', 'spt_version_id')
|
|
|
|
->orderByDesc('spt_versions.version_major')
|
|
|
|
->orderByDesc('spt_versions.version_minor')
|
|
|
|
->orderByDesc('spt_versions.version_patch')
|
|
|
|
->orderByDesc('spt_versions.version_pre_release')
|
2024-08-29 15:46:10 -04:00
|
|
|
->limit(1);
|
2024-07-26 02:19:42 -04:00
|
|
|
}
|
|
|
|
|
2024-07-20 19:52:36 -04:00
|
|
|
/**
|
2024-08-29 15:46:10 -04:00
|
|
|
* The relationship between a mod version and its SPT versions.
|
2024-09-12 13:19:52 -04:00
|
|
|
*
|
|
|
|
* @return BelongsToMany<SptVersion>
|
2024-07-20 19:52:36 -04:00
|
|
|
*/
|
2024-08-29 15:46:10 -04:00
|
|
|
public function sptVersions(): BelongsToMany
|
2024-05-15 00:31:24 -04:00
|
|
|
{
|
2024-09-15 23:05:38 -04:00
|
|
|
return $this->belongsToMany(SptVersion::class)
|
|
|
|
->using(ModVersionSptVersion::class)
|
|
|
|
->orderByDesc('version_major')
|
|
|
|
->orderByDesc('version_minor')
|
|
|
|
->orderByDesc('version_patch')
|
|
|
|
->orderByDesc('version_pre_release');
|
2024-05-17 17:11:54 -04:00
|
|
|
}
|
2024-05-15 00:31:24 -04:00
|
|
|
}
|