'true', 'COMPRAS_ORDER_CANCELLATION_ROOT' => $destination]; if ($changes !== $plan['proposed_settings_only']) { throw new LogicException('Unreviewed cancellation configuration refused.'); } $candidate = $root.'/'.$plan['candidate_root']; OrderCancellationRuntime::verifyArtifact($candidate); $preflight = new Process([PHP_BINARY, 'scripts/check-order-cancellation-pilot.php', '--read-only'], $root, timeout: 30); $preflight->mustRun(); if (json_decode($preflight->getOutput(), true, 32, JSON_THROW_ON_ERROR)['state'] !== 'passed') { throw new LogicException('Cancellation read-only preflight failed.'); } if ($argv[1] === '--preview') { echo json_encode(['state' => 'preview', 'changed_keys' => array_keys($changes), 'database_writes' => false], JSON_THROW_ON_ERROR).PHP_EOL; exit; } if (file_exists($destination)) { throw new LogicException('Preserve existing cancellation component directory.'); } $backup = 'C:/laragon/data/compras/backups/cancellation-activation-'.gmdate('Ymd-His').'-'.bin2hex(random_bytes(6)); if (! mkdir($backup, 0700, true) || file_put_contents($backup.'/settings.before.private.json', $before, LOCK_EX) !== strlen($before) || ! hash_equals(hash('sha256', $before), hash_file('sha256', $backup.'/settings.before.private.json'))) { throw new LogicException('Cancellation configuration backup failed.'); } $db = new PDO('pgsql:host=127.0.0.1;port=5432;dbname='.$settings['DB_DATABASE'], $settings['DB_USERNAME'], $settings['DB_PASSWORD'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); $db->exec('SET default_transaction_read_only=on'); $frames = static function () use ($db): array { $db->beginTransaction(); try { $db->exec('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY'); $out = []; foreach ($db->query("SELECT tablename FROM pg_tables WHERE schemaname='public' ORDER BY tablename")->fetchAll(PDO::FETCH_COLUMN) as $table) { $quoted = '"'.str_replace('"', '""', $table).'"'; $rows = $db->query('SELECT row_to_json(t)::text FROM '.$quoted.' t ORDER BY row_to_json(t)::text')->fetchAll(PDO::FETCH_COLUMN); $out[$table] = hash('sha256', json_encode($rows, JSON_THROW_ON_ERROR)); } $db->commit(); return $out; } finally { if ($db->inTransaction()) { $db->rollBack(); } } }; $baseline = $frames(); file_put_contents($backup.'/table-hashes.private.json', json_encode($baseline, JSON_THROW_ON_ERROR)); $envHash = hash_file('sha256', $root.'/.env'); $assetsHash = hash_file('sha256', $root.'/public/build/manifest.json'); if (! mkdir($destination, 0700) || ! mkdir($destination.'/src', 0700)) { throw new LogicException('Cancellation private destination unavailable.'); } foreach (['manifest.json', 'src/CommitmentReversal.php'] as $file) { if (! copy($candidate.'/'.$file, $destination.'/'.$file)) { throw new LogicException('Cancellation component copy failed before activation.'); } } OrderCancellationRuntime::verifyArtifact($destination); $after = [...$settings, ...$changes]; $encoded = json_encode($after, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR).PHP_EOL; $temporary = $sourcePath.'.cancellation-'.bin2hex(random_bytes(6)).'.tmp'; try { if (file_put_contents($temporary, $encoded, LOCK_EX) !== strlen($encoded) || ! hash_equals(hash('sha256', $before), hash_file('sha256', $sourcePath)) || ! rename($temporary, $sourcePath)) { throw new LogicException('Cancellation atomic configuration selection failed.'); } $check = new Process([PHP_BINARY, 'scripts/check-order-cancellation-pilot.php', '--installed-read-only'], $root, timeout: 30); $check->mustRun(); if (json_decode($check->getOutput(), true, 32, JSON_THROW_ON_ERROR)['state'] !== 'passed' || $frames() !== $baseline || hash_file('sha256', $root.'/.env') !== $envHash || hash_file('sha256', $root.'/public/build/manifest.json') !== $assetsHash || json_decode(file_get_contents($sourcePath), true, 32, JSON_THROW_ON_ERROR) !== $after) { throw new LogicException('Cancellation installation conservation failed; inspect without reverting business rows.'); } $report = ['date' => gmdate('c'), 'state' => 'passed', 'target' => 'http://127.0.0.1:8005', 'authorization' => 'order-cancellation-pilot-authorization.json', 'backup_directory' => $backup, 'component_directory' => $destination, 'manifest_sha256' => OrderCancellationRuntime::MANIFEST_SHA256, 'changed_keys' => array_keys($changes), 'settings_before_sha256' => hash('sha256', $before), 'settings_after_sha256' => hash('sha256', $encoded), 'all_database_tables_preserved' => count($baseline), 'unrelated_settings_financial_receipt_env_and_frontend_preserved' => true, 'policies_grants_migrations_or_business_commands' => false, 'application_complete' => false]; file_put_contents($resultPath, json_encode($report, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL); echo json_encode($report, JSON_THROW_ON_ERROR).PHP_EOL; } finally { if (is_file($temporary)) { unlink($temporary); } }