Files
compras/app/Services/StandaloneInstallation.php
T
nala 30bfb1a773
tests / ci (push) Canceled after 0s
commit compras3 2911
2026-11-29 16:03:11 +02:00

91 lines
5.1 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use LogicException;
/** Deployment operation, separate from feature activation and all actor grants. */
final class StandaloneInstallation
{
/** @return array<string,mixed> */
public function prepare(string $database, string $role, string $operator, string $reason, bool $apply = false, bool $rebind = false, bool $reuseComponents = false): array
{
Validator::make(compact('database', 'role', 'operator', 'reason'), [
'database' => 'required|string|max:200', 'role' => 'required|string|max:200',
'operator' => 'required|string|min:3|max:200', 'reason' => 'required|string|min:10|max:4000',
])->validate();
if ($rebind && $reuseComponents) {
throw new LogicException('Recovery binding and initial component reuse are separate operations.');
}
if (config('standalone-runtime.enabled') === true) {
throw new LogicException('Disable runtime activation before explicit installation or recovery.');
}
if (config('standalone-runtime.prepare_only') !== true) {
throw new LogicException('Use explicit console preparation mode to verify the pinned packages before loading executable files.');
}
$settings = config('standalone-runtime');
$settings['enabled'] = true;
$settings['features'] = array_fill_keys(StandaloneRuntime::FEATURES, true);
$runtime = new StandaloneRuntime($settings, new StandaloneArtifact);
$artifact = $runtime->artifact();
$runtime->assertLoadedPackages(allowMissing: true);
foreach (StandaloneArtifact::PACKAGES as $package) {
require_once $artifact['paths'][$package].'/bootstrap.php';
}
$runtime->assertLoadedPackages();
$db = DB::connection();
$binding = StandaloneRuntime::databaseBinding($db);
$identity = $db->selectOne('SELECT current_database() AS database_name,current_user AS role_name');
if ($identity === null || $identity->database_name !== $database || $identity->role_name !== $role) {
throw new LogicException('COMPRAS_RUNTIME_EXPECTED_DESTINATION');
}
$already = $db->getSchemaBuilder()->hasTable('compras_runtime_installations')
&& $db->table('compras_runtime_installations')->where('id', $settings['installation_id'])->exists();
if ($already) {
$runtime->assertInstallation($db);
}
$reused = $reuseComponents && ! $already ? StandaloneComponentSchema::reusable($db) : [];
$plan = ['contract' => StandaloneArtifact::CONTRACT, 'installation_id' => $settings['installation_id'],
'manifest_sha256' => $settings['manifest_sha256'], 'database' => $database, 'role' => $role,
'feature_activation' => false, 'actor_grants' => false, 'rebind' => $rebind,
'reuse_components' => $reuseComponents, 'existing_components' => $reused];
if (! $apply) {
return $plan + ['state' => 'preview', 'written' => false];
}
return $db->transaction(function () use ($db, $runtime, $settings, $binding, $operator, $reason, $rebind, $plan, $reuseComponents, $reused) {
$db->selectOne('SELECT pg_advisory_xact_lock(hashtextextended(?, 0))', ['COMPRAS-STANDALONE-INSTALLATION']);
$schema = $db->getSchemaBuilder();
if (! $schema->hasTable('compras_runtime_installations')) {
if ($rebind) {
throw new LogicException('COMPRAS_RUNTIME_RECOVERY_HISTORY_MISSING');
}
(require database_path('standalone-migrations/2026_11_29_000001_create_runtime_installations.php'))->up();
}
if ($db->table('compras_runtime_installations')->where('id', $settings['installation_id'])->exists()) {
$runtime->assertInstallation($db);
StandaloneSchema::verify($schema, $settings['features']);
return $plan + ['state' => 'already_prepared', 'written' => false];
}
if ($rebind) {
StandaloneSchema::verify($schema, $settings['features']);
}
$db->table('compras_runtime_installations')->insert([
'id' => $settings['installation_id'], 'product' => 'compras', 'contract' => StandaloneArtifact::CONTRACT,
'manifest_sha256' => $settings['manifest_sha256'], 'database_binding_sha256' => $binding,
'recorded_by' => $operator, 'reason' => $reason.($reused === [] ? '' : "\nReused canonical components: ".json_encode(array_intersect_key(StandaloneComponentSchema::SUPPORTED, array_flip($reused)), JSON_THROW_ON_ERROR)), 'installed_at' => now(),
]);
if (! $rebind) {
StandaloneExtensionSchema::install($db, $reuseComponents, $reused);
}
$runtime->assertInstallation($db);
StandaloneSchema::verify($schema, $settings['features']);
return $plan + ['state' => $rebind ? 'rebound' : 'prepared', 'written' => true];
});
}
}