<?php
$case=json_decode(file_get_contents(__DIR__.'/private/current.json'),true);$auth=json_decode(file_get_contents(__DIR__.'/private/auth.json'),true);$path=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);$raw=file_get_contents('php://input');if(empty($case['readonly'])||!preg_match('/^(?:R(?:0[1-9]|1[0-2])|R01-fixed)$/D',$case['label'])||$path!==$case['path']||($_SERVER['HTTP_X_CDX_CASE']??'')!==$case['label']||($_SERVER['HTTP_TOKEN']??'')!==$auth['token']||json_decode($raw,true)!==$case['params']||$_SERVER['REQUEST_METHOD']!=='POST'||is_file(__DIR__.'/private/wire-'.$case['label'].'.json')){http_response_code(403);exit;}
$GLOBALS['FOOD_READ_BATCH_ACTIVE']=true;require __DIR__.'/common.php';require __DIR__.'/read-source.php';FoodGate::$case=$case;$inputError=null;try{$inputs=$readInputs($case);}catch(Throwable $e){$inputs=[];$inputError=['class'=>get_class($e),'message'=>$e->getMessage()];}$save('oracle-'.$case['label'].'.json',['inputs'=>$inputs,'oracle_input_error'=>$inputError,'observer'=>$GLOBALS['FOOD_OBSERVER']]);unset($inputs);$GLOBALS['FOOD_OBSERVER']=[];gc_collect_cycles();$started=microtime(true);$record=['case'=>$case,'raw_request_base64'=>base64_encode($raw),'oracle_file'=>'oracle-'.$case['label'].'.json','oracle_input_error'=>$inputError,'memory_before_App'=>memory_get_usage(true),'observer'=>[]];$reserve=str_repeat('R',131072);register_shutdown_function(static function()use(&$reserve,$case){$reserve=null;$e=error_get_last();if($e&&in_array($e['type'],[E_ERROR,E_PARSE,E_CORE_ERROR,E_COMPILE_ERROR],true)){$f=__DIR__.'/private/fatal-'.$case['label'].'.json';if(!file_exists($f)){file_put_contents($f,json_encode(['error'=>$e,'native'=>FoodGate::$journal,'DML'=>FoodGate::$dml,'peak_memory'=>memory_get_peak_usage(true)],JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));chmod($f,0600);}}});$warnings=[];$old=null;$old=set_error_handler(static function($s,$m,$f,$l)use(&$warnings,&$old){$warnings[]=['severity'=>$s,'message'=>$m,'file'=>$f,'line'=>$l];return is_callable($old)?call_user_func($old,$s,$m,$f,$l):false;});ob_start();
try{$_SERVER['PATH_INFO']=$path;require __DIR__.'/init.php';$response=\think\App::run();$save('response-'.$case['label'].'.json',['status'=>$response->getCode(),'headers'=>$response->getHeader(),'body'=>$response->getContent(),'body_sha256'=>hash('sha256',$response->getContent())]);$save('execution-'.$case['label'].'.json',['native'=>FoodGate::$journal,'DML_attempts'=>FoodGate::$dml,'warnings'=>$warnings,'effective_pagesize'=>\think\Config::get('pagesize'),'transport_spy'=>$GLOBALS['FOOD_TRANSPORT_SPY']??[],'notification_spy'=>$GLOBALS['FOOD_NOTIFICATION_SPY']??[],'memory_before_App'=>$record['memory_before_App'],'duration_before_send'=>microtime(true)-$started]);$response->send();}catch(Throwable $e){$record['source_error']=['class'=>get_class($e),'message'=>$e->getMessage()];}
$record['body']=(string)ob_get_contents();$record['status']=http_response_code();$record['headers']=headers_list();ob_end_flush();flush();$record['native']=FoodGate::$journal;$record['DML_attempts']=FoodGate::$dml;$record['warnings']=$warnings;$record['effective_pagesize']=\think\Config::get('pagesize');$record['transport_spy']=$GLOBALS['FOOD_TRANSPORT_SPY']??[];$record['notification_spy']=$GLOBALS['FOOD_NOTIFICATION_SPY']??[];$record['tchef_config_isolation_calls']=$GLOBALS['FOOD_TCHEF_CONFIG_ISOLATION_CALLS']??[];$record['duration']=microtime(true)-$started;$record['included_files']=[];foreach(get_included_files()as $f)$record['included_files'][]=['path'=>$f,'sha256'=>hash_file('sha256',$f)];restore_error_handler();$save('wire-'.$case['label'].'.json',$record);
