<?php
declare(strict_types=1);
$started=microtime(true);$path=parse_url($_SERVER['REQUEST_URI']??'',PHP_URL_PATH);$allowed=['supply','dishesType','dietary','vitamin','dietaryFiber','cookingMethod'];$action=basename((string)$path);
if($path!=='/__boundary'&&(!preg_match('#^/terminal/analysis\.analysis/[^/]+$#iD',(string)$path)||!in_array($action,$allowed,true)||($_SERVER['REQUEST_METHOD']??'')!=='GET')){http_response_code(403);echo '{"denied":true}';return;}
$root=getenv('ANALYSIS_ROOT');$runtime=getenv('ANALYSIS_RUNTIME');ini_set('log_errors','1');ini_set('error_log',$runtime.'/private-errors.log');$_SERVER['SCRIPT_FILENAME']=$root.'/public/index.php';$_SERVER['SCRIPT_NAME']='/index.php';$_SERVER['PHP_SELF']='/index.php';define('TP_ENV','poc');define('BUSINESS','aizhct');define('ENTERPRISE_NO','10001');require $root.'/vendor/autoload.php';spl_autoload_register(static function($class)use($root){foreach(['app\\'=>'app/','cores\\'=>'cores/'] as $prefix=>$dir)if(strpos($class,$prefix)===0){$f=$root.'/'.$dir.str_replace('\\','/',substr($class,strlen($prefix))).'.php';if(is_file($f))require $f;}},true,true);require __DIR__.'/external_boundary.php';
if($path==='/__boundary'){$classes=['app\\terminal\\controller\\analysis\\Analysis','app\\terminal\\service\\analysis\\Analysis','app\\terminal\\service\\meal\\Meal','app\\terminal\\model\\meal\\Meal','app\\common\\model\\meal\\Meal','app\\terminal\\model\\meal\\MealInfos','app\\common\\model\\meal\\MealInfos','app\\terminal\\model\\meal\\MealRecommend','app\\common\\model\\meal\\MealRecommend','app\\common\\model\\dishes\\Dishes','app\\terminal\\model\\dishes\\Dishes','app\\common\\model\\dishes\\DishesCate','app\\common\\model\\ConfigEnum','cores\\BaseModel','app\\common\\database\\DmBuilder'];$out=[];foreach($classes as $class){$file=(new ReflectionClass($class))->getFileName();$relative=str_replace('\\','/',$class).'.php';$out[$relative]=['actual_private_path'=>realpath($file)===realpath($root.'/'.$relative),'sha256'=>hash_file('sha256',$file)];}header('Content-Type: application/json');echo json_encode(['reflection'=>$out,'private_cache'=>true,'external_forbidden'=>true]);return;}
$scenario=$_SERVER['HTTP_X_PILOT_SCENARIO']??'';if(!preg_match('/^PILOT_TA_[A-Z0-9_]{1,60}$/D',$scenario)){http_response_code(403);echo '{"denied":true}';return;}$private=json_decode(file_get_contents($runtime.'/metadata-samples.json'),true);$chosen=end($private['selected']);$tenant=$chosen['store_id'];
$app=new \think\App($root);$app->setEnvName('poc');$app->setRuntimePath($runtime.'/runtime/');$app->initialize();$app->env->set('store.host',['analysis-natural.invalid'=>$tenant]);if($app->request->host()!=='analysis-natural.invalid'||getStoreId()!==$tenant){http_response_code(403);echo '{"tenant_denied":true}';return;}$cfg=$app->config->get('database');foreach($cfg['connections'] as &$c){$c['trigger_sql']=true;$c['debug']=false;}unset($c);$app->config->set($cfg,'database');$app->db->setConfig($app->config);$app->config->set(['default'=>'file','stores'=>['file'=>['type'=>'File','path'=>$runtime.'/cache/','prefix'=>'','expire'=>0]]],'cache');$app->instance('cache',new \think\Cache($app));$log=$app->config->get('log');$log['close']=true;foreach($log['channels'] as &$c)$c['close']=true;unset($c);$app->config->set($log,'log');require __DIR__.'/SqlCoverageRecorder.php';$rec=new SqlCoverageRecorder($runtime.'/queries.jsonl',$root,$scenario);$app->db->listen(static function($sql,$elapsed=null)use($rec,$runtime,$scenario){if(strpos((string)$sql,'CONNECT:')===0)return;$rec->observe($sql,$elapsed);file_put_contents($runtime.'/private-sql.jsonl',json_encode(['scenario'=>$scenario,'sql'=>$sql],JSON_UNESCAPED_UNICODE)."\n",FILE_APPEND|LOCK_EX);chmod($runtime.'/private-sql.jsonl',0600);});register_shutdown_function([$rec,'finish']);
$http=$app->http;$response=$http->run();file_put_contents($runtime.'/private-wire.jsonl',json_encode(['scenario'=>$scenario,'path'=>$path,'http_status'=>$response->getCode(),'elapsed_ms'=>(microtime(true)-$started)*1000,'raw'=>$response->getContent()],JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION)."\n",FILE_APPEND|LOCK_EX);chmod($runtime.'/private-wire.jsonl',0600);$response->send();$http->end($response);
