79 lines
1.9 KiB
PHP
79 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Setting;
|
|
use Illuminate\Console\Command;
|
|
|
|
class VersionFetch extends Command
|
|
{
|
|
protected $signature = 'version:fetch';
|
|
|
|
protected $description = 'Sync version/build metadata into settings using composer.json as source of truth.';
|
|
|
|
public function handle(): int
|
|
{
|
|
$meta = $this->resolveComposerMetadata();
|
|
if ($meta === null) {
|
|
$this->error('Unable to determine version/build from composer.json.');
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$version = $meta['version'];
|
|
$build = $meta['build'];
|
|
|
|
Setting::updateOrCreate(
|
|
['key' => 'version'],
|
|
['value' => $version],
|
|
);
|
|
|
|
Setting::updateOrCreate(
|
|
['key' => 'build'],
|
|
['value' => (string) $build],
|
|
);
|
|
|
|
$this->info("Version/build synced: {$version} (build {$build}).");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function resolveComposerMetadata(): ?array
|
|
{
|
|
$composerPath = base_path('composer.json');
|
|
|
|
if (!is_file($composerPath) || !is_readable($composerPath)) {
|
|
return null;
|
|
}
|
|
|
|
$raw = file_get_contents($composerPath);
|
|
if ($raw === false) {
|
|
return null;
|
|
}
|
|
|
|
$data = json_decode($raw, true);
|
|
if (!is_array($data)) {
|
|
return null;
|
|
}
|
|
|
|
$version = trim((string) ($data['version'] ?? ''));
|
|
$buildRaw = trim((string) ($data['build'] ?? ''));
|
|
|
|
if ($version === '' || $buildRaw === '') {
|
|
return null;
|
|
}
|
|
|
|
if (!preg_match('/^\d+\.\d+(?:\.\d+)?(?:[-._][0-9A-Za-z.-]+)?$/', $version)) {
|
|
return null;
|
|
}
|
|
|
|
if (!ctype_digit($buildRaw)) {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
'version' => $version,
|
|
'build' => (int) $buildRaw,
|
|
];
|
|
}
|
|
}
|