<?php
declare(strict_types=1);
$r=json_decode(file_get_contents(__DIR__.'/private/request.json'),true);$path=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);$case=null;foreach($r['cases']as $c)if($c['label']===($_SERVER['HTTP_X_CDX_CASE']??'')&&$c['path']===$path)$case=$c;
if(!$case||$_SERVER['REQUEST_METHOD']!=='POST'||($_SERVER['HTTP_TOKEN']??'')!==$r['token']){http_response_code(403);exit;}$actual=$_POST;$expected=$case['params'];ksort($actual);ksort($expected);if($actual!==$expected){http_response_code(403);exit;}
$wire='wire-'.$case['label'].'.json';if(is_file(__DIR__.'/private/'.$wire)){http_response_code(409);exit;}
foreach(json_decode(file_get_contents(__DIR__.'/source-lock.json'),true)as $f=>$sha)if(hash_file('sha256',__DIR__.'/source/application/'.$f)!==$sha){http_response_code(409);exit;}
require __DIR__.'/common.php';$before=$source();$base=json_decode(file_get_contents(__DIR__.'/private/batch-before.json'),true);foreach($before as $k=>$v)if($v!==$base[$k])throw new RuntimeException('Request source drift '.$k);
require __DIR__.'/NativeGate.php';$GLOBALS['NR_ORACLE']=[];$started=microtime(true);ob_start();
register_shutdown_function(static function()use($source,$before,$case,$save,$started,$wire){$body=(string)ob_get_contents();$after=$source();$reflection=[];foreach(['app\\p\\controller\\Stageanalyze','app\\p\\logic\\Stageanalyze','app\\cores\\database\\Dm','think\\db\\connector\\Mysql']as $class)if(class_exists($class,false)){$f=(new ReflectionClass($class))->getFileName();$reflection[$class]=['file'=>$f,'sha256'=>hash_file('sha256',$f)];}$save($wire,['label'=>$case['label'],'path'=>$case['path'],'params'=>$case['params'],'kind'=>$case['kind'],'action'=>$case['action'],'status'=>http_response_code(),'body'=>$body,'body_sha256'=>hash('sha256',$body),'source_before'=>$before,'source_after'=>$after,'source_unchanged'=>$before===$after,'native'=>NRGate::$journal,'oracle'=>$GLOBALS['NR_ORACLE'],'reflection'=>$reflection,'duration'=>microtime(true)-$started,'last_error'=>error_get_last()]);});
$_SERVER['PATH_INFO']=$path;require __DIR__.'/app-init.php';\think\App::run()->send();
