74 lines
2.0 KiB
PHP
74 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Setting;
|
|
use Illuminate\Console\Command;
|
|
|
|
class VersionSet extends Command
|
|
{
|
|
protected $signature = 'version:set {version}';
|
|
|
|
protected $description = 'Set the forum version (e.g. 26.0.1).';
|
|
|
|
public function handle(): int
|
|
{
|
|
$version = trim((string) $this->argument('version'));
|
|
if (!$this->isValidVersion($version)) {
|
|
$this->error('Version format must be X.Y or X.Y.Z (optionally with suffix).');
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$current = Setting::query()->where('key', 'version')->value('value');
|
|
Setting::updateOrCreate(['key' => 'version'], ['value' => $version]);
|
|
|
|
if (!$this->syncComposerVersion($version)) {
|
|
$this->error('Failed to sync version to composer.json.');
|
|
return self::FAILURE;
|
|
}
|
|
|
|
if ($current) {
|
|
$this->info("Version updated: {$current} -> {$version}");
|
|
} else {
|
|
$this->info("Version set to {$version}");
|
|
}
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function isValidVersion(string $version): bool
|
|
{
|
|
return (bool) preg_match('/^\d+\.\d+(?:\.\d+)?(?:[-._][0-9A-Za-z.-]+)?$/', $version);
|
|
}
|
|
|
|
private function syncComposerVersion(string $version): bool
|
|
{
|
|
$composerPath = base_path('composer.json');
|
|
|
|
if (!is_file($composerPath) || !is_readable($composerPath)) {
|
|
return false;
|
|
}
|
|
|
|
$raw = file_get_contents($composerPath);
|
|
if ($raw === false) {
|
|
return false;
|
|
}
|
|
|
|
$data = json_decode($raw, true);
|
|
if (!is_array($data)) {
|
|
return false;
|
|
}
|
|
|
|
$data['version'] = $version;
|
|
|
|
$encoded = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
|
if ($encoded === false) {
|
|
return false;
|
|
}
|
|
|
|
$encoded .= "\n";
|
|
|
|
return file_put_contents($composerPath, $encoded) !== false;
|
|
}
|
|
}
|