<?php
declare(strict_types=1);
$case=json_decode((string)file_get_contents(__DIR__.'/private/current-case.json'),true);$auth=json_decode((string)file_get_contents(__DIR__.'/private/auth.json'),true);$path=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);$raw=file_get_contents('php://input');$actual=json_decode($raw,true);
if(!$case||$case['path']!==$path||($_SERVER['HTTP_X_CDX_CASE']??'')!==$case['label']||($_SERVER['HTTP_TOKEN']??'')!==$auth['token']||$_SERVER['REQUEST_METHOD']!=='POST'||$actual!==$case['params']||strpos($_SERVER['CONTENT_TYPE']??'','application/json')!==0){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';require __DIR__.'/observe.php';require __DIR__.'/NativeGate.php';NFGate::$lane=$lane;NFGate::$case=$case;$before=$source();$lease=json_decode(file_get_contents(__DIR__.'/private/current-lease.json'),true);if($before!==$lease){http_response_code(409);exit;}$exact=$q('SELECT * FROM ydy_nutrition WHERE uuid=?',[$case['params']['uuid']]);if(count($exact)!==1)throw new RuntimeException('One exact own row');
$started=microtime(true);ob_start();
register_shutdown_function(static function()use($source,$protected,$physical,$included,$before,$case,$save,$started,$wire,$raw){$body=(string)ob_get_contents();$record=['case'=>$case,'raw_request_base64'=>base64_encode($raw),'status'=>http_response_code(),'body'=>$body,'body_sha256'=>hash('sha256',$body),'native'=>NFGate::$journal,'DML_attempts'=>NFGate::$dml,'before'=>$before,'included_files'=>$included(),'duration'=>microtime(true)-$started,'last_error'=>error_get_last()];try{$record['after']=$source();$record['protected_equal']=$protected($before,$record['after'],$case['params']['uuid']);$record['physical']=$physical($case['params']['uuid']);$record['oracle']=$GLOBALS['NF_ORACLE'];}catch(Throwable $e){$record['oracle_error']=['class'=>get_class($e),'message'=>$e->getMessage()];}$save($wire,$record);});
$_SERVER['PATH_INFO']=$path;require __DIR__.'/app-init.php';\think\App::run()->send();

