Files
SGF/app/Shared/Installation/CompositionRuntime.php
2026-11-11 09:00:17 +02:00

30 lines
2.1 KiB
PHP

<?php
namespace App\Shared\Installation;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Facades\DB;
/** The composed presentation remains physically isolated, regardless of environment labels. */
final class CompositionRuntime
{
public static function physical(?ConnectionInterface $db=null):array
{
$db??=DB::connection();$c=$db->getConfig();$run=(string)getenv('SGF_ISOLATED_REGRESSION_RUN');
abort_unless($db->getDriverName()==='pgsql'&&preg_match('/^budget_core_sgf_([a-f0-9]{12})$/D',$db->getDatabaseName(),$m)
&&($c['username']??null)==='ir_composition_'.$m[1]&&($c['host']??null)==='127.0.0.1'&&(int)($c['port']??0)===55439
&&preg_match('/^[a-f0-9]{12}$/D',$run)&&empty($c['url'])&&empty($c['read'])&&empty($c['write'])&&($c['search_path']??'public')==='public',403);
$actual=$db->selectOne("SELECT current_database() db,current_user role,inet_server_port() port,current_setting('cluster_name') cluster,rolsuper OR rolcreatedb OR rolcreaterole OR rolreplication OR rolbypassrls elevated FROM pg_roles WHERE rolname=current_user");
abort_unless($actual->db===$db->getDatabaseName()&&$actual->role===$c['username']&&(int)$actual->port===55439&&$actual->cluster==='sgf_modular_regression_'.$run&&!$actual->elevated,403);
return ['database'=>$actual->db,'role'=>$actual->role,'cluster'=>$actual->cluster,'composition'=>$m[1]];
}
public static function assert(?string $connection=null):void
{
abort_unless(app()->environment('budget-testing')&&config('sgf_single_installation.enabled')===true,403);
$db=DB::connection($connection);self::physical($db);$pin=config('sgf_single_installation.schema_sha256');
abort_unless(is_string($pin)&&preg_match('/^[a-f0-9]{64}$/D',$pin)&&$db->getSchemaBuilder()->hasTable('sgf_composition_schema'),503,'Composição SGF não instalada ou não conferida.');
abort_unless(hash_equals($pin,(string)$db->table('sgf_composition_schema')->where('id',1)->value('source_hash')),503,'A composição SGF mudou; exige nova conferência.');
}
}